Spring事务管理的三种方式

一 、第一种:全注解声明式事务 

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:tx="http://www.springframework.org/schema/tx"
6.       xmlns:aop="http://www.springframework.org/schema/aop"
7.       xmlns:context="http://www.springframework.org/schema/context"
8.       xsi:schemaLocation="http://www.springframework.org/schema/beans
9.       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
10.       http://www.springframework.org/schema/tx
11.       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
12.       http://www.springframework.org/schema/aop
13.       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
14.       http://www.springframework.org/schema/context
15.       http://www.springframework.org/schema/context/spring-context-2.5.xsd"
16.       default-init-method="init">
17.
18.    <!-- 引入jdbc配置文件 -->
19.    <context:property-placeholder location="classpath:jdbc.properties" />
20.
21.    <!--创建jdbc数据源 -->
22.    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
23.        <property name="driverClassName" value="${driver}" />
24.        <property name="url" value="${url}" />
25.        <property name="username" value="${username}" />
26.        <property name="password" value="${password}" />
27.    </bean>
28.
29.    <!-- 创建SqlSessionFactory,同时指定数据源 -->
30.    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
31.        <property name="dataSource" ref="dataSource" />
32.        <!-- 配置sql映射文件所在位置  注意:默认与mapper类位置相同   -->
33.        <property name="mapperLocations" value="classpath:sqlmap/*.xml" />
34.    </bean>
35.
36.    <!-- 配置事务管理器:第一种 全注解声明式事务  -->
37.    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
38.        <property name="dataSource" ref="dataSource" />
39.    </bean>
40.    <tx:annotation-driven transaction-manager="transactionManager" />
41.    <!-- 第一种 结束位置 -->
42.    <!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->
43.    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
44.        <property name="basePackage" value="com.xieke.test.mapper" />
45.        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
46.        <!-- 默认情况下会自动注入id=sqlSessionFactory的bean,也可以按上述方式指定sqlSessionFactory -->
47.    </bean>
48.
49.</beans>  

   使用时:在需要事务控制的类上加上@Transactional注解就可以了.

   二、第二种:使用tx标签配置的拦截器声明式事务

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:tx="http://www.springframework.org/schema/tx"
6.       xmlns:aop="http://www.springframework.org/schema/aop"
7.       xmlns:context="http://www.springframework.org/schema/context"
8.       xsi:schemaLocation="http://www.springframework.org/schema/beans
9.       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
10.       http://www.springframework.org/schema/tx
11.       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
12.       http://www.springframework.org/schema/aop
13.       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
14.       http://www.springframework.org/schema/context
15.       http://www.springframework.org/schema/context/spring-context-2.5.xsd"
16.       default-init-method="init">
17.
18.    <!-- 引入jdbc配置文件 -->
19.    <context:property-placeholder location="classpath:jdbc.properties" />
20.
21.    <!--创建jdbc数据源 -->
22.    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
23.        <property name="driverClassName" value="${driver}" />
24.        <property name="url" value="${url}" />
25.        <property name="username" value="${username}" />
26.        <property name="password" value="${password}" />
27.    </bean>
28.
29.    <!-- 创建SqlSessionFactory,同时指定数据源 -->
30.    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
31.        <property name="dataSource" ref="dataSource" />
32.        <!-- 配置sql映射文件所在位置  注意:默认与mapper类位置相同   -->
33.        <property name="mapperLocations" value="classpath:sqlmap/*.xml" />
34.    </bean>
35.
36.    <!-- 配置事务管理器:第二种 使用tx标签配置的拦截器声明式事务  -->
37.    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
38.        <property name="dataSource" ref="dataSource" />
39.    </bean>
40.    <!-- 配置事务的传播特性  -->
41.    <tx:advice id="txAdvice" transaction-manager="transactionManager">
42.        <tx:attributes>
43.            <tx:method name="add*" propagation="NESTED" />
44.            <tx:method name="del*" propagation="NESTED" />
45.            <tx:method name="*" read-only="true" />
46.        </tx:attributes>
47.    </tx:advice>
48.    <!-- 配置事务的切入点  -->
49.    <aop:config>
50.        <aop:pointcut id="targetMethod" expression="execution(* com.xieke.test.service.impl.*.*(..))" />
51.        <aop:advisor advice-ref="txAdvice" pointcut-ref="targetMethod" />
52.    </aop:config>
53.    <!-- 第二种 结束结束位置 -->
54.
55.    <!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->
56.    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
57.        <property name="basePackage" value="com.xieke.test.mapper" />
58.        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
59.        <!-- 默认情况下会自动注入id=sqlSessionFactory的bean,也可以按上述方式指定sqlSessionFactory -->
60.    </bean>
61.
62.</beans>    

    三、 第三种:使用拦截器声明式事务 

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:tx="http://www.springframework.org/schema/tx"
6.       xmlns:aop="http://www.springframework.org/schema/aop"
7.       xmlns:context="http://www.springframework.org/schema/context"
8.       xsi:schemaLocation="http://www.springframework.org/schema/beans
9.       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
10.       http://www.springframework.org/schema/tx
11.       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
12.       http://www.springframework.org/schema/aop
13.       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
14.       http://www.springframework.org/schema/context
15.       http://www.springframework.org/schema/context/spring-context-2.5.xsd"
16.       default-init-method="init">
17.
18.    <!-- 引入jdbc配置文件 -->
19.    <context:property-placeholder location="classpath:jdbc.properties" />
20.
21.    <!--创建jdbc数据源 -->
22.    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
23.        <property name="driverClassName" value="${driver}" />
24.        <property name="url" value="${url}" />
25.        <property name="username" value="${username}" />
26.        <property name="password" value="${password}" />
27.    </bean>
28.
29.    <!-- 创建SqlSessionFactory,同时指定数据源 -->
30.    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
31.        <property name="dataSource" ref="dataSource" />
32.        <!-- 配置sql映射文件所在位置  注意:默认与mapper类位置相同   -->
33.        <property name="mapperLocations" value="classpath:sqlmap/*.xml" />
34.    </bean>
35.
36.    <!-- 配置事务管理器:第三种 使用拦截器声明式事务  -->
37.    <bean id="transactionManager"
38.        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
39.        <property name="dataSource" ref="dataSource" />
40.    </bean>
41.    <bean id="transactionInterceptor"
42.        class="org.springframework.transaction.interceptor.TransactionInterceptor">
43.        <property name="transactionManager" ref="transactionManager" />
44.        <!-- 配置事务属性   -->
45.        <property name="transactionAttributes">
46.            <props>
47.                <!-- PROPAGATION_REQUIRED:支持当前事务,如果当前没有事务,则新建一个事务    -->
48.                <prop key="add*">PROPAGATION_REQUIRED</prop>
49.                <prop key="del*">PROPAGATION_REQUIRED</prop>
50.            </props>
51.        </property>
52.    </bean>
53.    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
54.        <property name="beanNames">
55.            <list>
56.                <value>*ServiceImpl</value>
57.            </list>
58.        </property>
59.        <property name="interceptorNames">
60.            <list>
61.                <value>transactionInterceptor</value>
62.            </list>
63.        </property>
64.    </bean>
65.    <!-- 第三种 结束位置 -->
66.
67.    <!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->
68.    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
69.        <property name="basePackage" value="com.xieke.test.mapper" />
70.        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
71.        <!-- 默认情况下会自动注入id=sqlSessionFactory的bean,也可以按上述方式指定sqlSessionFactory -->
72.    </bean>
73.
74.</beans> 

