JTA in Spring
开发环境:JDK1.5(sun)+Myeclipse6.0+Tomcat5.5+ant 1.7.1+MySql5.0.4
框架版本:JSF1.2(sun)+hibernate3.3.1.GA+spring2.5.6
JTA(Java Transaction API)
EJB只支持标准的持久化(JTA)的事务管理,而Spring可以支持大部分流行持久化框架的事务管理。
在Spring中,通过实现org.springframework.transaction.PlatformTransactionManager接口能达到多种持久化框架的事务管理。
持久化方案 | Spring中配置事务的相对应类 |
JDBC | org.springframework.jdbc.datasource.DataSourceTransactionManager |
JTA | org.springframework.transaction.jta.JtaTransactionManager |
Hibernate | org.springframework.orm.hibernate3.HibernateTransactionManager |
JPA | org.springframework.orm.jpa.JpaTransactionManager |
Spring提供的事务管理器仅仅是对现有的事务实现API(Hibernate、JDBC、JTA)进行封装,其本身并没有提供具体的事务服务实现。
在Spring中事务在org.springframework.transaction.TransactionDefinition接口中定义。如果想深入了解Sping中的事务机制,必须要了解这个接口。
Spring中支持事务的传播属性共有七种,可以参考博文《Spring中的事务传播属性详解》及 《解惑 spring 嵌套事务》
假如我要达到如下要求:在特定的类中,如果方法名称是以insert,delete开头的方法要支持当前事务安全,如果当前没有事务,就新建一个事务,其他的方法只读。
下面就以上要求介绍两种在Spring中常用的事务配置方法:
方法一:用BeanNameAutoProxyCreator自动创建事务代理
-
- <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="insert*">PROPAGATION_REQUIRED</prop>
- <prop key="delete*">PROPAGATION_REQUIRED </prop>
- <prop key="*">readOnly</prop>
- </props>
- </property>
- </bean>
-
- <bean
- class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
- <property name="beanNames">
- <list>
- <value>persondao</value>
- </list>
- </property>
- <property name="interceptorNames">
- <list>
- <value>transactionInterceptor</value>
- </list>
- </property>
- </bean>
-
- <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
-
- <bean id="persondao"
- class="com.sms.freeborders.dao.hbimpl.PersonDAOImpl">
- <property name="hibernateTemplate" ref="hibernateTemplate" />
- </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="insert*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED </prop>
<prop key="*">readOnly</prop>
</props>
</property>
</bean>
<!-- 添加要实现事务规则的BeanName -->
<bean
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>persondao</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="persondao"
class="com.sms.freeborders.dao.hbimpl.PersonDAOImpl">
<property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>
方法二:利用AOP原理来管理事务
随着Sping项目代码量变大,你会发现配置越来越来多,且繁琐。在Sping2.0之后,Sping推出了简化的XML配置。具体可以看江南白衣的《简化Spring--配置文件》。
在使用方法二之前,需要加入包如下包:aspectjrt.jar、aspectjweaver.jar
且applicationContext.xml的头部定义如下:
- <?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:p="http://www.springframework.org/schema/p"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
- xmlns:jee="http://www.springframework.org/schema/jee"
- 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/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
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/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">
接着配置事务
- <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
-
-
-
- <aop:config>
- <aop:pointcut id="allManagerMethod"
- expression="execution(* com.sms.freeborders.dao.hbimpl.*.*(..))" />
- <aop:advisor advice-ref="txAdvice"
- pointcut-ref="allManagerMethod" />
- </aop:config>
-
-
-
- <tx:advice id="txAdvice">
- <tx:attributes>
- <tx:method name="insert *" />
- <tx:method name="delete*" />
- <tx:method name="*" read-only="true" />
- </tx:attributes>
- </tx:advice>
-
- <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
-
- <bean id="persondao"
- class="com.sms.freeborders.dao.hbimpl.PersonDAOImpl">
- <property name="hibernateTemplate" ref="hibernateTemplate" />
- </bean>