Spring事務(wù)策略是通過PlatformTransactionManager接口提現(xiàn)的
public interface PlatformTransactionManager {
//獲取平臺(tái)無關(guān)的事務(wù)
TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException;
//平臺(tái)無關(guān)的事務(wù)提交
void commit(TransactionStatus status) throws TransactionException;
//平臺(tái)無關(guān)的事務(wù)回滾
void rollback(TransactionStatus status) throws TransactionException;
}
TransactionDefinition 接口定義了一個(gè)事務(wù)的規(guī)則兔朦,有如下幾個(gè)屬性:
- 事務(wù)隔離 當(dāng)前事務(wù)和其它事務(wù)的隔離程度
- 事務(wù)傳播 一個(gè)事務(wù)上下文已經(jīng)存在螟蝙,有幾個(gè)事務(wù)選項(xiàng)可指定該事務(wù)性方法的執(zhí)行行為。
- 事務(wù)超時(shí) 事務(wù)的最長持續(xù)時(shí)間,超時(shí)回滾
- 只讀狀態(tài) 不能修改任何數(shù)據(jù)
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代表事務(wù)本身
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">
<!-- 自動(dòng)掃描 -->
<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一起啟動(dòng)@Aspectj支持-->
<!--<aop:aspectj-autoproxy/>-->
<!--啟動(dòng)@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>
<!-- 連接池最大數(shù)量 -->
<property name="maxActive" value="${maxActive}"></property>
<!-- 連接池最大空閑 -->
<property name="maxIdle" value="${maxIdle}"></property>
<!-- 連接池最小空閑 -->
<property name="minIdle" value="${minIdle}"></property>
<!-- 獲取連接最大等待時(shí)間 -->
<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>–>-->
<!--<!– 自動(dòng)掃描mapping.xml文件 –>-->
<!--<property name="mapperLocations" value="classpath:mapper/*.xml"></property>-->
<!--</bean>-->
<!--<!– DAO接口所在包名照弥,Spring會(huì)自動(dòng)查找其下的類言津,掃描所有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>
<!--<!– (事務(wù)管理)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>
數(shù)據(jù)庫定義
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);
}
}
結(jié)果:執(zhí)行前數(shù)據(jù)庫
執(zhí)行后數(shù)據(jù)庫
沒有事務(wù)控制,出現(xiàn)異常的情況下取试,由于沒有回滾悬槽,只做了減錢沒有加錢,出現(xiàn)了前后不一致的問題瞬浓。
使用TemplateTranscation,修改執(zhí)行方法
@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);
}
可以看到執(zhí)行前后都為:做了事務(wù)回滾操作
修改執(zhí)行方法初婆,去掉異常,正常執(zhí)行
@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);
}
執(zhí)行后
方式二:聲明式配置
配置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">
<!-- 自動(dòng)掃描 -->
<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一起啟動(dòng)@Aspectj支持-->
<!--<aop:aspectj-autoproxy/>-->
<!--啟動(dòng)@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>
<!-- 連接池最大數(shù)量 -->
<property name="maxActive" value="${maxActive}"></property>
<!-- 連接池最大空閑 -->
<property name="maxIdle" value="${maxIdle}"></property>
<!-- 連接池最小空閑 -->
<property name="minIdle" value="${minIdle}"></property>
<!-- 獲取連接最大等待時(shí)間 -->
<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>–>-->
<!--<!– 自動(dòng)掃描mapping.xml文件 –>-->
<!--<property name="mapperLocations" value="classpath:mapper/*.xml"></property>-->
<!--</bean>-->
<!--<!– DAO接口所在包名磅叛,Spring會(huì)自動(dòng)查找其下的類,掃描所有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>-->
<!-- 配置事務(wù)通知 -->
<tx:advice transaction-manager="transactionManager" id="txAdvice">
<tx:attributes>
<!-- 以方法為單位,指定方法應(yīng)用什么事務(wù)屬性 isolation:隔離級(jí)別 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>
<!-- 配置切點(diǎn)表達(dá)式 -->
<aop:pointcut id="txPc" expression="execution(* com.lq.play.serviceimpl.*.*(..))"/>
<!-- 配置切面 : 通知+切點(diǎn) advice-ref:通知的名稱 pointcut-ref:切點(diǎn)的名稱 -->
<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>
<!--<!– (事務(wù)管理)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;
}
}
執(zhí)行
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);
}
}
執(zhí)行前后結(jié)果都為
方式三:注解式
@Transactional可指定如下幾個(gè)屬性:
- isolation 事務(wù)隔離級(jí)別
- noRollbackFor 遇到指定異常強(qiáng)制不會(huì)滾
- noRollbackForClassName 指定遇到特定的多個(gè)異常時(shí)強(qiáng)制不回滾事務(wù)萨赁”浊伲可以指定多個(gè)異常類名。
- propagation 指定事務(wù)傳播行為
- readOnly 指定事務(wù)是否只讀
- rollbackFor 指定遇到特定異常時(shí)強(qiáng)制回滾事務(wù)
- rollbackForClassName 指定遇到特定的多個(gè)異常時(shí)強(qiáng)制回滾事務(wù)位迂。該屬性值可以指定多個(gè)異常類名访雪。
- timeout 指定事務(wù)的超時(shí)時(shí)長。
配置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">
<!-- 自動(dòng)掃描 -->
<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一起啟動(dòng)@Aspectj支持-->
<!--<aop:aspectj-autoproxy/>-->
<!--啟動(dòng)@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>
<!-- 連接池最大數(shù)量 -->
<property name="maxActive" value="${maxActive}"></property>
<!-- 連接池最大空閑 -->
<property name="maxIdle" value="${maxIdle}"></property>
<!-- 連接池最小空閑 -->
<property name="minIdle" value="${minIdle}"></property>
<!-- 獲取連接最大等待時(shí)間 -->
<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>–>-->
<!--<!– 自動(dòng)掃描mapping.xml文件 –>-->
<!--<property name="mapperLocations" value="classpath:mapper/*.xml"></property>-->
<!--</bean>-->
<!--<!– DAO接口所在包名臣缀,Spring會(huì)自動(dòng)查找其下的類,掃描所有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>-->
<!-- 配置事務(wù)通知 -->
<!--<tx:advice transaction-manager="transactionManager" id="txAdvice">-->
<!--<tx:attributes>-->
<!--<!– 以方法為單位,指定方法應(yīng)用什么事務(wù)屬性 isolation:隔離級(jí)別 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>-->
<!--<!– 配置切點(diǎn)表達(dá)式 –>-->
<!--<aop:pointcut id="txPc" expression="execution(* com.lq.play.serviceimpl.*.*(..))"/>-->
<!--<!– 配置切面 : 通知+切點(diǎn) advice-ref:通知的名稱 pointcut-ref:切點(diǎn)的名稱 –>-->
<!--<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>-->
<!--<!– (事務(wù)管理)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);
}
執(zhí)行前后結(jié)果都為