原貼地址:
http://blogger.org.cn/blog/more.asp?name=hongrui&id=11162在spring中使用聲明型事務
spring使用aop機制管理jdbc的連接和事務。它使用TransactionInterceptor類,Spring事務支持中的核心接口是
org.springframework.transaction.PlatformTransactionManager。為了實際執行事務,Spring所有的事務劃分功能都通過傳遞適當的TransactionDefinition實例,委托給PlatformTransactionManager。
盡管PlatformTransactionManager接口可以直接使用,應用程序通常配置具體的事務管理器并使用聲明性事務來劃分事務。
Spring具有多種PlatformTransactionManager實現,它們分為兩類:
局部事務策略即針對單個資源執行事務(主要是針對單個的數據庫)。實現有org.springframework.jdbc.datasource.DataSourceTransactionManager。 它用于jdbc數據源的配置,調用TransactionInterceptor開是一個事務,
從DataSource得到一個connection并確保auto-commit設為disabled。他用JdbcTemplate在一個線程內綁定一個JDBC connection,TransactionInterceptor負責提交事務,
DataSourceTransactionManager調用Connection.commit()關閉connection,并解除綁定(potentially allowing for one thread connection per data source)。
例如
<bean id="DataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@localhost:1521:hua2</value>
</property>
<property name="username">
<value>user</value></property>
<property name="password">
<value>gotpassword</value>
</property>
</bean>
</beans>
<bean id="DataSourceTransactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="DataSource" />
</bean>
<bean id="tatanTransactionScriptsProxyCreator"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<idref bean="tatanTransactionScripts" />
</list>
</property>
<property name="interceptorNames">
<list>
<idref bean="DataSourceTransactionInterceptor" />
</list>
</property>
</bean>
<bean id="DataSourceTransactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager"
ref="DataSourceTransactionManager" />
<property name="transactionAttributeSource">
<value>
com.tatan.tatanTransactionScriptsImpl.*=PROPAGATION_REQUIRED
</value>
</property>
</bean>
?transactionAttributesSource 屬性指定每個方法的transaction attribute,PROPAGATION_REQUIRED說明在一個事務內這個方法被執行。
?和EJB一樣,默認的情況下,spring只有當unchecked exception被拋出時,才rollback事務,也可以自己加入checked exception。
?tatanTransactionScripts被TransactionInterceptor封裝,在一個事物內執行類的每一個方法。
更為簡單的配置
?
??? <bean id="UserManagerTran"
??????????? class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
??????????? <property name="transactionManager">
??????????????? <ref bean="transactionManager"/></property>
??????????? <property name="target"><ref bean="UserManager"/></property>
??????????? <property name="transactionAttributes">
??????????????? <props>
??????????????????? <prop key="insert*">
??????????????????????? PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED</prop>
??????????????????? <prop key="tran*">
??????????????????????? PROPAGATION_REQUIRED, ISOLATION_SERIALIZABLE</prop>
??????????????????? <prop key="deposit*">
??????????????????????? PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED</prop>
??????????????? </props>
??????????? </property>
??????? </bean>
???????? <bean id="transactionManager"
?????????? class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
?????????? <property name="dataSource"><ref bean="dataSource"/></property>
?????? </bean>
(The TransactionProxyFactoryBean is a ProxyFactoryBean where every bean is adviced with a TransactionInterceptor. And the TransactionInterceptor is a piece of advice.
So you can use a seperate TransactionInterceptor and ProxyFactoryBean. But if you are lazy/smart, you can use the TransactionProxyFactoryBean that does the same thing (only less configuration needed))
對于特定的方法或方法命名模式,代理的具體事務行為由事務屬性驅動,如下面的例子所示:
<prop key="insert*">
?ROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED
?</prop>
key屬性確定代理應該給哪個方法增加事務行為。這樣的屬性最重要的部份是傳播行為。有以下選項可供使用:
?PROPAGATION_REQUIRED--支持當前事務,如果當前沒有事務,就新建一個事務。這是最常見的選擇。
?PROPAGATION_SUPPORTS--支持當前事務,如果當前沒有事務,就以非事務方式執行。
?PROPAGATION_MANDATORY--支持當前事務,如果當前沒有事務,就拋出異常。
?PROPAGATION_REQUIRES_NEW--新建事務,如果當前存在事務,把當前事務掛起。
?PROPAGATION_NOT_SUPPORTED--以非事務方式執行操作,如果當前存在事務,就把當前事務掛起。
?PROPAGATION_NEVER--以非事務方式執行,如果當前存在事務,則拋出異常。
?PROPAGATION_NESTED--如果當前存在事務,則在嵌套事務內執行。如果當前沒有事務,則進行與PROPAGATION_REQUIRED類似的操作。
?前六個策略類似于EJB CMT,第七個(PROPAGATION_NESTED)是Spring所提供的一個特殊變量。
?它要求事務管理器或者使用JDBC 3.0 Savepoint API提供嵌套事務行為(如Spring的DataSourceTransactionManager)。
事務屬性中的readOnly標志表示對應的事務應該被最優化為只讀事務。這是一個最優化提示。在一些情況下,一些事務策略能夠起到顯著的最優化效果,例如在使用Object/Relational映射工具(如:Hibernate或TopLink)時避免dirty checking(試圖“刷新”)。
在事務屬性中還有定義“timeout”值的選項,指定事務超時為幾秒。在JTA中,這將被簡單地傳遞到J2EE服務器的事務協調程序,并據此得到相應的解釋。
?? 全局事務管理即執行有可能跨越多個資源的全局事務。主要對應的Spring類是org.springframework.transaction.jta.JtaTransactionManager,它委托給遵循JTA規范的J2EE服務器,也有例外。
spring支持JTA,只需要一個標準的JtaTransactionManager定義,數據庫必須支持XA protocol,或者J2EE服務器提供支持XA規范的DataSource。
默認的Spring JtaTransactionManager設置將從標準的JNDI位置獲取JTA的javax.transaction.UserTransaction對象,該JNDI位置由J2EE指定:java:comp/UserTransaction。對于大多數標準J2EE環境下的用例來說,它工作良好。
但是,默認的JtaTransactionManager不能執行事務掛起操作(即它不支持PROPAGATION_REQUIRES_NEW和PROPAGATION_NOT_SUPPORTED)。原因是標準的JTA UserTransaction接口不支持掛起或恢復事務的操作;它只支持開始和完成新事務的操作。
為執行事務掛起操作,還需要提供javax.transaction.TransactionManager實例,按照JTA的規定,它提供標準的掛起和恢復方法。遺憾的是,J2EE沒有為JTA TransactionManager定義標準的JNDI位置!
因此,必須使用特定于供應商的(vendor-specific)查尋機制。J2EE沒有考慮把JTA TransactionManager接口作為它的公開API的一部分。JTA規范規定的TransactionManager接口原本是打算用于容器集成的。
但是為JTA TransactionManager定義標準的JNDI位置還是有重大意義的,尤其是對于輕量級容器(如Spring);然后,便可以以同樣的方式來定位任意的J2EE服務器的JTA TransactionManager。
結合jboss JTA的Spring事務劃分
oracle-ds.xml
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
?<xa-datasource>
??<jndi-name>XASpringDS</jndi-name>
??<track-connection-by-tx/>
??<isSameRM-override-value>false</isSameRM-override-value>
??<xa-datasource-class>oracle.jdbc.xa.client.OracleXADataSource</xa-datasource-class>
??<xa-datasource-property name="URL">jdbc:oracle:oci8:@orcl</xa-datasource-property>
??<xa-datasource-property name="User">SCOTT</xa-datasource-property>
??<xa-datasource-property name="Password">tiger</xa-datasource-property>
??<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter</exception-sorter-class-name>
??<no-tx-separate-pools/>
?</xa-datasource>
?<mbean
??code="org.jboss.resource.adapter.jdbc.xa.oracle.OracleXAExceptionFormatter"
??name="jboss.jca:service=OracleXAExceptionFormatter">
??<depends optional-attribute-name="TransactionManagerService">
???jboss:service=TransactionManager</depends>
?</mbean>
</datasources>
spring配置
<!-- Data source bean -->
?
?<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
??<property name="jndiName"><value>java:/XASpringDS</value></property>
?</bean>
?
?<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>
Spring有效地將DAO實現從實際的運行時環境中分離出來,允許在J2EE容器之外輕松地測試或重用用。
Spring提供了多種事務策略,比如JtaTransactionManager和JDBC DataSourceTransactionManager,
前者委托給J2EE服務器的事務協調程序,后者則針對單個JDBC DataSource(即單個的目標數據庫)執行事務。
通過對后端配置進行簡單的更改,就能夠輕松地調整事務策略適應另一個環境。
posted on 2006-10-23 09:24
OMG 閱讀(804)
評論(0) 編輯 收藏 所屬分類:
Spring