`
ruruhuang
  • 浏览: 189141 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Spring之事务管理

阅读更多

Spring之事务管理

EJB被人骂的够多了,除了SLSB(无状态SessionBean)CMT(Container-ManagedTransaction,容器管理事务)外,但是CMT依然需要以来ApplicationServerSpring提供了比CMT更加轻量的,好用易用的事务管理。

org.springframework.transaction.PlatformTransactionManager:是Spring事务管理的核心接口,正如类名,他分离了平台独立性。配合Springbeandefinition,他可以让我们在不同的事务平台上切换(jta->jdbc,etc)。

package org.springframework.transaction;
public interface PlatformTransactionManager {
  TransactionStatus getTransaction(TransactionDefinition definition)
      throws TransactionException;
  void commit(TransactionStatus status) throws TransactionException;
  void rollback(TransactionStatus status) throws TransactionException;

}

PlatformTransactionManager接口只有三个方法。Spring中使用AOP配合PlatformTransactionManager,可以使你感觉不到这个接口和他所依赖的类的存在。你需要做的只是在beandefinition中做写配置。你的代码不需要写一行关于事务的代码(特殊情况除外,如果你想在代码中控制事务的commitrollback)。当然你也可以使用编程式事务处理(这里不做介绍,可以参数Springdocument)


申明式事务管理

一种是使用AOPProxyFactoryBean TransactionInterceptor

<beans>

...

<beanid="mytxmanager"class="org.springframework.orm.hibernate.hibernatetransactionmanager">

<propertyname="sessionfactory" ref="mySessionFactory">

</bean>

<beanid="mytxinterceptor"< font="">

class="org.springframework.transaction.interceptor.TransactionInterceptor">

<propertyname="transactionmanager" ref="myTxManager">

<propertyname="transactionattributes">

<props>

<propkey="store*">PROPAGATION_REQUIRED</prop>

<propkey="create*">PROPAGATION_REQUIRED</prop>

<propkey="put*">PROPAGATION_REQUIRED</prop>

<propkey="*">PROPAGATION_REQUIRED,readOnly</prop>

</props>

</property>

</bean>

<beanid="myproductservicetarget"class="product.productserviceimpl">

<propertyname="productdao" ref="myProductDao">

</bean>

<beanid="myproductservice"class="org.springframework.aop.framework.proxyfactorybean">

<propertyname="proxyinterfaces">

<value>product.ProductService</value>

</property>

<propertyname="target" ref="myProductServiceTarget">

<propertyname="interceptornames">

<list>

<value>myTxInterceptor</value>

<value><!----></value>

</list>

</property>

</bean>

</beans>

这种配置更加灵活,你可以加更多的interceptorProductService中,如securityInterceptor.


二使用易用的,便利的TransactionProxyFactoryBean

<beans>

...

<beanid="mytxmanager"class="org.springframework.orm.hibernate.hibernatetransactionmanager">

<propertyname="sessionfactory" ref="mySessionFactory">

</bean>

<beanid="myproductservicetarget"class="product.productserviceimpl">

<propertyname="productdao" ref="myProductDao">

</bean>

<bean id="myProductService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">

<propertyname="transactionmanager" ref="myTxManager">

<propertyname="target" ref="myProductServiceTarget">

<propertyname="transactionattributes">

<props>

<propkey="store*">PROPAGATION_REQUIRED</prop>

<propkey="create*">PROPAGATION_REQUIRED</prop>

<propkey="put*">PROPAGATION_REQUIRED</prop>

<propkey="*">PROPAGATION_REQUIRED,readOnly</prop>

</props>

</property>

</bean>

</beans>

相比上面的代码,现在的代码少多了。我觉得Spring的类层次设计的非常合理,你可以使用其中的几个接口合并成另外一些大接口。如:ResourceBeanFactory

http://www.writely.com/View.aspx?docid=bdd75sqmr39x8

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics