淺析Spring提供的交易管理方法

來源:互聯網
上載者:User
Spring提供的交易管理可以分為兩類:編程式的和聲明式的。編程式的,比較靈活,但是代碼量大,存在重複的代碼比較多;而聲明式的比編程式的更靈活方便。本文將討論這兩種交易管理的區別。

  傳統的JDBC交易管理

  以往使用JDBC進行資料操作時,一般採用DataSource,從資料來源中得到Connection,我們知道資料來源是安全執行緒的,而串連不是安全執行緒的,所以對每個請求都是從資料來源中重新取出一個串連。一般的資料來源由容器進行管理,包括串連池。例如TOMCAT,WEBSPHERE,WEBLOGIC等這些J2EE商業容器都提供了這個功能。

  以往的我們使用JDBC在寫代碼時,交易管理可能會是這樣:

Connection conn = null;
try
{
 conn = DBConnectionFactory.getConnection;
 conn.setAutoCommit(false);
 //do something
 conn.commit(); //commit transcation
}
catch(Exception e)
{
 conn.rollback();
 //do sth
}
finally
{
 try
 {
  conn.close();
 }
 catch(SQLException se){ //do sth.}
 //close ResultSet,PreparedStatement,Connection
 //notice:Maybe ocurr Exception when u close rs,pstmt,conn
}
  按照以往的思路來寫代碼,代碼量比較長,而且容易疏忽,忘掉一些try/catch,引發一些異常無法catch,雖然有時候我們會寫DBTool類,來關閉這些資源,並且保證在關閉這些資源時,不向外拋異常。

  Spring提供的編程式的交易處理

  Spring提供了幾個關於交易處理的類:

  ·TransactionDefinition //事務屬性定義
  ·TranscationStatus //代表了當前的事務,可以提交,復原。
  ·PlatformTransactionManager這個是spring提供的用於管理事務的基礎介面,其下有一個實現的抽象類別AbstractPlatformTransactionManager,我們使用的交易管理類例如DataSourceTransactionManager等都是這個類的子類。

  我們使用編程式的交易管理流程可能如下:

  1 聲明資料來源

  2 聲明一個交易管理類,例如DataSourceTransactionManager,HibernateTransactionManger,JTATransactionManager等

  3 在我們的代碼中加入交易處理代碼:

TransactionDefinition td = new TransactionDefinition();
TransactionStatus ts = transactionManager.getTransaction(td);
try
{
 //do sth
 transactionManager.commit(ts);
}
catch(Exception e){transactionManager.rollback(ts);}
  使用spring提供的事務模板TransactionTemplate

void add()
{
 transactionTemplate.execute( new TransactionCallback(){
  pulic Object doInTransaction(TransactionStatus ts)
  { //do sth}
 }
}
  TransactionTemplate也是為我們省去了部分事務提交、復原代碼;定義事務模板時,需注入交易管理對象.

  Spring聲明式交易處理

  Spring聲明式交易處理也主要使用了ioc,aop思想,提供了TransactionInterceptor攔截器和常用的代理類TransactionProxyFactoryBean,可以直接對組件進行事務代理。

  使用TransactionInterceptor步驟

  1.定義資料來源,交易管理類

  2.定義事務攔截器,such as:

<bean id = "transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager"><ref bean="transactionManager"/></property>
<property name="transactionAttributeSource">
<value>
com.test.UserManager.*r=PROPAGATION_REQUIRED
</value>
</property>
</bean>
  3.為組件聲明一個代理類:ProxyFactoryBean

<bean id="userManager" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces"><value>com.test.UserManager</value></property>
<property name="interceptorNames">
<list>
<idref local="transactionInterceptor"/>
</list>
</property>
</bean>
  使用TransactionProxyFactoryBean:

<bean id="userManager" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager"><ref bean="transactionManager"/></property>
<property name="target"><ref local="userManagerTarget"/></property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
  TransactionProxyFactoryBean只是為組件的事務代理,如果我們要給組件添加一些業務方面的驗證等,可以使用TransactionTemplate加攔截器方式,為組件添加多個攔截器,spring AOP中提供了三類Advice,即前增強,後增強,拋出異常時的增強,可以靈活使用。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.