接口介绍
- PlatformTransactionManager事务管理器
- TransactionDefinition事务定义信息
- ISOLATION隔离级别
- PROPAGATION传播行为
- TIMEOUT超时信息
- TransactionStatus事务具体运行状态
编程式事务管理(很少应用)
通过TransactionTemplate
手动管理事务,在service
使用TransactonTemplate
,依赖DataSourceTransactionManager
,依赖DataSource
构造
spring
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| <beans> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.cj.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test?serverTimeZone=GMT&useUnicode=true&characterEncoding=utf8"/> <property name="user" value="root"/> <property name="password"value="root"/> </bean> <bean id="accountService" class="com.ahao.javaeeDemo.service"> <property id="dao" ref="accountDao"/> <property id="transactionTemplate" ref="transactionTemplate" /> </bean> <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"> <property name="transactionManager" ref="transactionManager"/> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> </beans>
|
在service
层编写java
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public class AccountServiceImpl implements AccountService{ private AccountDao dao; private TransactionTemplate transactionTemplate; public void transfer(String out, String in, Double money){ transactionTemplate.execute(new TransactionCallbackWithoutResult(){ protected void doInTransactionWithoutResult(TransactionStatus status){ dao.outMoney(out, money); int i = 1/0; dao.inMoney (in, money); } }); } }
|
声明式事务管理
通过XML
配置,代码侵入性小,通过AOP
实现
需要导入
基于TransactionProxyFactoryBean的方式(很少应用)
缺点是要为每个增强类添加一个TransactionProxyFactoryBean
配置。
spring
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <beans> <bean id="accountService" class="com.ahao.javaeeDemo.service"> <property id="dao" ref="accountDao"/> </bean> <bean id="accountServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="target" ref="accountService"/> <property name="transactionManager" ref="transactionManager"/> <property name="transactionAttributes"> <props> <prop key="transfer">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> </beans>
|
基于AspectJ的XML方式(推荐)
需要导入
spring
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| <beans> <bean id="accountService" class="com.ahao.javaeeDemo.service"> <property id="dao" ref="accountDao"/> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="transfer" propagation="REQUIRED" isolation="DEFAULT" read-only="false" no-rollback-for="MyException1" rollback-for="MyException2" timeout="-1"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="pointcut" expression="execution(* com.ahao.javaeeDemo.service.AccountService+.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" /> </aop:config>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> </beans>
|
基于注解的方式(代码侵入性强,配置简单)
spring
配置
1 2 3 4 5 6 7 8 9 10 11 12 13
| <beans> <bean id="accountService" class="com.ahao.javaeeDemo.service"> <property id="dao" ref="accountDao"/> </bean> <tx:annotation-driven transaction-manager="transactionManager"/> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> </beans>
|
在类上添加注解
1 2 3 4 5 6 7 8 9 10 11 12 13
| @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT,readOnly = false) public class AccountServiceImpl implements AccountService { private AccountDaoImpl dao; public void setDao(AccountDaoImpl dao) { this.dao = dao; } @Override public void transfer(String out, String in, Double money) { dao.outMoney(out, money); int i = 1/0; dao.inMoney(in, money); } }
|