spring管理事务的方法有三种。
第一种是编程式管理,首先需要在业务层声明dataSourceTemplate,
private TransactionTemplate transactionTemplate;
public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
this.transactionTemplate = transactionTemplate;
}
然后在spring配置文件中声明一个事务模板,
<!-- 定义事务管理模板 -->
<bean id="transactionTemplate"
class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager"></property>
</bean>
模板下需要有一个事务管理器,事务管理器下面又需要数据源
<!-- 定义事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dateSource"></property>
</bean>
<!-- 配置数据源 -->
<bean id="dateSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="username" value="${name}" />
<property name="password" value="${password}" />
<property name="url" value="${url}" />
<property name="driverClassName" value="${driverClassName}" />
</bean>
在业务层使用的时候可以将需要实现的业务包裹在TransactionCallback的内部方法里面 最后拿给事务管理模板来判断事务是提交还是回滚。
TransactionCallback tc = new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus tx) {
accountDao.outMoney(out, money);
//int i = 3 / 0; // 检验在事务过程中是否有错误需要回滚
accountDao.inMoney(in, money);
}
};
transactionTemplate.execute(tc);
这样就实现了编程式的事务管理
方法2
编程式的事务管理完全可以使用aop思想来完成,所以sping就为我们已经提供了XML的事务管理方式
首先 需要配置spring核心文件的命名空间
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
然后定义通知
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes >
<tx:method name="transfer" /></tx:attributes>
</tx:advice>
method就是想要添加事务的方法
同事需要提供事务管理器
<!-- 定义事务管理器 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dateSource"></property>
</bean>
也需要提供数据源
<!-- 配置数据源 -->
<bean id="dateSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="username" value="${name}" />
<property name="password" value="${password}" />
<property name="url" value="${url}" />
<property name="driverClassName" value="${driverClassName}" />
</bean>
然后配置aop
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(void *..*.*.trans*(..))"/> //利用通配符来寻找切面
</aop:config>
这样就不需要在业务层添加那么些代码,也不用定义TransactionTemplate了。方法还是原来的方法。
方法三:注解方式管理(现在最流行的 )
注解开发非常的简单,
首先在spring核心配置文件中配置事务管理驱动,同时制定一个事务管理器
<!-- 提供事务管理驱动 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 定义事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dateSource"></property>
</bean>
<!-- 配置数据源 -->
<bean id="dateSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="username" value="${name}" />
<property name="password" value="${password}" />
<property name="url" value="${url}" />
<property name="driverClassName" value="${driverClassName}" />
</bean>
然后在需要添加事务的业务层添加@Transactional
就这么简单。而且能够管理这整个业务层的事务。
PS:第一次写。太啰嗦了。