Spring之事务管理
EJB被人骂的够多了,除了SLSB(无状态SessionBean),CMT(Container-ManagedTransaction,容器管理事务)外,但是CMT依然需要以来ApplicationServer。Spring提供了比CMT更加轻量的,好用易用的事务管理。
org.springframework.transaction.PlatformTransactionManager:是Spring事务管理的核心接口,正如类名,他分离了平台独立性。配合Spring的beandefinition,他可以让我们在不同的事务平台上切换(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中做写配置。你的代码不需要写一行关于事务的代码(特殊情况除外,如果你想在代码中控制事务的commit或rollback)。当然你也可以使用编程式事务处理(这里不做介绍,可以参数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>
这种配置更加灵活,你可以加更多的interceptor到ProductService中,如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
分享到:
相关推荐
Spring事务管理Demo
本代码使用H2内存数据库演示spring事务使用,包括编程式事务,声明式事务@Transactional使用,自定义事务事务注解实现自定义事务管理器
Java高级编程 实验报告 spring 声明事务 实验目的 掌握spring 声明式事务管理配置 实验环境 本实验采用本实验采用的eclipse或者 Myeclpse开发工具。Spring 4.0以上 Jdk1.7以上、oracle/mysql。
Spring事务管理教程,详细讲解了Spring中的事务管理,包括声明式事务,注解式事务,以及事务配置等等
3、了解Spring事务管理的3个核心接口; 4、了解Spring事务管理的两种方式; 5、掌握基于XML和Annotation的声明式事务管理的使用。 二.实验内容 (1)使用Spring JDBC实现书店的购书过程,即有如下一个BookShopDao接口...
spring的事务管理配置详解.txt详细的描述了如何配置spring的事物。
Spring事务管理.pdf 1.资料 2.本地事务与分布式事务 3.编程式模型 4.宣告式模型
自己做的一个小案例,spring 的事务管理小案例,你也可以去以下链接看文章讲解https://blog.csdn.net/zyqblog/article/details/82624441
关于spring_事务管理的一个实例,代码,全的!可以下载学习学习
spring 事务管理的理解
【免费】深入描述spring的事务处理机制,很不错的资源。
spring3.0两种事务管理配置
spring事务管理 配置文件等详解
spring-tx spring-transaction spring事务管理实例
Spring事务管理(全),需要的可下载!
Synchronized锁在Spring事务管理下,导致线程不安全。
spring 的事务管理
Spring的事务框架将开发过程中事务管理相关的关注点进行适当的分离,并对这些关注点进行合 理的抽象,最终打造了一套使用方便,却功能强大的事务管理“利器”。通过Spring的事务框架,我 们可以按照统一的编程模型来...
Spring2.5实现事务管理(本地事务、分布式事务).doc