1. 程式人生 > >spring xml事務 (spring事務三)

spring xml事務 (spring事務三)

xml配置

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.3.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://127.0.0.1:3306/test" />
		<property name="username" value="root" />
		<property name="password" value="root" />
	</bean>
	
	<!-- spring與mybatis的整合類 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:mybaties.xml" />
	</bean>
	
	<!-- 掃dao包 -->
	<bean id="mapperFactoryBean" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="priv.dengjl.springmybatis.dao"/>
		<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
		<property name="annotationClass" value="org.springframework.stereotype.Repository"/>
	</bean>
	
	<!-- 掃service包 -->
	<context:component-scan base-package="priv.dengjl.springmybatis.service" />

	<!-- 事務管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	
	<!-- 事務控制,xml配置方式 -->
	<!-- 通知 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!-- 測試類只用到其中insert -->
			<tx:method name="insert*" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
			
			<tx:method name="delete*" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
			<tx:method name="update*" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
			<tx:method name="list*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
		</tx:attributes>
	</tx:advice>
	
	<!-- 切面 -->
	<aop:config>
		<aop:advisor advice-ref="txAdvice" pointcut="execution(* priv.dengjl.springmybatis.service.*.*(..))" />
	</aop:config>
</beans>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<!-- 配置資訊 -->
	<typeAliases>
		<typeAlias type="priv.dengjl.springmybatis.bean.Role" alias="role"/>
	</typeAliases>
	<mappers>
		<mapper resource="priv/dengjl/springmybatis/mapper/RoleMapper.xml" />
	</mappers>
</configuration>

service介面

xml配置了insert*攔截器,這些方法會把事務代理

@Service
public class RoleXmlServiceImpl implements RoleXmlService {
	
	@Autowired
	private RoleMapper dao;
	
	// 方法名上沒有註解
	@Override
	public void insertRole(Role role) {
		dao.insertRole(role);
	}
	
}

測試類

// xml事務
private static final Logger logger = LoggerFactory.getLogger(TestMapperScanner.class);
	
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("spring-tx-xml.xml");
		RoleXmlService bean = context.getBean(RoleXmlService.class);
		
		Role role = new Role();
		role.setId(10089);
		role.setName("10089");
		role.setDesc("10089");
		bean.insertRole(role);
		
		logger.debug(bean.toString());
	}
}

測試資訊

Creating new transaction with name [priv.dengjl.springmybatis.service.RoleXmlServiceImpl.insertRole]: PROPAGATION_REQUIRED,ISOLATION_REPEATABLE_READ
Acquired Connection [jdbc:mysql://127.0.0.1:3306/test, [email protected], MySQL Connector/J] for JDBC transaction
Changing isolation level of JDBC Connection [jdbc:mysql://127.0.0.1:3306/test, [email protected], MySQL Connector/J] to 4
Switching JDBC Connection [jdbc:mysql://127.0.0.1:3306/test, [email protected], MySQL Connector/J] to manual commit
Creating a new SqlSession
Registering transaction synchronization for SqlSession [[email protected]]
JDBC Connection [jdbc:mysql://127.0.0.1:3306/test, [email protected], MySQL Connector/J] will be managed by Spring
==>  Preparing: insert into role(id, name, `desc`) values(?, ?, ?) 
==> Parameters: 10089(Integer), 10089(String), 10089(String)
<==    Updates: 1
Releasing transactional SqlSession [[email protected]]
Transaction synchronization committing SqlSession [[email protected]]
Transaction synchronization deregistering SqlSession [[email protected]]
Transaction synchronization closing SqlSession [[email protected]]
Initiating transaction commit
Committing JDBC transaction on Connection [jdbc:mysql://127.0.0.1:3306/test, [email protected], MySQL Connector/J]
Releasing JDBC Connection [jdbc:mysql://127.0.0.1:3306/test, [email protected], MySQL Connector/J] after transaction
Returning JDBC Connection to DataSource
[email protected]