1、配置文件 applicationContext.xml:
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource"/>
2、在需要加事务的方法上加上
DataSourceTransactionManager transactionManager = (DataSourceTransactionManager) ctx
.getBean("txManager");
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); // 事物隔离级别,开启新事务,这样会比较安全些。
TransactionStatus status = transactionManager.getTransaction(def); // 获得事务状态
try {
//逻辑代码,可以写上你的逻辑处理代码
transactionManager.commit(status);
} catch (Exception e) {
transactionManager.rollback(status);
}
这是在有 ApplicationContext 的情况下、
3. ApplicationContext 不存在的情况下
@Autowired
private DataSourceTransactionManager txManager;
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);// 事物隔离级别,开启新事务
TransactionStatus status = txManager.getTransaction(def); // 获得事务状态
try{
//逻辑代码,可以写上你的逻辑处理代码
txManager.commit(status);
}catch(Exception e){
txManager.rollback(status);
}
3. Spring声明式事务配置
<!-- 事务配置 spring 3.0 -->
<beanid="transactionManager class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<propertyname="dataSource"ref="dataSource"/>
</bean>
<aop:config>
<aop:pointcutexpression="execution(public * com.*.service.impl.*Impl.*(..))"id="pointcut"/>
<aop:advisoradvice-ref="txAdvice"pointcut-ref="pointcut"/>
</aop:config>
<tx:adviceid="txAdvice"transaction-manager="transactionManager">
<tx:attributes>
<tx:methodname="query*"propagation="REQUIRED"read-only="true"/>
<tx:methodname="find*"propagation="REQUIRED"read-only="true"/>
<tx:methodname="get*"propagation="REQUIRED"read-only="true"/>
<tx:methodname="save*"propagation="REQUIRED"/>
<tx:methodname="add*"propagation="REQUIRED"/>
<tx:methodname="create*"propagation="REQUIRED"/>
<tx:methodname="delete*"propagation="REQUIRED"/>
<tx:methodname="del*"propagation="REQUIRED"/>
<tx:methodname="remove*"propagation="REQUIRED"/>
<tx:methodname="modify*"propagation="REQUIRED"/>
<tx:methodname="update*"propagation="REQUIRED"/>
<tx:methodname="clear*"propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
在使用Spring声明式事务时,不需要手动的开启事务和关闭事务,但是对于一些场景则需要开发人员手动的提交事务,比如说一个操作中需要处理大量的数据库更改,可以将大量的数据库更改分批的提交,又比如一次事务中一类的操作的失败并不需要对其他类操作进行事务回滚,就可以将此类的事务先进行提交,这样就需要手动的获取Spring管理的Transaction来提交事务。
1、applicationContext.xml配置
1 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 2 <property name="dataSource" ref="dataSource" /> 3 </bean> 4 5 <tx:advice id="txAdvice" transaction-manager="transactionManager"> 6 <tx:attributes> 7 <tx:method name="*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> 8 <tx:method name="find*" read-only="true" propagation="SUPPORTS" /> 9 <tx:method name="get*" read-only="true" propagation="SUPPORTS" /> 10 <tx:method name="select*" read-only="true" propagation="SUPPORTS" /> 11 <tx:method name="list*" read-only="true" propagation="SUPPORTS" /> 12 <tx:method name="load*" read-only="true" propagation="SUPPORTS" /> 13 </tx:attributes> 14 </tx:advice> 15 16 <aop:config> 17 <aop:pointcut id="servicePointCut" expression="execution(* com.xxx.xxx.service..*(..))" /> 18 <aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointCut" /> 19 </aop:config>
2、手动提交事务
1 @Resource(name="transactionManager") 2 private DataSourceTransactionManager transactionManager; 3 4 DefaultTransactionDefinition transDefinition = new DefaultTransactionDefinition(); 5 //开启新事物 6 transDefinition.setPropagationBehavior(DefaultTransactionDefinition.PROPAGATION_REQUIRES_NEW); 7 TransactionStatus transStatus = transactionManager.getTransaction(transDefinition); 8 try { 9 //TODO 10 transactionManager.commit(transStatus); 11 } catch (Exception e) { 12 transactionManager.rollback(transStatus); 13 }