javascript
Spring管理事务的若干配置形式
Spring管理事務的若干配置形式
雖說利用Spring來實現配置式事務的基本原理都是AOP,但其配置方法也多種多樣,以下從互聯網摘抄了一些,希望起一個總結作用(有版權問題的話請留言作者,我將立即刪除):
以下配置均忽略datasource,transactionManager,sessionFactory之類的配置,因為無論何種方式,前兩者都不能少
1、? 比較原始和煩瑣的配置方法(每個SERVICE都來這么一下,不累死?):
<bean id="serviceMe"??? class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
??? ??? <property name="transactionManager">
?????? ??? <ref bean="transactionManager" />
??? ??? </property>
??? ??? <property name="target">
<bean??? class="com.test.test.service.Service1">
?????????? ??? <property name="myDao">
?????????? ?????? <ref bean="myDao" />
?????????? ??? </property>
?????? ??? </bean>
??? ??? </property>
??? ??? <property name="transactionAttributes">
?????? ??? <props>
?????????? ??? <prop key="save*">PROPAGATION_REQUIRED_NEW,-Exception</prop>
?????????? ??? <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
?????? ??? </props>
??? ??? </property>
??? </bean>
?
2、下面有一點小改進,但還是有一些煩
<bean id="txProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> ??? <property name="transactionManager"> ??????? ref bean="transactionManager"/> ??? </property> ??? <property name="transactionAttributes"> ??????? <props> ??????????? <prop key="find*">PROPAGATION_REQUIRED, readOnly</prop> ??????????? <prop key="*">PROPAGATION_REQUIRED</prop> ??????? </props> ??? </property> </bean> ? <bean id="userManager" parent="txProxyTemplate"> ??? <property name="target"> ??????? <bean class="some.package.UserManagerImpl"> ??????????? <property name="userDAO"><ref bean="userDAO"/></property> ??????? </bean> ??? </property> </bean>以后,如果增加新的Service/Manager,則XML配置的增量是這一段:
<bean id="someOtherManager" parent="txProxyTemplate"> ??? <property name="target"> ??????? <bean class="some.package.someOtherManagerImpl"> ??????? </bean> ??? </property> </bean>上面說的是老的做法,比較傳統。缺點是增量比較大,配置起來copy&paste讓人覺得不太爽,比較臃腫。
3、用 DefaultAdvisorAutoProxyCreator 實現自動代理,實現了對容器中所有 bean 的自動代理, 增加一個需要事務的業務 bean 時只要在 transactionInterceptor 增加一行即可, 增加別的 interceptor 也非常方便,極大減少了配置量
?
<beans> ???? <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) --> ??? <bean id="transactionManager" ????????? class="org.springframework.orm.hibernate3.HibernateTransactionManager"> ????????? <property name="sessionFactory"> ????????????? <ref bean="sessionFactory"/> ????????? </property> ??? </bean> ? ???? <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> ??? ???? <property name="transactionManager" ref="transactionManager"/> ???? ???? <property name="transactionAttributeSource"> ??????? ? <value> ??????? ???? com.skyon.user.manager.UserManager.*=PROPAGATION_REQUIRED ??????? ???? #Add new defines here -> ??????? ? </value> ???? ???? </property> ???? </bean> ? ???? <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"> ???? ???? <property name="interceptorNames"> ??????? ???? <list> ??????? ???? ???? <value>transactionInterceptor</value> ??????? ???? ???? <!-- ??????? ???? ???? 增加新的 Interceptor ??????? ???? ???? --> ??????? ???? </list> ???? ???? </property> ???? </bean> ? ???? <bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor"> ???? ? <property name="transactionInterceptor" ref="transactionInterceptor"/> ???? </bean> ? </beans>?
4、改用BeanNameAutoProxyCreator,畢竟不是context下的每個bean都需要事務
<beans> ??? <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) --> ??? <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> ??? ????<property name="sessionFactory"> ??????????? <ref bean="sessionFactory"/> ??????? </property> ??? </bean> ? ??? <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> ??????? <property name="transactionManager" ref="transactionManager"/>? ???? ???? <property name="transactionAttributes"> ??????? ???? <props> ??????? ???? ???? <prop key="*">PROPAGATION_REQUIRED</prop> ??????? ???? ???? <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> ??????? ???? </props> ???? ???? </property> ??? </bean> ? ??? <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> ??????? <property name="beanNames"> ??????????? <value>*Service,*Manager</value> ??????? </property> ??????? <property name="interceptorNames"> ??????????? <list> ??????????????? <value>transactionInterceptor</value> ??????????????? <!-- 此處增加新的Interceptor --> ??????????? </list> ??????? </property> ??? </bean> ? ??? <bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor"> ??????? <property name="transactionInterceptor" ref="transactionInterceptor"/>? ?? ?</bean> ? ??? <bean id="userManager" class="some.package.UserManagerImpl" autoWire="byName"/> ? </beans>以后每次的增量是這一段:
<bean id="userManager" class="some.package.UserManagerImpl" autoWire="byName"/>?
5、BeanNameAutoProxyCreator的另一種配法
<!-- Transaction Interceptor set up to do PROPOGATION_REQUIRED on all methods -->
? <bean id="matchAllWithPropReq"
????? class="org.springframework.transaction.interceptor.MatchAlwaysTransactionAttributeSource">
??? <property name="transactionAttribute"><value>PROPAGATION_REQUIRED</value></property>
? </bean>
? <bean id="matchAllTxInterceptor"
????? class="org.springframework.transaction.interceptor.TransactionInterceptor">
??? <property name="transactionManager"><ref bean="transactionManager"/></property>
??? <property name="transactionAttributeSource"><ref bean="matchAllWithPropReq"/></property>
? </bean>
?
? <!-- One BeanNameAutoProxyCreator handles all beans where we want all methods to use
?????? PROPOGATION_REQUIRED -->
? <bean id="autoProxyCreator"
????? class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
??? <property name="interceptorNames">
????? <list>
??????? <idref local="matchAllTxInterceptor"/>
??????? <idref bean="hibInterceptor"/>
????? </list>
??? </property>
??? <property name="beanNames">
????? <list>
??????? <idref local="core-services-applicationControllerSevice"/>
??????? <idref local="core-services-deviceService"/>
??????? <idref local="core-services-authenticationService"/>
??????? <idref local="core-services-packagingMessageHandler"/>
??????? <idref local="core-services-sendEmail"/>
??????? <idref local="core-services-userService"/>
????? </list>
??? </property>
? </bean>
?
?
其它一些注意:
1、如果你想使用配置事務,但又不想使用Spring的模板方法,要注意,獲取connection時不能直接從dataSource獲得:
Connection conn = DataSourceUtils.getConnection(dataSource);
2、? 默認情況下SPRING只有在RuntimeException時才回滾事務,要使其它異常也回滾,需要在配置中作一些改變,見第一種配置中的相關信息。
?
總結
以上是生活随笔為你收集整理的Spring管理事务的若干配置形式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 购房记---看房8
- 下一篇: Spring2 Hibernate3 J