Spring事務SPI及配置介紹,spring事務spi

來源:互聯網
上載者:User

Spring事務SPI及配置介紹,spring事務spi

Spring交易管理的抽象,核心的三個介面:PlatformTransactionManager、TransactionDefinition和TransactionStatus。關係如所示:


TransactionDefinition:定義了Spring相容的事務屬性,包含:交易隔離等級、事務傳播行為、逾時時間長度、唯讀狀態;

TransactionStatus:代表一個事務的具體運行狀態。交易管理員通過該介面得到事務啟動並執行狀態資訊,也可以通過它復原事務等;

PlatformTransactionManager:交易管理員的頂級介面,它的常用實作類別如所示:


Spring設定檔中關於事務配置總是由三個組成部分,分別是DataSource、TransactionManager和代理機制這三部分,無論哪種配置方式,一般變化的只是代理機制這部分。

DataSource、TransactionManager這兩部分只是會根據資料訪問方式有所變化,比如使用Hibernate進行資料訪問時,DataSource實際為SessionFactory,TransactionManager的實現為HibernateTransactionManager。


根據代理機制的不同,總結了五種Spring事務的配置方式,設定檔如下:

第一種方式:每個Bean都有一個代理

<?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:aop="http://www.springframework.org/schema/aop"    xsi:schemaLocation="http://www.springframework.org/schema/beans            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd           http://www.springframework.org/schema/context           http://www.springframework.org/schema/context/spring-context-2.5.xsd           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">    <bean id="sessionFactory"              class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">          <property name="configLocation" value="classpath:hibernate.cfg.xml" />          <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />    </bean>      <!-- 定義交易管理員(聲明式的事務) -->      <bean id="transactionManager"        class="org.springframework.orm.hibernate3.HibernateTransactionManager">        <property name="sessionFactory" ref="sessionFactory" />    </bean>        <!-- 配置DAO -->    <bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl">        <property name="sessionFactory" ref="sessionFactory" />    </bean>        <bean id="userDao"          class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">             <!-- 配置交易管理員 -->             <property name="transactionManager" ref="transactionManager" />             <property name="target" ref="userDaoTarget" />           <property name="proxyInterfaces" value="com.bluesky.spring.dao.GeneratorDao" />        <!-- 配置事務屬性 -->          <property name="transactionAttributes">              <props>                  <prop key="*">PROPAGATION_REQUIRED</prop>            </props>          </property>      </bean>  </beans>

第二種方式:所有Bean共用一個代理基類

<?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:aop="http://www.springframework.org/schema/aop"    xsi:schemaLocation="http://www.springframework.org/schema/beans            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd           http://www.springframework.org/schema/context           http://www.springframework.org/schema/context/spring-context-2.5.xsd           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">    <bean id="sessionFactory"              class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">          <property name="configLocation" value="classpath:hibernate.cfg.xml" />          <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />    </bean>      <!-- 定義交易管理員(聲明式的事務) -->      <bean id="transactionManager"        class="org.springframework.orm.hibernate3.HibernateTransactionManager">        <property name="sessionFactory" ref="sessionFactory" />    </bean>        <bean id="transactionBase"              class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"              lazy-init="true" abstract="true">          <!-- 配置交易管理員 -->          <property name="transactionManager" ref="transactionManager" />          <!-- 配置事務屬性 -->          <property name="transactionAttributes">              <props>                  <prop key="*">PROPAGATION_REQUIRED</prop>              </props>          </property>      </bean>           <!-- 配置DAO -->    <bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl">        <property name="sessionFactory" ref="sessionFactory" />    </bean>        <bean id="userDao" parent="transactionBase" >          <property name="target" ref="userDaoTarget" />       </bean></beans>

第三種方式:使用攔截器

<?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:aop="http://www.springframework.org/schema/aop"    xsi:schemaLocation="http://www.springframework.org/schema/beans            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd           http://www.springframework.org/schema/context           http://www.springframework.org/schema/context/spring-context-2.5.xsd           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">    <bean id="sessionFactory"              class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">          <property name="configLocation" value="classpath:hibernate.cfg.xml" />          <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />    </bean>      <!-- 定義交易管理員(聲明式的事務) -->      <bean id="transactionManager"        class="org.springframework.orm.hibernate3.HibernateTransactionManager">        <property name="sessionFactory" ref="sessionFactory" />    </bean>        <bean id="transactionInterceptor"          class="org.springframework.transaction.interceptor.TransactionInterceptor">          <property name="transactionManager" ref="transactionManager" />          <!-- 配置事務屬性 -->          <property name="transactionAttributes">              <props>                  <prop key="*">PROPAGATION_REQUIRED</prop>              </props>          </property>      </bean>          <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">          <property name="beanNames">              <list>                  <value>*Dao</value>            </list>          </property>          <property name="interceptorNames">              <list>                  <value>transactionInterceptor</value>              </list>          </property>      </bean>        <!-- 配置DAO -->    <bean id="userDao" class="com.bluesky.spring.dao.UserDaoImpl">        <property name="sessionFactory" ref="sessionFactory" />    </bean></beans>

第四種方式:使用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: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.5.xsd           http://www.springframework.org/schema/context           http://www.springframework.org/schema/context/spring-context-2.5.xsd           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">    <context:annotation-config />    <context:component-scan base-package="com.bluesky" />    <bean id="sessionFactory"              class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">          <property name="configLocation" value="classpath:hibernate.cfg.xml" />          <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />    </bean>      <!-- 定義交易管理員(聲明式的事務) -->      <bean id="transactionManager"        class="org.springframework.orm.hibernate3.HibernateTransactionManager">        <property name="sessionFactory" ref="sessionFactory" />    </bean>    <tx:advice id="txAdvice" transaction-manager="transactionManager">        <tx:attributes>            <tx:method name="*" propagation="REQUIRED" />        </tx:attributes>    </tx:advice>        <aop:config>        <aop:pointcut id="interceptorPointCuts"            expression="execution(* com.bluesky.spring.dao.*.*(..))" />        <aop:advisor advice-ref="txAdvice"            pointcut-ref="interceptorPointCuts" />            </aop:config>      </beans>

<span style="font-size:18px;color:#cc33cc;"><strong>第五種方式:全註解</strong></span>


<?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: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.5.xsd           http://www.springframework.org/schema/context           http://www.springframework.org/schema/context/spring-context-2.5.xsd           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">    <context:annotation-config />    <context:component-scan base-package="com.bluesky" />    <tx:annotation-driven transaction-manager="transactionManager"/>    <bean id="sessionFactory"              class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">          <property name="configLocation" value="classpath:hibernate.cfg.xml" />          <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />    </bean>      <!-- 定義交易管理員(聲明式的事務) -->      <bean id="transactionManager"        class="org.springframework.orm.hibernate3.HibernateTransactionManager">        <property name="sessionFactory" ref="sessionFactory" />    </bean>    </beans>

此時在DAO上需加上@Transactional註解,如下:

@Transactional@Component("userDao")public class UserDaoImpl extends HibernateDaoSupport implements UserDao {    public List<User> listUsers() {        return this.getSession().createQuery("from User").list();    }        }


配置參考:http://www.blogjava.net/robbie/archive/2009/04/05/264003.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.