spring整合hibernate事務管理器配置
- 首先在/WEB-INF/applicationContext.xml新增以下內容:
<!-- 配置事務管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="mySessionFactory"/>
</property>
</bean>
注:這是作為公共使用的事務管理器Bean。這個會是事先配置好的,不需各個模組各自去配。
- 下面就開始配置各個模組所必須的部分,在各自的applicationContext-XXX-beans.xml配置的對於事務管理的詳細資訊。
首先就是配置事務的傳播特性,如下:
<!-- 配置事務傳播特性 -->
<tx:advice id="TestAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation ="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="REQUIRED"/>
<tx:method name="get*" propagation ="REQUIRED"/>
<tx:method name="apply*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 配置參與事務的類 -->
<aop:config>
<aop:pointcut id="allTestServiceMethod" expression="execution(* com.test.testAda.test.model.service.*.*(..))"/>
<aop:advisor pointcut-ref="allTestServiceMethod" advice-ref="TestAdvice" />
</aop:config>
需要注意的地方:
(1) advice(建議)的命名:由於每個模組都會有自己的Advice,所以在命名上需要作出規範,初步的構想就是模組名+Advice(只是一種命名規範)。
(2) tx:attribute標籤所配置的是作為事務的方法的命名型別。
如<tx:method name="save*" propagation="REQUIRED"/>
其中*為萬用字元,即代表以save為開頭的所有方法,即表示符合此命名規則的方法作為一個事務。
propagation="REQUIRED"代表支援當前事務,如果當前沒有事務,就新建一個事務。這是最常見的選擇。
(3) aop:pointcut標籤配置參與事務的類,由於是在Service中進行資料庫業務操作,配的應該是包含那些作為事務的方法的Service類。
首先應該特別注意的是id的命名,同樣由於每個模組都有自己事務切面,所以我覺得初步的命名規則因為 all+模組名+ServiceMethod。而且每個模組之間不同之處還在於以下一句:
expression="execution(* com.test.testAda.test.model.service.*.*(..))"
其中第一個*代表返回值,第二*代表service下子包,第三個*代表方法名,“(..)”代表方法引數。
(4) aop:advisor標籤就是把上面我們所配置的事務管理兩部分屬性整合起來作為整個事務管理。
下文轉自http://blog.csdn.net/dw_java08/article/details/7682890
applicationContext-common.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: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-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!-- 配置事務管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<!-- 配置事務的傳播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="modify*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 那些類的哪些方法參與事務 -->
<aop:config>
<aop:pointcut id="allManagerMethod" expression="execution(* com.z2sci.soa.manager.*.*(..))"/>
<aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>
</aop:config>
</beans>
spring使用
<tx:advice>和 <aop:config> 用來配置事務,具體如何配置你可以參考Spring文件。
我解釋一下(* com.z2sci.soa.manager.*.*(..))中幾個萬用字元的含義:
|第一個 * —— 通配 任意返回值型別|
|第二個 * —— 通配 包com.z2sci.soa.manager下的任意class|
|第三個 * —— 通配 包com.z2sci.soa.manager下的任意class的任意方法|
|第四個 .. —— 通配 方法可以有0個或多個引數|
綜上:包com.z2sci.soa.manager下的任意class的具有任意返回值型別、任意數目引數和任意名稱的方法
<tx:advice/> 有關的設定
這一節裡將描述通過 <tx:advice/> 標籤來指定不同的事務性設定。預設的 <tx:advice/> 設定如下:
事務傳播設定是 REQUIRED
隔離級別是 DEFAULT
事務是 讀/寫
事務超時預設是依賴於事務系統的,或者事務超時沒有被支援。
任何 RuntimeException 將觸發事務回滾,但是任何 checked Exception 將不觸發事務回滾
這些預設的設定當然也是可以被改變的。 <tx:advice/> 和 <tx:attributes/> 標籤裡的 <tx:method/> 各種屬性設定總結如下:
表 9.1. <tx:method/> 有關的設定
屬性 | 是否需要? | 預設值 | 描述 |
name | 是 | 與事務屬性關聯的方法名。萬用字元(*)可以用來指定一批關聯到相同的事務屬性的方法。 如:'get*'、'handle*'、'on*Event'等等。 | |
propagation | 不 | REQUIRED | 事務傳播行為 |
isolation | 不 | DEFAULT | 事務隔離級別 |
timeout | 不 | -1 | 事務超時的時間(以秒為單位) |
read-only | 不 | false | 事務是否只讀? |
rollback-for | 不 | 將被觸發進行回滾的 Exception(s);以逗號分開。 如:'com.foo.MyBusinessException,ServletException' | |
no-rollback-for | 不 | 不 被觸發進行回滾的 Exception(s);以逗號分開。 如:'com.foo.MyBusinessException |