标签:pre cte 而且 验证 部分 complete ble final 事务
事务的实现方式有三种,JTA,Spring事务,Web Container方式。本篇讲述Spring事务。
Spring事务分为两个部分核心对象,Spring事务的实现方式。
Spring事务实现的方式有三种。声明式,注解式,代码的方式。声明方式在实际项目中运用比较广泛,注解方式需要在每个方法上添加@Transactional注解,代码冗余度比较高。代码方式只是为了更好的理解Spring事务的机制,在实际项目中并不适用。
事务管理接口。
事务的属性或定义对象。包含的属性如下:
Isolation:事务的隔离级别。
Propagation:事务的传播机制。
Timeout:超时时间。
read-only:是否是只读的事务。
表示一个新创建的事务或者已存在的事务。
代码示例如下:
1 //第一步:创建事务属性对象TransactionDefinition,创建事务管理对象TransactionManager。 2 // 创建事务属性对象 3 DefaultTransactionDefinition def = new DefaultTransactionDefinition(); 4 def.setName("trans1"); 5 def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); 6 // 创建事务管理对象 7 DataSourceTransactionManager transcationManager = new DataSourceTransactionManager(); 8 9 //第二步:获取事务对象, 10 TransactionStatus status = transcationManager.getTransaction(def); 11 //第三步:实现业务逻辑代码 12 try 13 { 14 // 代码逻辑 15 } 16 catch(Exception e) 17 { 18 transcationManager.rollback(status); 19 throw e; 20 } 21 transcationManager.commit(status);
spring事务实现的方式有三种。配置切面方式,注解方式,代码方式。配置切面方式在实际项目中运用比较广泛,注解方式需要在每个方法上添加@Transactional注解,代码冗余度比较高。代码方式只是为了更好的理解Spring事务的机制,在实际项目中并不适用。
第一步:配置事务管理。
<!-- 配置事务管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property> </bean>
第二步:通过”tx:advice”标签配置事务的场景。指定在哪些方法上配置事务,以及这些事务的定义。tx:advice包含一个或多个tx:attribtue,tx:attribute包含一个或多个tx:method。tx:method标签配置具体的事务。
tx:method的属性有
示例如下:
<tx:advice id="txAdvice"> <tx:attributes> <!--方法名称以get开头的事务为只读事务,隔离级别为SERIALIZABLE--> <tx:method name="get*" read-only="true" isolation="SERIALIZABLE" /> <!--方法名称以insert开头的事务,隔离级别为SERIALIZABLE,传播机制为REQUIRED,当发生TestException时回滚事务,ArithmeticException不会回滚事务。--> <tx:method name="insert*" isolation="SERIALIZABLE" timeout="20" propagation="REQUIRED" rollback-for="com.rain.exception.TestException" no-rollbackfor="java.lang.ArithmeticException" /> <tx:method name="*" isolation="READ_COMMITTED" /> </tx:attributes> </tx:advice>
第三步:通过aop:config配置事务切面。aop:config包含一个或多个(aop:pointcut,aop:advisor)。aop:pointcut指定为哪些类配置切面,aop:advisor将tx:advice与aop:pointcut关联起来。其中aop:config标签中expression的格式为:execution(* packageName.className.*(..))。它是SPEL表达式。
示例如下:
<aop:config> <aop:pointcut expression="execution(* com.rain.service.*.*(..))" id="studentServiceMethod" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="studentServiceMethod" /> </aop:config>
如果配置多个事务,重复上述步骤。也可以在aop:config中配置多组(aop:pointcut,aop:advisor)。
第四步:验证。使用Log4J日志,将级别设置为DEBUG。启动时,会加载事务的配置。
2017-05-30 12:22:00 DEBUG NameMatchTransactionAttributeSource:96 - Adding transactional method [get*] with attribute [PROPAGATION_REQUIRED,ISOLATION_READ_COMMITTED,readOnly] 2017-05-30 12:22:00 DEBUG NameMatchTransactionAttributeSource:96 - Adding transactional method [insert*] with attribute [PROPAGATION_REQUIRED,ISOLATION_SERIALIZABLE,timeout_20,-com.rain.exception.TestException,+java.lang.ArithmeticException] 2017-05-30 12:22:00 DEBUG NameMatchTransactionAttributeSource:96 - Adding transactional method [*] with attribute [PROPAGATION_REQUIRED,ISOLATION_READ_COMMITTED]
运行insertXX方法时,显示事务处理的详细过程。
2017-05-30 11:49:57 DEBUG DataSourceTransactionManager:367 - Creating new transaction with name [com.rain.service.impl.StudentServiceImpl.insertStudent]: PROPAGATION_REQUIRED,ISOLATION_SERIALIZABLE,timeout_20,-com.rain.exception.TestException,+java.lang.ArithmeticException。 2017-05-30 11:49:58 TRACE TransactionInterceptor:517 - Completing transaction for [com.rain.service.impl.StudentServiceImpl.insertStudent] after exception: java.lang.ArithmeticException: / by zero 2017-05-30 11:49:58 TRACE RuleBasedTransactionAttribute:131 - Applying rules to determine whether transaction should rollback on java.lang.ArithmeticException: / by zero 2017-05-30 11:49:58 TRACE RuleBasedTransactionAttribute:148 - Winning rollback rule is: NoRollbackRuleAttribute with pattern [java.lang.ArithmeticException] 2017-05-30 11:49:58 DEBUG DataSourceTransactionManager:759 - Initiating transaction commit 2017-05-30 11:49:58 DEBUG DataSourceTransactionManager:310 - Committing JDBC transaction on Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@25df00a0] 2017-05-30 11:49:58 TRACE TransactionSynchronizationManager:331 - Clearing transaction synchronization
当insertXX方法有java.lang.ArithmeticException异常时。不会回滚事务。
2017-05-30 12:09:17 TRACE RuleBasedTransactionAttribute:131 -
Applying rules to determine whether transaction should rollback on java.lang.ArithmeticException: / by zero 2017-05-30 12:09:17 TRACE RuleBasedTransactionAttribute:148 - Winning rollback rule is: NoRollbackRuleAttribute with pattern [java.lang.ArithmeticException]
当insertXX方法中有TestException异常时。回滚事务。
Applying rules to determine whether transaction should rollback on com.rain.exception.TestException: 这是测试用的异常 2017-05-30 12:22:01 TRACE RuleBasedTransactionAttribute:148 - Winning rollback rule is: RollbackRuleAttribute with pattern [com.rain.exception.TestException] 2017-05-30 12:22:01 DEBUG DataSourceTransactionManager:851 - Initiating transaction rollback 2017-05-30 12:22:01 DEBUG DataSourceTransactionManager:325 - Rolling back JDBC transaction on Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@aba625]
第一步:配置事务管理。与声明式事务完全相同。参考配置切面方式第一步
第二步:通过tx:annotation-driven配置事务支持注解方式。tx:annotation-driven的属性有
示例如下:
<!-- 配置注解驱动--> <tx:annotation-driven transaction-manager="transactionManager" mode="proxy" proxy-target-class="true"/>
第三步:在方法上加@Transactional注解,该注解属性的含义如下:
@Transactional(propagation=Propagation.REQUIRED,isolation = Isolation.SERIALIZABLE,timeout=20,rollbackFor = {com.rain.exception.TestException.class}, noRollbackFor = {java.lang.ArithmeticException.class})
第四步:验证。在运行该方法时。会加载事务属性,并创建事务。
2017-05-30 14:06:57 DEBUG AnnotationTransactionAttributeSource:116 -
Adding transactional method ‘com.rain.service.impl.StudentServiceImpl.insertStudent‘ with attribute: PROPAGATION_REQUIRED,ISOLATION_SERIALIZABLE; ‘‘,-com.rain.exception.TestException,-java.lang.NullPointerException,+java.lang.ArithmeticException 2017-05-30 14:06:57 DEBUG DataSourceTransactionManager:367 -
Creating new transaction with name [com.rain.service.impl.StudentServiceImpl.insertStudent]: PROPAGATION_REQUIRED,ISOLATION_SERIALIZABLE; ‘‘,-com.rain.exception.TestException,-java.lang.NullPointerException,+java.lang.ArithmeticException
当发生TestException时,事务回滚。
2017-05-30 14:06:57 TRACE RuleBasedTransactionAttribute:131 - Applying rules to determine whether transaction should rollback on com.rain.exception.TestException: 这是测试用的异常 2017-05-30 14:06:57 TRACE RuleBasedTransactionAttribute:148 - Winning rollback rule is: RollbackRuleAttribute with pattern [com.rain.exception.TestException]
当发生ArithmeticException时,事务不会回滚。
2017-05-30 12:09:17 TRACE RuleBasedTransactionAttribute:131 - Applying rules to determine whether transaction should rollback on java.lang.ArithmeticException: / by zero 2017-05-30 12:09:17 TRACE RuleBasedTransactionAttribute:148 - Winning rollback rule is: NoRollbackRuleAttribute with pattern [java.lang.ArithmeticException]
代码实现事务的方式有两种,一种是TransactionTemplate,该对象可以设置事务的属性。另外一种是TransactionManager方式。TransactionManager方参考核心对象下面的代码示例。
第一步:初始化TransactionTemplate,
1 // 创建事务template类 2 private final TransactionTemplate tranTemp; 3 4 // 创建学生的service类 5 @Autowired 6 private IStudentService studentService; 7 8 // 在构造器中初始化TransactionTemplate 9 public TransactionServiceImpl(PlatformTransactionManager tranManager) { 10 Assert.notNull(tranManager, "The ‘tranManager‘ argument must not be null "); 11 this.tranTemp = new TransactionTemplate(tranManager); 12 }
第二步:调用transactionTemplate的execute方法,参数为TransactionCallback,或者是TransactionCallbackWithoutResult,前者返回类型为void,后者有返回类型。
无返回类型TransactionCallbackWithoutResult
1 // 通过代码的方式实现事务 2 @Override 3 public void insertStudent(Student stu) { 4 tranTemp.execute(new TransactionCallbackWithoutResult() { 5 @Override 6 protected void doInTransactionWithoutResult(TransactionStatus status) { 7 try { 8 // 代码逻辑 9 } catch (Exception e) { 10 e.printStackTrace(); 11 status.setRollbackOnly(); 12 } 13 } 14 }); 15 }
有返回类型TransactionCallback<T>
1 public void insertStudentWithReturn(Student stu) 2 { 3 tranTemp.execute(new TransactionCallback<Student>() { 4 @Override 5 public Student doInTransaction(TransactionStatus status) { 6 try 7 { 8 // 代码逻辑 9 }catch(Exception e) 10 { 11 e.printStackTrace(); 12 // 回滚事务 13 status.setRollbackOnly(); 14 } 15 return stu; 16 }}); 17 }
至此本篇内容结束
标签:pre cte 而且 验证 部分 complete ble final 事务
原文地址:http://www.cnblogs.com/rain144576/p/6931031.html