Spring事务的传播属性:
下载地址   
PROPAGATION_REQUIRED -- 支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
PROPAGATION_SUPPORTS -- 支持当前事务,如果当前没有事务,就以非事务方式执行。
PROPAGATION_MANDATORY -- 支持当前事务,如果当前没有事务,就抛出异常。
PROPAGATION_REQUIRES_NEW -- 新建事务,如果当前存在事务,把当前事务挂起。
PROPAGATION_NOT_SUPPORTED -- 以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
PROPAGATION_NEVER -- 以非事务方式执行,如果当前存在事务,则抛出异常。
PROPAGATION_NESTED -- 如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则进行与PROPAGATION_REQUIRED类似的操作。

时间: 2024-12-11 16:59:39

Spring事务管理的三种方式的相关文章

配置spring事务管理的几种方式

Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分. DataSource.TransactionManager这两部分只是会根据数据访问方式有所变化,比如使用Hibernate进行数据访问时,DataSource实际为SessionFactory,TransactionManager的实现为HibernateTransactionManager. 具体如下图:

Spring事务管理之几种方式实现事务(转)

一:事务认识 大家所了解的事务Transaction,它是一些列严密操作动作,要么都操作完成,要么都回滚撤销.Spring事务管理基于底层数据库本身的事务处理机制.数据库事务的基础,是掌握Spring事务管理的基础.这篇总结下Spring事务. 事务具备ACID四种特性,ACID是Atomic(原子性).Consistency(一致性).Isolation(隔离性)和Durability(持久性)的英文缩写. (1)原子性(Atomicity) 事务最基本的操作单元,要么全部成功,要么全部失败,

Spring事务管理之几种方式实现事务

也可以参考https://blog.csdn.net/liuhaiabc/article/details/52450167 https://blog.csdn.net/baidu_37107022/article/details/77481670 数据库事务的隔离级别有4个,由低到高依次为Read uncommitted.Read committed.Repeatable read.Serializable,这四个级别可以逐个解决脏读.不可重复读.幻读这几类问题. √: 可能出现    ×: 不

Spring事务管理的两种方式

参考文档: http://www.iteye.com/topic/1123347 http://blog.csdn.net/lcj8/article/details/2835432 PS:好像还是tx标签的方式较为简单 tx标签设置拦截器: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans&quo

spring的事务管理有几种方式实现 (转自:http://blog.csdn.net/bopzhou/article/details/7094108)

spring的事务管理有几种方式实现 标签: springhibernate数据库beanlistclass 2011-12-22 09:12 3883人阅读 评论(0) 收藏 举报 Spring+Hibernate的实质: 就是把Hibernate用到的数据源Datasource,Hibernate的SessionFactory实例,事务管理器HibernateTransactionManager,都交给Spring管理. 那么再没整合之前Hibernate是如何实现事务管理的呢? 通过Ser

Spring事务配置的五种方式

Spring事务配置的五种方式 前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识.通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的. 总结如下: Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分. DataSource.TransactionManager这两部分

Spring事务配置的五种方式和spring里面事务的传播属性和事务隔离级别

转: http://blog.csdn.net/it_man/article/details/5074371 Spring事务配置的五种方式 前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识.通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的. 总结如下: Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪

Spring事务配置的五种方式(转)

Spring事务配置的五种方式 前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识.通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的. 总结如下: Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分. DataSource.TransactionManager这两部分

【转】 Spring事务配置的五种方式和spring里面事务的传播属性和事务隔离级别

spring事务配置的五种方式 前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识.通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的. 总结如下: Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分. DataSource.TransactionManager这两部分