Spring20简明手册系列之五 事务管理.docx
《Spring20简明手册系列之五 事务管理.docx》由会员分享,可在线阅读,更多相关《Spring20简明手册系列之五 事务管理.docx(16页珍藏版)》请在冰点文库上搜索。
![Spring20简明手册系列之五 事务管理.docx](https://file1.bingdoc.com/fileroot1/2023-5/4/b93eccd1-22d2-404a-b5c0-b9888e67098d/b93eccd1-22d2-404a-b5c0-b9888e67098d1.gif)
Spring20简明手册系列之五事务管理
Spring2.0简明手册(系列之五事务管理)
2008-07-1621:
26:
45
标签:
Spring简明手册
原创作品,允许转载,转载时请务必以超链接形式标明文章原始出处、作者信息和本声明。
否则将追究法律责任。
1.声明式事务管理
Spring2.0及以后的版本中声明式事务的配置与之前的版本有相当大的不同。
主要差异在于不再需要配置TransactionProxyFactoryBean了。
1.1基于XMLSchema
//我们想做成事务性的服务接口
packagex.y.service;
publicinterfaceFooService{
FoogetFoo(StringfooName);
FoogetFoo(StringfooName,StringbarName);
voidinsertFoo(Foofoo);
voidupdateFoo(Foofoo);
}
//上述接口的一个实现
packagex.y.service;
publicclassDefaultFooServiceimplementsFooService{
publicFoogetFoo(StringfooName){
thrownewUnsupportedOperationException();
}
publicFoogetFoo(StringfooName,StringbarName){
thrownewUnsupportedOperationException();
}
publicvoidinsertFoo(Foofoo){
thrownewUnsupportedOperationException();
}
publicvoidupdateFoo(Foofoo){
thrownewUnsupportedOperationException();
}
}
xmlversion="1.0"encoding="UTF-8"?
>
//www.springframework.org/schema/beans"
xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
xmlns:
aop="http:
//www.springframework.org/schema/aop"
xmlns:
tx="http:
//www.springframework.org/schema/tx"
xsi:
schemaLocation="
[url]http:
//www.springframework.org/schema/beans[/url][url]http:
//www.springframework.org/schema/beans/spring-beans-2.0.xsd[/url]
[url]http:
//www.springframework.org/schema/tx[/url][url]http:
//www.springframework.org/schema/tx/spring-tx-2.0.xsd[/url]
[url]http:
//www.springframework.org/schema/aop[/url][url]http:
//www.springframework.org/schema/aop/spring-aop-2.0.xsd[/url]">
--===================================-->
-- 加载属性文件 -->
--===================================-->
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
classpath:
jdbc-config.properties
--===================================-->
-- 配置数据源 -->
--===================================-->
--===================================-->
-- 配置事务 -->
--===================================-->
config>
pointcutid="defaultServiceOperation"expression="execution(*x.y.service.*Service.*(..))"/>
advisorpointcut-ref="defaultServiceOperation"advice-ref="defaultTxAdvice"/>
pointcutid="noTxServiceOperation"expression="execution(*x.y.service.ddl.DefaultDdlManager.*(..))"/>
advisorpointcut-ref="noTxServiceOperation"advice-ref="noTxAdvice"/>
config>
adviceid="defaultTxAdvice"transaction-manager="txManager">
attributes>
methodname="get*"read-only="true"/>
methodname="*"/>
attributes>
advice>
adviceid="noTxAdvice"transaction-manager="txManager">
attributes>
methodname="*"propagation="NEVER"/>
attributes>
advice>
--===================================-->
-- 配置业务Bean -->
--===================================-->
--
thisbeanwillbetransactional(c.f.the'defaultServiceOperation'pointcut)
-->
--thisbeanwillalsobetransactional,
butwithtotallydifferenttransactionalsettings-->
更进一步:
参照Spring2.0参考手册--9.5.5.advice/>有关的设置
1.2使用@Transactional
//theserviceclassthatwewanttomaketransactional
@Transactional(readOnly=true)
publicclassDefaultFooServiceimplementsFooService{
publicFoogetFoo(StringfooName){
//dosomething
}
//thesesettingshaveprecedenceforthismethod
@Transactional(readOnly=false,propagation=Propagation.REQUIRES_NEW)
publicvoidupdateFoo(Foofoo){
//dosomething
}
}
xmlversion="1.0"encoding="UTF-8"?
>
//www.springframework.org/schema/beans"
xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
xmlns:
tx="http:
//www.springframework.org/schema/tx"
xsi:
schemaLocation="
[url]http:
//www.springframework.org/schema/beans[/url][url]http:
//www.springframework.org/schema/beans/spring-beans-2.0.xsd[/url]
[url]http:
//www.springframework.org/schema/tx[/url][url]http:
//www.springframework.org/schema/tx/spring-tx-2.0.xsd[/url]">
--===================================-->
-- 加载属性文件 -->
--===================================-->
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
classpath:
jdbc-config.properties
--===================================-->
-- 配置数据源 -->
--===================================-->
--===================================-->
-- 配置事务 -->
--===================================-->
--enabletheconfigurationoftransactionalbehaviorbasedonannotations-->
annotation-driventransaction-manager="txManager"/>
--===================================-->
-- 配置业务Bean -->
--===================================-->
--thisistheserviceobjectthatwewanttomaketransactional-->
更进一步:
参照Spring2.0参考手册--9.5.6.1.@Transactional有关的设置
1.3插入事务操作
1.3.1使用@Transactional
--theserviceclassthatwewanttomaketransactional-->
@Transactional(readOnly=true)
publicclassDefaultFooServiceimplementsFooService{
publicFoogetFoo(StringfooName){
//dosomething
}
//thesesettingshaveprecedenceforthismethod
@Transactional(readOnly=false,propagation=Propagation.REQUIRES_NEW)
publicvoidupdateFoo(Foofoo){
//dosomething
}
}
packagex.y;
importorg.aspectj.lang.ProceedingJoinPoint;
importorg.springframework.util.StopWatch;
importorg.springframework.core.Ordered;
publicclassSimpleProfilerimplementsOrdered{
privateintorder;
//allowsustocontroltheorderingofadvice
publicintgetOrder(){
returnthis.order;
}
publicvoidsetOrder(intorder){
this.order=order;
}
//thismethodisthearoundadvice
publicObjectprofile(ProceedingJoinPointcall)throwsThrowable{
ObjectreturnValue;
StopWatchclock=newStopWatch(getClass().getName());
try{
clock.start(call.toShortString());
returnValue=call.proceed();
}finally{
clock.stop();
System.out.println(clock.prettyPrint());
}
returnreturnValue;
}
}
xmlversion="1.0"encoding="UTF-8"?
>
//www.springframework.org/schema/beans"
xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
xmlns:
aop="http:
//www.springframework.org/schema/aop"
xmlns:
tx="http:
//www.springframework.org/schema/tx"
xsi:
schemaLocation="
[url]http:
//www.springframework.org/schema/beans[/url][url]http:
//www.springframework.org/schema/beans/spring-beans-2.0.xsd[/url]
[url]http:
//www.springframework.org/schema/tx[/url][url]http:
//www.springframework.org/schema/tx/spring-tx-2.0.xsd[/url]
[url]http:
//www.springframework.org/schema/aop[/url][url]http:
//www.springframework.org/schema/aop/spring-aop-2.0.xsd[/url]">
--thi