`

spring 事务管理配置总结

 
阅读更多

前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识。通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的。

总结如下:

Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource、TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分。

DataSource、TransactionManager这两部分只是会根据数据访问方式有所变化,比如使用Hibernate进行数据访问时,DataSource实际为SessionFactory,TransactionManager的实现为HibernateTransactionManager。

具体如下图:

Spring事务配置 (2)

根据代理机制的不同,总结了五种Spring事务的配置方式,配置文件如下:

第一种方式:每个Bean都有一个代理

<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="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"
>

<beanid="sessionFactory"
class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<propertyname="configLocation"value="classpath:hibernate.cfg.xml"/>
<propertyname="configurationClass"value="org.hibernate.cfg.AnnotationConfiguration"/>
</bean>

<!--定义事务管理器(声明式的事务)-->
<beanid="transactionManager"
class
="org.springframework.orm.hibernate3.HibernateTransactionManager">
<propertyname="sessionFactory"ref="sessionFactory"/>
</bean>

<!--配置DAO-->
<beanid="userDaoTarget"class="com.bluesky.spring.dao.UserDaoImpl">
<propertyname="sessionFactory"ref="sessionFactory"/>
</bean>

<beanid="userDao"
class
="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<!--配置事务管理器-->
<propertyname="transactionManager"ref="transactionManager"/>
<propertyname="target"ref="userDaoTarget"/>
<propertyname="proxyInterfaces"value="com.bluesky.spring.dao.GeneratorDao"/>
<!--配置事务属性-->
<propertyname="transactionAttributes">
<props>
<propkey="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>

第二种方式:所有Bean共享一个代理基类

<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="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"
>

<beanid="sessionFactory"
class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<propertyname="configLocation"value="classpath:hibernate.cfg.xml"/>
<propertyname="configurationClass"value="org.hibernate.cfg.AnnotationConfiguration"/>
</bean>

<!--定义事务管理器(声明式的事务)-->
<beanid="transactionManager"
class
="org.springframework.orm.hibernate3.HibernateTransactionManager">
<propertyname="sessionFactory"ref="sessionFactory"/>
</bean>

<beanid="transactionBase"
class
="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
lazy-init
="true"abstract="true">
<!--配置事务管理器-->
<propertyname="transactionManager"ref="transactionManager"/>
<!--配置事务属性-->
<propertyname="transactionAttributes">
<props>
<propkey="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>

<!--配置DAO-->
<beanid="userDaoTarget"class="com.bluesky.spring.dao.UserDaoImpl">
<propertyname="sessionFactory"ref="sessionFactory"/>
</bean>

<beanid="userDao"parent="transactionBase">
<propertyname="target"ref="userDaoTarget"/>
</bean>
</beans>

第三种方式:使用拦截器

<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="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"
>

<beanid="sessionFactory"
class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<propertyname="configLocation"value="classpath:hibernate.cfg.xml"/>
<propertyname="configurationClass"value="org.hibernate.cfg.AnnotationConfiguration"/>
</bean>

<!--定义事务管理器(声明式的事务)-->
<beanid="transactionManager"
class
="org.springframework.orm.hibernate3.HibernateTransactionManager">
<propertyname="sessionFactory"ref="sessionFactory"/>
</bean>

<beanid="transactionInterceptor"
class
="org.springframework.transaction.interceptor.TransactionInterceptor">
<propertyname="transactionManager"ref="transactionManager"/>
<!--配置事务属性-->
<propertyname="transactionAttributes">
<props>
<propkey="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>

<beanclass="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<propertyname="beanNames">
<list>
<value>*Dao</value>
</list>
</property>
<propertyname="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>

<!--配置DAO-->
<beanid="userDao"class="com.bluesky.spring.dao.UserDaoImpl">
<propertyname="sessionFactory"ref="sessionFactory"/>
</bean>
</beans>

第四种方式:使用tx标签配置的拦截器

<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="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-scanbase-package="com.bluesky"/>

<beanid="sessionFactory"
class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<propertyname="configLocation"value="classpath:hibernate.cfg.xml"/>
<propertyname="configurationClass"value="org.hibernate.cfg.AnnotationConfiguration"/>
</bean>

<!--定义事务管理器(声明式的事务)-->
<beanid="transactionManager"
class
="org.springframework.orm.hibernate3.HibernateTransactionManager">
<propertyname="sessionFactory"ref="sessionFactory"/>
</bean>

<tx:adviceid="txAdvice"transaction-manager="transactionManager">
<tx:attributes>
<tx:methodname="*"propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>

<aop:config>
<aop:pointcutid="interceptorPointCuts"
expression
="execution(* com.bluesky.spring.dao.*.*(..))"/>
<aop:advisoradvice-ref="txAdvice"
pointcut-ref
="interceptorPointCuts"/>
</aop:config>
</beans>

第五种方式:全注解

<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="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-scanbase-package="com.bluesky"/>

<tx:annotation-driventransaction-manager="transactionManager"/>

<beanid="sessionFactory"
class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<propertyname="configLocation"value="classpath:hibernate.cfg.xml"/>
<propertyname="configurationClass"value="org.hibernate.cfg.AnnotationConfiguration"/>
</bean>

<!--定义事务管理器(声明式的事务)-->
<beanid="transactionManager"
class
="org.springframework.orm.hibernate3.HibernateTransactionManager">
<propertyname="sessionFactory"ref="sessionFactory"/>
</bean>

</beans>

此时在DAO上需加上@Transactional注解,如下:

packagecom.bluesky.spring.dao;

importjava.util.List;

importorg.hibernate.SessionFactory;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;
importorg.springframework.stereotype.Component;

importcom.bluesky.spring.domain.User;

@Transactional
@Component(
"userDao")
publicclassUserDaoImplextendsHibernateDaoSupportimplementsUserDao {

publicList<User>listUsers() {
returnthis.getSession().createQuery("from User").list();
}


}
分享到:
评论

相关推荐

    spring杂谈 作者zhang KaiTao

    1.9 Spring对事务管理的支持的发展历程(基础篇) 1.10 基于JDK动态代理和CGLIB动态代理的实现Spring注解管理事务(@Trasactional)到底有什么区别。 1.11 在spring中获取代理对象代理的目标对象工具类 1.12 如何为...

    Spring + Hibernate + Struts 事务配置小例子(带提示框等小技巧)

    -- 配置事务管理器 --&gt; class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt; &lt;!-- 配置事务拦截器--&gt; class="org.springframework.transaction.interceptor....

    全网最热spring问题总结.pdf

    事务管理: Spring 提供一个持续的事务管理接口, 可以扩展到上至本地 事务下至全局事务(JTA) 。 异常处理: Spring 提供方便的 API 把具体技术相关的异常(比如由 JDBC, Hibernate or JDO 抛出的) 转化为...

    spring.doc

    5.1.8.3Spring事务的隔离级别 117 拓展: 118 5.1.8.4以XML配置的 形式 119 拓展: 120 5.1.8.5以注解方式配置 125 拓展: 127 5.1.9使用CGLIB以XML形式配置事务 130 5.2 Spring+Hibernate 131 5.2.1 ...

    Spring 2.0 开发参考手册

    9.5.1. 理解Spring的声明式事务管理实现 9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. &lt;tx:advice/&gt; 有关的设置 9.5.6. 使用 @Transactional 9.5.7. 插入事务操作 9.5.8. ...

    Spring-Reference_zh_CN(Spring中文参考手册)

    9.5.1. 理解Spring的声明式事务管理实现 9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. &lt;tx:advice/&gt; 有关的设置 9.5.6. 使用 @Transactional 9.5.6.1. @Transactional 有关的设置 ...

    spring.net中文手册在线版

    14.5.1.理解Spring.NET声明式事务管理的实现 14.5.2.第一个例子 14.5.3.Transaction特性的设置 14.5.4.通过AutoProxyCreator使用声明式事务 14.5.5.通过TransactionProxyFactoryObject使用声明式事务 14.5.6. 通过...

    spring chm文档

    9.5.1. 理解Spring的声明式事务管理实现 9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. &lt;tx:advice/&gt; 有关的设置 9.5.6. 使用 @Transactional 9.5.7. 插入事务操作 9.5.8. ...

    Spring中文帮助文档

    9.5.1. 理解Spring的声明式事务管理实现 9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. &lt;tx:advice/&gt; 有关的设置 9.5.6. 使用 @Transactional 9.5.7. 事务传播 9.5.8. 通知...

    Spring.3.x企业应用开发实战(完整版).part2

    10.5.1 Spring事务管理器的应对 10.5.2 Hibernate+Spring JDBC混合框架的事务管理 10.6 特殊方法成漏网之鱼 10.6.1 哪些方法不能实施Spring AOP事务 10.6.2 事务增强遗漏实例 10.7 数据连接泄漏 10.7.1 底层连接资源...

    Spring API

    9.5.1. 理解Spring的声明式事务管理实现 9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. &lt;tx:advice/&gt; 有关的设置 9.5.6. 使用 @Transactional 9.5.7. 事务传播 9.5.8. 通知...

    Spring3.x企业应用开发实战(完整版) part1

    10.5.1 Spring事务管理器的应对 10.5.2 Hibernate+Spring JDBC混合框架的事务管理 10.6 特殊方法成漏网之鱼 10.6.1 哪些方法不能实施Spring AOP事务 10.6.2 事务增强遗漏实例 10.7 数据连接泄漏 10.7.1 底层连接资源...

    Spring面试题

    Spring AOP 模块为基于 Spring 的应用程序中的对象提供了事务管理服务。通过使用 Spring AOP,不用依赖 EJB 组件,就可以将声明性事务管理集成到应用程序中。 ☆ Spring DAO:JDBC DAO 抽象层提供了有意义的异常...

    OA系统的毕业设计的毕业论文

    5.4.5 配置DAO事务 43 6 系统测试 44 6.1 测试计划 44 6.2 测试用例 44 6.2.1 对身份验证功能进行测试 44 6.2.2 对职工信息管理功能进行测试 45 6.3 测试结果 46 7 系统开发总结 47 8 结束语 48 参考文献 49 致 谢 ...

    基于Spring + Spark商品大数据实时推荐系统

    通过学习Spring Boot,我了解了其核心思想和基本原理,以及如何构建RESTful Web服务、使用数据库、进行事务管理等。我学会了使用Spring Boot快速搭建Java Web应用程序,并且能够运用Spring Boot的特性来简化开发流程...

    springmybatis

    2. 设置mybatis 配置文件:Configuration.xml, 在src_user目录下建立此文件,内容如下: 程序代码 程序代码 &lt;!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" ...

    基于spark、mahout和spring boot构建的智能推荐系统.zip

    通过学习Spring Boot,我了解了其核心思想和基本原理,以及如何构建RESTful Web服务、使用数据库、进行事务管理等。我学会了使用Spring Boot快速搭建Java Web应用程序,并且能够运用Spring Boot的特性来简化开发流程...

    SpringBoot+mybatisPlus+atomikos+druid.zip

    Spring Boot:mybatis-plus + atomikos + druid 实现不同实例数据库的多数据源配置和分布式事务管理(demo项目),想到工作上可能会用到多数据源,但是自己在这方面并不是很熟悉,于是在网上查阅了很多文章,结果...

    Spring-Boot集成Neo4j结合Spark的朴素贝叶斯分类器实现基于电影知识图谱的智能问答系统.zip

    通过学习Spring Boot,我了解了其核心思想和基本原理,以及如何构建RESTful Web服务、使用数据库、进行事务管理等。我学会了使用Spring Boot快速搭建Java Web应用程序,并且能够运用Spring Boot的特性来简化开发流程...

    ssh(structs,spring,hibernate)框架中的上传下载

     由于Spring通过代理Hibernate完成数据层的操作,所以原Hibernate的配置文件hibernate.cfg.xml的信息也转移到Spring的配置文件中:  代码 4 Spring中有关Hibernate的配置信息 1. 2. !-- 数据源的配置 //--> 3. ...

Global site tag (gtag.js) - Google Analytics