SpringMVC+MyBatis 事务管理(实例)
前言
spring事务管理包含两种情况,编程式事务、声明式事务。而声明式事务又包括基于注解@transactional和tx+aop的方式。那么本文先分析编程式注解事务和基于注解的声明式事务。 编程式事务管理使用transactiontemplate或者platformtransactionmanager。对于编程式事务spring推荐使用transactiontemplate。
一、编程式事务
spring事务特性
spring中所有的事务策略类都继承自org.springframework.transaction.platformtransactionmanager接口
public interface platformtransactionmanager { transactionstatus gettransaction(transactiondefinition definition) throws transactionexception; void commit(transactionstatus status) throws transactionexception; void rollback(transactionstatus status) throws transactionexception; }
编程式事务transactiontemplate需要手动在代码中处理事务,一般不推荐使用,也不符合spring的思想,因为它直接耦合代码,但各有利弊。先看下transactiontemplate的源码。
public class transactiontemplate extends defaulttransactiondefinition implements transactionoperations, initializingbean { protected final log logger = logfactory.getlog(getclass()); private platformtransactionmanager transactionmanager; public transactiontemplate() { } public transactiontemplate(platformtransactionmanager transactionmanager) { this.transactionmanager = transactionmanager; } public transactiontemplate(platformtransactionmanager transactionmanager, transactiondefinition transactiondefinition) { super(transactiondefinition); this.transactionmanager = transactionmanager; } public void settransactionmanager(platformtransactionmanager transactionmanager) { this.transactionmanager = transactionmanager; } public platformtransactionmanager gettransactionmanager() { return this.transactionmanager; } @override public void afterpropertiesset() { if (this.transactionmanager == null) { throw new illegalargumentexception("property 'transactionmanager' is required"); } } @override public <t> t execute(transactioncallback<t> action) throws transactionexception { if (this.transactionmanager instanceof callbackpreferringplatformtransactionmanager) { return ((callbackpreferringplatformtransactionmanager) this.transactionmanager).execute(this, action); } else { transactionstatus status = this.transactionmanager.gettransaction(this); t result; try { result = action.dointransaction(status); } catch (runtimeexception ex) { // transactional code threw application exception -> rollback rollbackonexception(status, ex); throw ex; } catch (error err) { // transactional code threw error -> rollback rollbackonexception(status, err); throw err; } catch (throwable ex) { // transactional code threw unexpected exception -> rollback rollbackonexception(status, ex); throw new undeclaredthrowableexception(ex, "transactioncallback threw undeclared checked exception"); } this.transactionmanager.commit(status); return result; } } private void rollbackonexception(transactionstatus status, throwable ex) throws transactionexception { logger.debug("initiating transaction rollback on application exception", ex); try { this.transactionmanager.rollback(status); } catch (transactionsystemexception ex2) { logger.error("application exception overridden by rollback exception", ex); ex2.initapplicationexception(ex); throw ex2; } catch (runtimeexception ex2) { logger.error("application exception overridden by rollback exception", ex); throw ex2; } catch (error err) { logger.error("application exception overridden by rollback error", ex); throw err; } } }
从上面的代码可以看到核心方法是execute,该方法入参transactioncallback<t>。查看transactioncallback源码:
public interface transactioncallback<t> { t dointransaction(transactionstatus status); }
那么以上两个源码可以确定我们使用编程式事务管理的方式也就是自己需要重写dointransaction()。ok,那么我们手动使用transactiontemplate处理下。
1、先配置transactionmanager
<!--事务管理器 --> <bean id="transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager"> <property name="datasource" ref="datasource"/> </bean>
2、配置transactiontemplate
<!--编程式事务,推荐使用transactiontemplate--> <bean id="transactiontemplate" class="org.springframework.transaction.support.transactiontemplate"> <property name="transactionmanager" ref="transactionmanager"/> </bean>
3、业务代码处理
@autowired private transactiontemplate transactiontemplate; public int insertuser2(final user user) { integer i= (integer) this.transactiontemplate.execute(new transactioncallback() { public object dointransaction(transactionstatus transactionstatus) { int i = usermapper.insertuser(user); if (i > 0) { system.out.println("success"); } int j = 10 / 0; return i; } }); return i; }
业务代码中我们使用by zero的异常故意抛出,你会发现能继续打印success,当你断点debug时,你会发现数据库一直是锁定状态,直到你程序执行完。如下图:
二、基于transactional注解的事务管理
当前应该是使用最清爽的事务管理方式了,也符合spring的理念,非入侵代码的方式。
1、配置
<!--事务管理器 --> <bean id="transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager"> <property name="datasource" ref="datasource"/> </bean> <!-- 使用注解事务,需要添加transactional注解属性 --> <tx:annotation-driven transaction-manager="transactionmanager"/> <!--启用最新的注解器、映射器--> <mvc:annotation-driven/>
2、配置后只需要在要处理的地方加上transactional注解,而且transactional注解的方式可以应用在类上,也可以应用在方法上,当然只针对public方法。
3、业务代码处理
@transactional public int insertuser(user user) { int i = usermapper.insertuser(user); if (i > 0) { system.out.println("success"); } int j = 10 / 0; return i; }
总结
以上所述是小编给大家介绍的springmvc+mybatis 事务管理,希望对大家有所帮助