spring 事物配置

 
查看文章
  
在Spring中配置事务

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自动创建事务代理

 

Xml代码 复制代码
  1. <!--   -->  
  2. <bean id="transactionManager"   class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  3.        <property name="sessionFactory" ref="sessionFactory" />  
  4. </bean>  
  5.   
  6. <!-- 定义事务规则的拦截器 -->  
  7. <bean id="transactionInterceptor"  
  8.     class="org.springframework.transaction.interceptor.TransactionInterceptor">  
  9.     <property name="transactionManager" ref="transactionManager" />  
  10.     <property name="transactionAttributes">  
  11.        <props>  
  12.            <prop key="insert*">PROPAGATION_REQUIRED</prop>  
  13.            <prop key="delete*">PROPAGATION_REQUIRED </prop>  
  14.            <prop key="*">readOnly</prop>  
  15.        </props>  
  16.     </property>  
  17. </bean>  
  18. <!-- 添加要实现事务规则的BeanName -->  
  19. <bean  
  20.     class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
  21.     <property name="beanNames">  
  22.        <list>  
  23.            <value>persondao</value>  
  24.        </list>  
  25.     </property>  
  26.     <property name="interceptorNames">  
  27.        <list>  
  28.            <value>transactionInterceptor</value>  
  29.        </list>  
  30.     </property>  
  31. </bean>  
  32.   
  33. <bean id="hibernateTemplate"      class="org.springframework.orm.hibernate3.HibernateTemplate">  
  34.     <property name="sessionFactory" ref="sessionFactory" />  
  35. </bean>  
  36.   
  37. <bean id="persondao"  
  38.        class="com.sms.freeborders.dao.hbimpl.PersonDAOImpl">  
  39.     <property name="hibernateTemplate" ref="hibernateTemplate" />  
  40. </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代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:p="http://www.springframework.org/schema/p"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop"   xmlns:context="http://www.springframework.org/schema/context"  
  6.     xmlns:jee="http://www.springframework.org/schema/jee"  
  7.     xmlns:tx="http://www.springframework.org/schema/tx"  
  8.     xsi:schemaLocation="   
  9.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  10.             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd   
  11.             http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd   
  12.             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd   
  13.             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 ">

 

接着配置事务

 

Xml代码 复制代码
  1. <bean id="transactionManager"   class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  2.     <property name="sessionFactory" ref="sessionFactory" />  
  3. </bean>  
  4.   
  5. <!-- 这里的配置有AOP的思想,找的这个面就是com.sms.freeborders.dao.hbimpl下的所有类-->  
  6.   
  7. <aop:config>  
  8.     <aop:pointcut id="allManagerMethod"  
  9.        expression="execution(* com.sms.freeborders.dao.hbimpl.*.*(..))" />  
  10.     <aop:advisor advice-ref="txAdvice"  
  11.        pointcut-ref="allManagerMethod" />  
  12. </aop:config>  
  13.   
  14. <!-- 配置事务规则 -->  
  15.   
  16. <tx:advice id="txAdvice">  
  17.     <tx:attributes>  
  18.        <tx:method name="insert *" />  
  19.        <tx:method name="delete*" />  
  20.        <tx:method name="*" read-only="true" />  
  21.     </tx:attributes>  
  22. </tx:advice>  
  23.   
  24. <bean id="hibernateTemplate"   class="org.springframework.orm.hibernate3.HibernateTemplate">  
  25.        <property name="sessionFactory" ref="sessionFactory" />  
  26. </bean>  
  27.   
  28. <bean id="persondao"  
  29.        class="com.sms.freeborders.dao.hbimpl.PersonDAOImpl">  
  30.     <property name="hibernateTemplate" ref="hibernateTemplate" />  
  31. </bean>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值