在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代码

 

<!--  --> 

<bean id="transactionManager"  class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 

      <property name="sessionFactory"ref="sessionFactory" /> 

</bean> 

 

<!-- 定义事务规则的拦截器--> 

<beanid="transactionInterceptor" 

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

   <property name="transactionManager"ref="transactionManager" /> 

   <property name="transactionAttributes"> 

      <props> 

          <propkey="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> 

 

<beanid="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代码

 

<?xml version="1.0"encoding="UTF-8"?> 

<beansxmlns="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/beanshttp://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/jeehttp://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/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd "> 

接着配置事务

 

Xml代码

 

<beanid="transactionManager"  class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 

   <property name="sessionFactory"ref="sessionFactory" /> 

</bean> 

 

<!-- 这里的配置有AOP的思想,找的这个面就是com.sms.freeborders.dao.hbimpl下的所有类--> 

 

<aop:config> 

   <aop:pointcut id="allManagerMethod" 

      expression="execution(*com.sms.freeborders.dao.hbimpl.*.*(..))" /> 

   <aop:advisor advice-ref="txAdvice" 

      pointcut-ref="allManagerMethod" /> 

</aop:config> 

 

<!-- 配置事务规则 --> 

 

<tx:adviceid="txAdvice"> 

   <tx:attributes> 

       <tx:method name="insert *"/> 

      <tx:method name="delete*" /> 

      <tx:method name="*" read-only="true" /> 

   </tx:attributes> 

</tx:advice> 

 

<beanid="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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值