在JAVA EE中,事务可以分为全局事务和局部事务,全局事务需要使用JTA(java transaction api)接口。由于JTA的UserTransaction 需要从JNDI获得,所以我们需要同时使用JTA和JNDI,所以使用JTA事务需要在JAVA EE应用服务器中运行,限制了应用的范围。全局事务管理一般通过EJB容器的 CMT (容器管理事务 ),CMT是一种声明式的事务管理机制, 但是需要依赖JTA和EJB容器,不会直接依赖JNDI(间接在EJB中依赖JNDI)。局部事务管理中,使用JDBC事务管理器,不能用于多个事务性资源,同时表现为入侵式的事务编程。
Spring同时提供了声明式的和编程式的事务管理机制,不依赖于EJB等任何容器,使应用开发者能够使用在 任何环境 下使用 一致 的编程模型。其中声明式的事务管理是首选,开发者通常书写很少的或没有与事务相关的代码,因此不依赖Spring框架或任何其他事务API。编程式事务管理具有一定的入侵性,不推荐使用。
Spring中声明式的事务管理类型配置:
1、spring aop 声明式事务的策略有:
(1)、 PROPAGATION_REQUIRED -- 支持当前的事务,如果不存在就创建一个新的。这是最常用的选择。
(2) 、 PROPAGATION_SUPPORTS -- 支持当前的事务,如果不存在就不使用事务。
(3) 、 PROPAGATION_MANDATORY -- 支持当前的事务,如果不存在就抛出异常。
(4) 、 PROPAGATION_REQUIRES_NEW -- 创建一个新的事务,并暂停当前的事务(如果存在)。
(5) 、 PROPAGATION_NOT_SUPPORTED -- 不使用事务,并暂停当前的事务(如果存在)。
(6) 、 PROPAGATION_NEVER -- 不使用事务,如果当前存在事务就抛出异常。
(7) 、 PROPAGATION_NESTED -- 如果当前存在事务就作为嵌入事务执行,否则与 PROPAGATION_REQUIRED 类似。
2、Spring事务配置文件由三个组成部分,分别是DataSource、TransactionManager和代理机制这三部分,其中,DataSource和TransactionManager随着不同实现厂商数据访问的技术进行变化,Spring中配置方式的变化主要体现在事务代理上。
一、TransactionProxyFactoryBean事务代理
<? xml version="1.0" encoding="UTF-8" ?>
< beans xmlns ="http://www.springframework.org/schema/beans"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context ="http://www.springframework.org/schema/context"
xmlns:aop ="http://www.springframework.org/schema/aop"
xsi:schemaLocation ="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" >
< bean id ="sessionFactory"
class ="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
< property name ="configLocation" value ="classpath:hibernate.cfg.xml" />
< property name ="configurationClass" value ="org.hibernate.cfg.AnnotationConfiguration" />
</ bean >
<!-- 定义事务管理器(声明式的事务) -->
< bean id ="transactionManager"
class ="org.springframework.orm.hibernate3.HibernateTransactionManager" >
< property name ="sessionFactory" ref ="sessionFactory" />
</ bean >
<!-- 配置DAO -->
< bean id ="userDaoTarget" class ="com.bluesky.spring.dao.UserDaoImpl" >
< property name ="sessionFactory" ref ="sessionFactory" />
</ bean >
< bean id ="userDao"
class ="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
<!-- 配置事务管理器 -->
< property name ="transactionManager" ref ="transactionManager" />
< property name ="target" ref ="userDaoTarget" />
< property name ="proxyInterfaces" value ="com.bluesky.spring.dao.GeneratorDao" />
<!-- 配置事务属性 -->
< property name ="transactionAttributes" >
< props >
< prop key ="*" > PROPAGATION_REQUIRED </ prop >
</ props >
</ property >
</ bean >
</ beans >
二、抽象基类事务代理
<!-- 事务代理的基类 -->
<bean id="baseTransactionProxy" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="proxyTargetClass" value="false"/>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- 业务类 -->
<bean id="myUserDAO" class="com.hs.dao.MyUserDAOImpl">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<!-- 业务类的事务代理 -->
<bean id="myUserDAOProxy" parent="baseTransactionProxy">
<property name="target">
<ref bean="myUserDAO"/>
</property>
</bean>
三、拦截器事务代理
<? xml version="1.0" encoding="UTF-8" ?>
< beans xmlns ="http://www.springframework.org/schema/beans"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context ="http://www.springframework.org/schema/context"
xmlns:aop ="http://www.springframework.org/schema/aop"
xsi:schemaLocation ="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" >
< bean id ="sessionFactory"
class ="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
< property name ="configLocation" value ="classpath:hibernate.cfg.xml" />
< property name ="configurationClass" value ="org.hibernate.cfg.AnnotationConfiguration" />
</ bean >
<!-- 定义事务管理器(声明式的事务) -->
< bean id ="transactionManager"
class ="org.springframework.orm.hibernate3.HibernateTransactionManager" >
< property name ="sessionFactory" ref ="sessionFactory" />
</ bean >
< bean id ="transactionInterceptor"
class ="org.springframework.transaction.interceptor.TransactionInterceptor" >
< property name ="transactionManager" ref ="transactionManager" />
<!-- 配置事务属性 -->
< property name ="transactionAttributes" >
< props >
< prop key ="*" > PROPAGATION_REQUIRED </ prop >
</ props >
</ property >
</ bean >
< bean class ="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" >
< property name ="beanNames" >
< list >
< value > *Dao </ value >
</ list >
</ property >
< property name ="interceptorNames" >
< list >
< value > transactionInterceptor </ value >
</ list >
</ property >
</ bean >
<!-- 配置DAO -->
< bean id ="userDao" class ="com.bluesky.spring.dao.UserDaoImpl" >
< property name ="sessionFactory" ref ="sessionFactory" />
</ bean >
</ beans >
四、 使用tx标签配置的拦截器
<? xml version="1.0" encoding="UTF-8" ?>
< beans xmlns ="http://www.springframework.org/schema/beans"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context ="http://www.springframework.org/schema/context"
xmlns:aop ="http://www.springframework.org/schema/aop"
xmlns:tx ="http://www.springframework.org/schema/tx"
xsi:schemaLocation ="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" >
< context:annotation-config />
< context:component-scan base-package ="com.bluesky" />
< bean id ="sessionFactory"
class ="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
< property name ="configLocation" value ="classpath:hibernate.cfg.xml" />
< property name ="configurationClass" value ="org.hibernate.cfg.AnnotationConfiguration" />
</ bean >
<!-- 定义事务管理器(声明式的事务) -->
< bean id ="transactionManager"
class ="org.springframework.orm.hibernate3.HibernateTransactionManager" >
< property name ="sessionFactory" ref ="sessionFactory" />
</ bean >
< tx:advice id ="txAdvice" transaction-manager ="transactionManager" >
< tx:attributes >
< tx:method name ="*" propagation ="REQUIRED" />
</ tx:attributes >
</ tx:advice >
< aop:config >
< aop:pointcut id ="interceptorPointCuts"
expression ="execution(* com.bluesky.spring.dao.*.*(..))" />
< aop:advisor advice-ref ="txAdvice"
pointcut-ref ="interceptorPointCuts" />
</ aop:config >
</ beans >
五、基于Annotation(注释)
<? xml version="1.0" encoding="UTF-8" ?>
< beans xmlns ="http://www.springframework.org/schema/beans"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context ="http://www.springframework.org/schema/context"
xmlns:aop ="http://www.springframework.org/schema/aop"
xmlns:tx ="http://www.springframework.org/schema/tx"
xsi:schemaLocation ="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" >
< context:annotation-config />
< context:component-scan base-package ="com.bluesky" />
< tx:annotation-driven transaction-manager ="transactionManager" />
< bean id ="sessionFactory"
class ="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
< property name ="configLocation" value ="classpath:hibernate.cfg.xml" />
< property name ="configurationClass" value ="org.hibernate.cfg.AnnotationConfiguration" />
</ bean >
<!-- 定义事务管理器(声明式的事务) -->
< bean id ="transactionManager"
class ="org.springframework.orm.hibernate3.HibernateTransactionManager" >
< property name ="sessionFactory" ref ="sessionFactory" />
</ bean >
</ beans >