开发环境: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>
分享到:
相关推荐
在Spring中配置Hibernate事务
spring事务配置详解 spring事务配置详解
很好的spring+ibatis事务的配置文档.
Spring AOP配置事务方法,描述了spring的事务配置,方便开发应用和数据库的连接管理。
1.4. 在Spring配置文件中配置JOTM 1.5. 在Spring中运行测试 2 Spring引用Tomcat的 JTA事务 2.1. 添加所需的JAR文件 2.2. 配置JOTM 2.3. 配置Tomcat环境,配置JNDI的数据源 2.4. Spring中相应的配置 3 小结
spring几种事务配置,spring几种事务配置详解【精】
Spring声明式事务配置管理方法
spring 事务spring 事务spring 事务spring 事务spring 事务
spring声明事务的配置 spring声明事务的配置 spring声明事务的配置
(2)(2) 在BookShopDao中添加一个purchase购书方法,其操作流程是获取书的单价->买书(更新库存)->更新账户余额,在BookShopDaoImp中实现该方法,并使用基于XML和Annotation的声明式事务管理来确保该购书过程能正常...
<?xml version="1.0" encoding="UTF-8"?><beansxmlns=...
Spring事务原理、Spring事务配置的五种方式
Java高级编程 实验报告 spring 声明事务 实验目的 掌握spring 声明式事务管理配置 实验环境 本实验采用本实验采用的eclipse或者 Myeclpse开发工具。Spring 4.0以上 Jdk1.7以上、oracle/mysql。
Spring 事务代理配置 Spring 事务代理配置
Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource、TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分。 DataSource、TransactionManager这两部分只是会...
很全面的介绍spring的事务配制方法。初学者可以看看
spring3.0两种事务管理配置
Spring的事务配置的五种方式 Spring的事务配置的五种方式 Spring的事务配置的五种方式 Spring的事务配置的五种方式
spring的事务管理配置详解.txt详细的描述了如何配置spring的事物。
这是在java里使用到spring的配置文件里,添加事务处理过程,以至于可以回滚事务,当中使用到拦截器。