在spring中使用聲明型事務(轉)

在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" <br="">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" <br="">class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="DataSource">
</bean>
<bean id="tatanTransactionScriptsProxyCreator" <br="">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" <br="">class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" <br="">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" <br="">??????????? 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" <br="">?????????? 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服務器的事務協調程序,並據此得到相應的解釋。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章