spring 事物通過<tx 標籤配置

配置文件如下:

<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<context:annotation-config />

<!--aop support-->
<aop:aspectj-autoproxy/>

<!--hibernate、數據庫配置-->
<import resource="application-datasource.xml"/>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:config/hibernate.cfg.xml"/>
<property name="namingStrategy">
<bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
</property>
</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<!-- hibernate配置結束 -->

<!-- 聲明性事務配置 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>

<tx:advice id="baseServiceAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="getDao" propagation="NOT_SUPPORTED"/>
<tx:method name="getJdbcTemplate" propagation="NOT_SUPPORTED"/>
<tx:method name="getHDao" propagation="NOT_SUPPORTED"/>
<tx:method name="getHibernateDao" propagation="NOT_SUPPORTED"/>
<tx:method name="getHibernateTemplate" propagation="NOT_SUPPORTED"/>
<tx:method name="getModelClass" propagation="NOT_SUPPORTED"/>
<tx:method name="get*" read-only="true" propagation="REQUIRED"/>
<tx:method name="find*" read-only="true" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="remove*" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED"/>

<tx:method name="*"/>
</tx:attributes>
</tx:advice>

<aop:config>
<aop:pointcut id="baseServiceTxOperation" expression="execution(* com.hisun.mvc.service..*.*(..))"/>
<aop:advisor pointcut-ref="baseServiceTxOperation" advice-ref="baseServiceAdvice"/>
</aop:config>

<!-- 聲明性事務配置結束 -->

<!-- 使用annotation 掃描機制註冊bean -->
<context:component-scan base-package="com.hisun.**">
<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation" />
</context:component-scan>

</beans>



Spring使用 <tx:advice>和 <aop:config> 用來配置事務,具體如何配置你可以參考Spring文檔。

我解釋一下execution(* com.hisun.mvc.service..*.*(..))"中幾個通配符的含義:

第一個 * —— 通配 任意返回值類型
第二個 * —— 通配 包com.hisun.mvc.service下的任意class
第三個 * —— 通配 包com.hisun.mvc.service下的任意class的任意方法
第四個 .. —— 通配 方法可以有0個或多個參數

綜上:包com.hisun.mvc.service下的任意class的具有任意返回值類型、任意數目參數和任意名稱的方法

 

<tx:advice/> 有關的設置

這一節裏將描述通過 <tx:advice/> 標籤來指定不同的事務性設置。默認的 <tx:advice/> 設置如下:

 

  • 事務傳播設置是 REQUIRED

  • 隔離級別是 DEFAULT

  • 事務是 讀/寫

  • 事務超時默認是依賴於事務系統的,或者事務超時沒有被支持。

  • 任何 RuntimeException 將觸發事務回滾,但是任何 checked Exception 將不觸發事務回滾

 

這些默認的設置當然也是可以被改變的。 <tx:advice/><tx:attributes/> 標籤裏的 <tx:method/> 各種屬性設置總結如下:

name與事務屬性關聯的方法名。通配符(*)可以用來指定一批關聯到相同的事務屬性的方法。 如:'get*''handle*''on*Event'等等。
propagationREQUIRED事務傳播行爲
isolationDEFAULT事務隔離級別
timeout-1事務超時的時間(以秒爲單位)
read-onlyfalse事務是否只讀?
rollback-for將被觸發進行回滾的 Exception(s);以逗號分開。 如:'com.foo.MyBusinessException,ServletException'
被觸發進行回滾的 Exception(s);以逗號分開。 如:'com.foo.MyBusinessException

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章