Spring聲明性事務(wù)常見(jiàn)問(wèn)題分析
聲明性事務(wù)是spring一個(gè)很重要的功能,可以避免開(kāi)發(fā)陷入繁瑣的事務(wù)控制邏輯中。但是可能是用著太方便了很多人對(duì)spring事務(wù)原理并不清楚,有必要做一番分析。
下邊以攔截器配置方式進(jìn)行說(shuō)明,tx標(biāo)簽配置方式將在接下來(lái)另一篇文章做分析。
一、首先看配置文件:
- <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- <bean id="matchAllTxInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
- <property name="transactionManager">
- <ref bean="transactionManager" />
- </property>
- <property name="transactionAttributes">
- <props>
- <prop key="get*">PROPAGATION_REQUIRED,readOnly,-Exception </prop>
- <prop key="find*">PROPAGATION_REQUIRED,readOnly,-Exception </prop>
- <prop key="search*">PROPAGATION_REQUIRED,readOnly,-Exception </prop>
- <prop key="save*">PROPAGATION_REQUIRED,-Exception </prop>
- <prop key="modify*">PROPAGATION_REQUIRED,-Exception </prop>
- <prop key="send*">PROPAGATION_REQUIRED,-Exception </prop>
- <prop key="revoke*">PROPAGATION_REQUIRED,-Exception </prop>
- <prop key="del*">PROPAGATION_REQUIRED,-Exception </prop>
- <prop key="logging*">PROPAGATION_NOT_SUPPORTED,readOnly,-Exception </prop>
- <prop key="*">PROPAGATION_SUPPORTS,-Exception </prop>
- </props>
- </property>
- </bean>
- <bean id="autoProxyCreator"
- class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
- <property name="interceptorNames">
- <list><idref local="matchAllTxInterceptor" /></list>
- </property>
- <property name="proxyTargetClass"><value>true</value></property>
- <property name="beanNames">
- <list><value>*Service</value></list>
- </property>
- </bean>
配置***步引入AOP代理autoProxyCreator,使用的是spring默認(rèn)的jdk動(dòng)態(tài)代理BeanNameAutoProxyCreator。
有兩個(gè)屬性要介紹一下:
1、攔截范圍beanNames
例子中攔截范圍是*Service,表示IOC容器中以Service結(jié)尾的bean,一般配置在spring.xml,serviceContext.xml之類(lèi)的spring配置文件。
要注意這里不是值src下邊的類(lèi)。
bean配置信息:
- <bean id="menuService" class="cn.ceopen.bss..service.impl.MenuServiceImpl"/>
有圖有真相,下邊是BeanNameAutoProxyCreator 調(diào)試信息。
2、截器interceptorNames
interceptorNames定義事務(wù)屬性和事務(wù)管理器。
配置第二步就是定義事務(wù)屬性:事務(wù)傳播范圍、事務(wù)隔離級(jí)別。
事務(wù)屬性沒(méi)什么好說(shuō)的,使用spring進(jìn)行事務(wù)管理的都了解,不在這里詳細(xì)說(shuō)了網(wǎng)上有大量資料。
置第三步,指定事務(wù)管理器。
這里用的是HibernateTransactionManager,spring提供對(duì)常見(jiàn)orm的事務(wù)支持。從spring源碼可以看出HibernateTransactionManager.doGetTransaction()同時(shí)支持hibernate和jdbc。支持hibernate和jdbc混合事務(wù),不使用jta方式的話(huà)有個(gè)前提條件:使用同一個(gè)數(shù)據(jù)源,這里所說(shuō)的同一個(gè)數(shù)據(jù)源,不僅僅指物理上是同一個(gè),在spring配置文件中也要是同一個(gè)。我在開(kāi)發(fā)中遇到過(guò)這個(gè)問(wèn)題,最早定義了一個(gè)數(shù)據(jù)baseDataSource,hibernate和jdbc都使用此數(shù)據(jù)源,后來(lái)項(xiàng)目要求使用動(dòng)態(tài)數(shù)據(jù)源就又配了一個(gè)數(shù)據(jù)源dynamicDataSource僅在hibernate下做了改動(dòng),未改動(dòng)jdbc對(duì)應(yīng)配置,出現(xiàn)了事務(wù)控制問(wèn)題。
出錯(cuò)了事務(wù)配置:
- <bean id="sessionFactory"
- class="com.sitechasia.webx.dao.hibernate3.NamedMoudleHbmLocalSessionFactoryBean">
- <property name="dataSource" ref="dynamicDataSource" />
- <!--與主題無(wú)關(guān),省略部分內(nèi)容-->
- </bean>
- <bean id="dynamicDataSource" class="cn.ceopen.bss.pub.base.dao.RoutingDataSource">
- <property name="targetDataSources">
- <map key-type="java.lang.String">
- <entry key="baseDataSource" value-ref="baseDataSource"/>
- </map>
- </property>
- <property name="defaultTargetDataSource" ref="baseDataSource"/>
- </bean>
- <bean id="baseDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
- <!--與主題無(wú)關(guān),省略部分內(nèi)容-->
- </bean>
- <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
- <!--dataSource應(yīng)該與sessionFactor一致-->
- <property name="dataSource"><ref bean="baseDataSource"/></property>
- </bean>
- <bean id="abstractJdbcDao" abstract="true">
- <property name="jdbc" ref="jdbcTemplate" />
- </bean>
dao配置文件:
- <bean id="actDao" class="cn.ceopen.bss.impl.ActDaoImpl" parent="abstractJdbcDao"/>
dao中同時(shí)支持hibernate操作和jdbc操作。
二、事務(wù)屬性傳播
先看這樣一個(gè)列子:
1、基于jdk動(dòng)態(tài)代理的AOP事務(wù)控制,只能針對(duì)接口。
在上邊的配置文件中設(shè)置的事務(wù)屬性對(duì)a3()都不起作用,a3()不能單獨(dú)設(shè)計(jì)事務(wù)屬性,只能繼承接口方法的事務(wù)屬性。
2、類(lèi)自身事務(wù)嵌套
***種情況:
- AbcIService abcService;
- BcdIService bcdService;
- abcService.a1();
- abcService.a2();
- bcdService.b1();
這三個(gè)方法對(duì)應(yīng)的事務(wù)屬性都起作用。
第二種情況:
方法定義
- public void a1() {
- bcdService.b1();
- }
調(diào)用:
- abcService.a1();
結(jié)果:
- abcService.a1();
- bcdService.b1();
這兩個(gè)方法對(duì)應(yīng)的事務(wù)屬性都起作用。
第三種情況:
方法定義
- public void a1() {
- this.a2();
- }
調(diào)用:
- abcService.a1();
結(jié)果:
- abcService.a1();
- abcService.a2();
a2()對(duì)應(yīng)的事務(wù)屬性配置不起作用。
解決辦法:
1)把a(bǔ)2()拆到另一個(gè)類(lèi)中去;
缺點(diǎn):麻煩,會(huì)把相關(guān)業(yè)務(wù)邏輯拆分了
2)調(diào)用是不用this.a2(),用abcService.a2();
- public void a1() {
- abcService.a2();
- }
缺點(diǎn):要在類(lèi)中注入自身引用。
原因分析:
為什么會(huì)出現(xiàn)這種情況呢?
我們?cè)谡{(diào)用abcService.a1();時(shí)abcService是從IOC容器獲取的,并AbcServiceImpl而是它的動(dòng)態(tài)代理AbcServiceProxy。
示意圖如下,spring不一定是這么實(shí)現(xiàn)的但原理一樣。
AbcServiceProxy.a()方法進(jìn)行了AOP增強(qiáng),根據(jù)配置文件中事務(wù)屬性增加了事務(wù)控制。
- public void a1() {
- this.a2();
- }
this.a2()這里this指的是AbcIServiceImpl并沒(méi)用進(jìn)行AOP增強(qiáng),所以沒(méi)用應(yīng)用事務(wù)屬性,只能繼承a1()的事務(wù)屬性。
- public void a1() {
- abcService.a2();
- }
abcService則實(shí)際是AbcServiceProxy.a2()所以可以應(yīng)用事務(wù)屬性。
所以在類(lèi)內(nèi)部進(jìn)行方法嵌套調(diào)用,如果被嵌套的方法a2()需要區(qū)別于嵌套方法a1()的事務(wù)屬性,需要:1)在接口公開(kāi);2)通過(guò)代理調(diào)用。
原文鏈接:http://www.blogjava.net/zyskm/archive/2011/11/11/363535.html
【編輯推薦】