Spring事务策略是通过PlatformTransactionManager接口提现的
public interface PlatformTransactionManager {
//获取平台无关的事务
TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException;
//平台无关的事务提交
void commit(TransactionStatus status) throws TransactionException;
//平台无关的事务回滚
void rollback(TransactionStatus status) throws TransactionException;
}
TransactionDefinition 接口定义了一个事务的规则,有如下几个属性:
- 事务隔离 当前事务和其它事务的隔离程度
- 事务传播 一个事务上下文已经存在,有几个事务选项可指定该事务性方法的执行行为。
- 事务超时 事务的最长持续时间,超时回滚
- 只读状态 不能修改任何数据
public interface TransactionDefinition {
int PROPAGATION_REQUIRED = 0;
int PROPAGATION_SUPPORTS = 1;
int PROPAGATION_MANDATORY = 2;
int PROPAGATION_REQUIRES_NEW = 3;
int PROPAGATION_NOT_SUPPORTED = 4;
int PROPAGATION_NEVER = 5;
int PROPAGATION_NESTED = 6;
int ISOLATION_DEFAULT = -1;
int ISOLATION_READ_UNCOMMITTED = Connection.TRANSACTION_READ_UNCOMMITTED;
int ISOLATION_READ_COMMITTED = Connection.TRANSACTION_READ_COMMITTED;
int ISOLATION_REPEATABLE_READ = Connection.TRANSACTION_REPEATABLE_READ;
int ISOLATION_SERIALIZABLE = Connection.TRANSACTION_SERIALIZABLE;
int TIMEOUT_DEFAULT = -1;
int getPropagationBehavior();
int getIsolationLevel();
int getTimeout();
boolean isReadOnly();
String getName();
}
TransactionStatus.java代表事务本身
public interface TransactionStatus extends SavepointManager, Flushable {
boolean isNewTransaction();
boolean hasSavepoint();
void setRollbackOnly();
@Override
void flush();
boolean isCompleted();
}
方式一:Spring TransactionTemplate
配置spring-mybatis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 自动扫描 -->
<context:component-scan base-package="com.lq.play" >
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect" />
</context:component-scan>
<!--和schema一起启动@Aspectj支持-->
<!--<aop:aspectj-autoproxy/>-->
<!--启动@Aspectj支持-->
<!--<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"/>-->
<!-- 引入配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${maxActive}"></property>
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="${maxIdle}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${maxWait}"></property>
</bean>
<!--<!– spring和MyBatis完美整合,不需要mybatis的配置映射文件 –>-->
<!--<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">-->
<!--<property name="dataSource" ref="dataSource" />-->
<!--<!–<property name="configLocation" value="classpath:mybatis-config.xml"></property>–>-->
<!--<!– 自动扫描mapping.xml文件 –>-->
<!--<property name="mapperLocations" value="classpath:mapper/*.xml"></property>-->
<!--</bean>-->
<!--<!– DAO接口所在包名,Spring会自动查找其下的类,扫描所有dao –>-->
<!--<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">-->
<!--<property name="basePackage" value="com.lq.play.mapper" />-->
<!--<property name="annotationClass" value="org.springframework.stereotype.Repository" />-->
<!--<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>-->
<!--</bean>-->
<!--<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">-->
<!--<property name="dataSource" ref="dataSource" />-->
<!--</bean>-->
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager"/>
</bean>
<bean id="accountDao" class="com.lq.play.daoimpl.AccountDaoImpl">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="accountService" class="com.lq.play.serviceimpl.AccountServiceImpl">
<property name="ad" ref="accountDao" />
<property name="tt" ref="transactionTemplate" />
</bean>
<!--<!– (事务管理)transaction manager, use JtaTransactionManager for global tx –>-->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!--<tx:annotation-driven transaction-manager="transactionManager"/>-->
</beans>
数据库定义
create table t_account
(
id bigint not null auto_increment primary key,
money int null
);
dao定义
package com.lq.play.daoimpl;
import com.lq.play.dao.AccountDao;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
@Override
public void addMoney(Integer id, Double money) {
getJdbcTemplate().update("update t_account set money = money+? where id = ? ", money,id);
}
@Override
public void minusMoney(Integer id, Double money) {
getJdbcTemplate().update("update t_account set money = money-? where id = ? ", money,id);
}
}
service定义
package com.lq.play.serviceimpl;
import com.lq.play.dao.AccountDao;
import com.lq.play.service.AccountService;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
//@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true)
public class AccountServiceImpl implements AccountService {
private AccountDao ad ;
private TransactionTemplate tt;
@Override
public void transfer(final Integer from,final Integer to,final Double money) {
System.out.println("transfer");
// tt.execute(new TransactionCallbackWithoutResult() {
// @Override
// protected void doInTransactionWithoutResult(TransactionStatus status) {
// //减钱
// ad.minusMoney(from, money);
// int i = 1/0;
// //加钱
// ad.addMoney(to, money);
// }
// });
//减钱
ad.minusMoney(from, money);
int i = 1/0;
//加钱
ad.addMoney(to, money);
}
public void setAd(AccountDao ad) {
this.ad = ad;
}
public void setTt(TransactionTemplate tt) {
this.tt = tt;
}
}
测试
package test;
import javax.annotation.Resource;
import com.lq.play.service.AccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:config/mybatis/spring-mybatis.xml"})
public class Demo {
@Resource(name="accountService")
private AccountService as;
@Test
public void fun1(){
as.transfer(1, 2, 100d);
}
}
结果:执行前数据库
执行后数据库
没有事务控制,出现异常的情况下,由于没有回滚,只做了减钱没有加钱,出现了前后不一致的问题。
使用TemplateTranscation,修改执行方法
@Override
public void transfer(final Integer from,final Integer to,final Double money) {
System.out.println("transfer");
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
//减钱
ad.minusMoney(from, money);
int i = 1/0;
//加钱
ad.addMoney(to, money);
}
});
//减钱
// ad.minusMoney(from, money);
// int i = 1/0;
// //加钱
// ad.addMoney(to, money);
}
可以看到执行前后都为:做了事务回滚操作
修改执行方法,去掉异常,正常执行
@Override
public void transfer(final Integer from,final Integer to,final Double money) {
System.out.println("transfer");
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
//减钱
ad.minusMoney(from, money);
// int i = 1/0;
//加钱
ad.addMoney(to, money);
}
});
//减钱
// ad.minusMoney(from, money);
// int i = 1/0;
// //加钱
// ad.addMoney(to, money);
}
执行后
方式二:声明式配置
配置spring-mybatis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 自动扫描 -->
<context:component-scan base-package="com.lq.play">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/>
</context:component-scan>
<!--和schema一起启动@Aspectj支持-->
<!--<aop:aspectj-autoproxy/>-->
<!--启动@Aspectj支持-->
<!--<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"/>-->
<!-- 引入配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"/>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<!-- 初始化连接大小 -->
<property name="initialSize" value="${initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${maxActive}"></property>
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="${maxIdle}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${maxWait}"></property>
</bean>
<!--<!– spring和MyBatis完美整合,不需要mybatis的配置映射文件 –>-->
<!--<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">-->
<!--<property name="dataSource" ref="dataSource" />-->
<!--<!–<property name="configLocation" value="classpath:mybatis-config.xml"></property>–>-->
<!--<!– 自动扫描mapping.xml文件 –>-->
<!--<property name="mapperLocations" value="classpath:mapper/*.xml"></property>-->
<!--</bean>-->
<!--<!– DAO接口所在包名,Spring会自动查找其下的类,扫描所有dao –>-->
<!--<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">-->
<!--<property name="basePackage" value="com.lq.play.mapper" />-->
<!--<property name="annotationClass" value="org.springframework.stereotype.Repository" />-->
<!--<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>-->
<!--</bean>-->
<!--<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">-->
<!--<property name="dataSource" ref="dataSource" />-->
<!--</bean>-->
<!-- 配置事务通知 -->
<tx:advice transaction-manager="transactionManager" id="txAdvice">
<tx:attributes>
<!-- 以方法为单位,指定方法应用什么事务属性 isolation:隔离级别 propagation:传播行为 read-only:是否只读 -->
<tx:method name="save*" read-only="false" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
<tx:method name="persist*" read-only="false" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
<tx:method name="update*" read-only="false" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
<tx:method name="modify*" read-only="false" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
<tx:method name="delete*" read-only="false" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
<tx:method name="remove*" read-only="false" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
<tx:method name="get*" read-only="true" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
<tx:method name="find*" read-only="true" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
<tx:method name="transfer" read-only="false" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
</tx:attributes>
</tx:advice>
<!-- 配置织入 -->
<aop:config>
<!-- 配置切点表达式 -->
<aop:pointcut id="txPc" expression="execution(* com.lq.play.serviceimpl.*.*(..))"/>
<!-- 配置切面 : 通知+切点 advice-ref:通知的名称 pointcut-ref:切点的名称 -->
<aop:advisor pointcut-ref="txPc" advice-ref="txAdvice"/>
</aop:config>
<!--<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">-->
<!--<property name="transactionManager" ref="transactionManager"/>-->
<!--</bean>-->
<bean id="accountDao" class="com.lq.play.daoimpl.AccountDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="accountService" class="com.lq.play.serviceimpl.AccountServiceImpl">
<property name="ad" ref="accountDao"/>
<!--<property name="tt" ref="transactionTemplate"/>-->
</bean>
<!--<!– (事务管理)transaction manager, use JtaTransactionManager for global tx –>-->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--<tx:annotation-driven transaction-manager="transactionManager"/>-->
</beans>
dao层不变service如下
package com.lq.play.serviceimpl;
import com.lq.play.dao.AccountDao;
import com.lq.play.service.AccountService;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
//@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true)
public class AccountServiceImpl implements AccountService {
private AccountDao ad ;
public void transfer(final Integer from,final Integer to,final Double money) {
//减钱
ad.minusMoney(from, money);
int i = 1/0;
//加钱
ad.addMoney(to, money);
}
public void setAd(AccountDao ad) {
this.ad = ad;
}
}
执行
package test;
import javax.annotation.Resource;
import com.lq.play.service.AccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:config/mybatis/spring-mybatis.xml"})
public class Demo {
@Resource(name="accountService")
private AccountService as;
@Test
public void fun1(){
as.transfer(1, 2, 100d);
}
}
执行前后结果都为
方式三:注解式
@Transactional可指定如下几个属性:
- isolation 事务隔离级别
- noRollbackFor 遇到指定异常强制不会滚
- noRollbackForClassName 指定遇到特定的多个异常时强制不回滚事务。可以指定多个异常类名。
- propagation 指定事务传播行为
- readOnly 指定事务是否只读
- rollbackFor 指定遇到特定异常时强制回滚事务
- rollbackForClassName 指定遇到特定的多个异常时强制回滚事务。该属性值可以指定多个异常类名。
- timeout 指定事务的超时时长。
配置spring-mybatis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 自动扫描 -->
<context:component-scan base-package="com.lq.play">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/>
</context:component-scan>
<!--和schema一起启动@Aspectj支持-->
<!--<aop:aspectj-autoproxy/>-->
<!--启动@Aspectj支持-->
<!--<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"/>-->
<!-- 引入配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"/>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<!-- 初始化连接大小 -->
<property name="initialSize" value="${initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${maxActive}"></property>
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="${maxIdle}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${maxWait}"></property>
</bean>
<!--<!– spring和MyBatis完美整合,不需要mybatis的配置映射文件 –>-->
<!--<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">-->
<!--<property name="dataSource" ref="dataSource" />-->
<!--<!–<property name="configLocation" value="classpath:mybatis-config.xml"></property>–>-->
<!--<!– 自动扫描mapping.xml文件 –>-->
<!--<property name="mapperLocations" value="classpath:mapper/*.xml"></property>-->
<!--</bean>-->
<!--<!– DAO接口所在包名,Spring会自动查找其下的类,扫描所有dao –>-->
<!--<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">-->
<!--<property name="basePackage" value="com.lq.play.mapper" />-->
<!--<property name="annotationClass" value="org.springframework.stereotype.Repository" />-->
<!--<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>-->
<!--</bean>-->
<!--<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">-->
<!--<property name="dataSource" ref="dataSource" />-->
<!--</bean>-->
<!-- 配置事务通知 -->
<!--<tx:advice transaction-manager="transactionManager" id="txAdvice">-->
<!--<tx:attributes>-->
<!--<!– 以方法为单位,指定方法应用什么事务属性 isolation:隔离级别 propagation:传播行为 read-only:是否只读 –>-->
<!--<tx:method name="save*" read-only="false" propagation="REQUIRED" isolation="REPEATABLE_READ"/>-->
<!--<tx:method name="persist*" read-only="false" propagation="REQUIRED" isolation="REPEATABLE_READ"/>-->
<!--<tx:method name="update*" read-only="false" propagation="REQUIRED" isolation="REPEATABLE_READ"/>-->
<!--<tx:method name="modify*" read-only="false" propagation="REQUIRED" isolation="REPEATABLE_READ"/>-->
<!--<tx:method name="delete*" read-only="false" propagation="REQUIRED" isolation="REPEATABLE_READ"/>-->
<!--<tx:method name="remove*" read-only="false" propagation="REQUIRED" isolation="REPEATABLE_READ"/>-->
<!--<tx:method name="get*" read-only="true" propagation="REQUIRED" isolation="REPEATABLE_READ"/>-->
<!--<tx:method name="find*" read-only="true" propagation="REQUIRED" isolation="REPEATABLE_READ"/>-->
<!--<tx:method name="transfer" read-only="false" propagation="REQUIRED" isolation="REPEATABLE_READ"/>-->
<!--</tx:attributes>-->
<!--</tx:advice>-->
<!--<!– 配置织入 –>-->
<!--<aop:config>-->
<!--<!– 配置切点表达式 –>-->
<!--<aop:pointcut id="txPc" expression="execution(* com.lq.play.serviceimpl.*.*(..))"/>-->
<!--<!– 配置切面 : 通知+切点 advice-ref:通知的名称 pointcut-ref:切点的名称 –>-->
<!--<aop:advisor pointcut-ref="txPc" advice-ref="txAdvice"/>-->
<!--</aop:config>-->
<!--<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">-->
<!--<property name="transactionManager" ref="transactionManager"/>-->
<!--</bean>-->
<!--<bean id="accountDao" class="com.lq.play.daoimpl.AccountDaoImpl">-->
<!--<property name="dataSource" ref="dataSource"/>-->
<!--</bean>-->
<!--<bean id="accountService" class="com.lq.play.serviceimpl.AccountServiceImpl">-->
<!--<property name="ad" ref="accountDao"/>-->
<!--<!–<property name="tt" ref="transactionTemplate"/>–>-->
<!--</bean>-->
<!--<!– (事务管理)transaction manager, use JtaTransactionManager for global tx –>-->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
service
public class AccountServiceImpl implements AccountService {
private AccountDao ad ;
// private TransactionTemplate tt;
@Override
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false)
public void transfer(final Integer from,final Integer to,final Double money) {
//减钱
ad.minusMoney(from, money);
int i = 1/0;
//加钱
ad.addMoney(to, money);
}
执行前后结果都为