<?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: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">
<!-- 引进src 下的properties配置文件 -->
<context:property-placeholder location="classpath:cn/test2/base10_transaction/jdbc.properties" />
<context:component-scan base-package="cn.goso.twoSessions" />
<!-- 数据源管理 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<!--可以这样从properties文件中取参数-->
<property name="driverClass" value="${driverClassName}" />
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=utf8" />
<property name="user" value="root" />
<property name="password" value="123456" />
<!-- 初始化时获得的连接数,取值应在minPoolSize和maxPoolSize之间,默认是3 -->
<property name="initialPoolSize" value="1" />
<!-- 连接池中保留的最小连接数 -->
<property name="minPoolSize" value="1" />
<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="60" />
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="15" />
<!-- 当连接池耗尽的时候c3p0一次性获取的连接数, 默认是3 -->
<property name="acquireIncrement" value="5" />
<!-- 隔多久检查连接池中的空闲连接 ,默认是0 -->
<property name="idleConnectionTestPeriod" value="60" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<!--spring事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!--
<tx:annotation-driven transaction-manager="txManager" />
-->
<tx:advice id="txAdvice" transaction-manager=" txManager ">
<tx:attributes>
<!-- get方法不走事务
<tx:method name="get*,find*" read-only="true" propagation="NOT_SUPPORTED"/>
-->
<!-- 支持当前事务,如果当前没有事务,就新建一个事务
*表示所有方法
-->
<tx:method name="*" rollback-for="Exception" isolation="DEFAULT" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!-- ..表示cn 下所有包所有类 * 表示所有方法名称(..) 表示所有方法参数
可以cn..service.impl..*(..) 表示
-->
<aop:pointcut id="transactionPointcut" expression="execution(* cn..*(..))" />
<aop:advisor advice-ref=" txAdvice " pointcut-ref="transactionPointcut" />
</aop:config>
</beans>
1、Spring中常用事务类型: 用于配置在<tx:method /> 的propagation 属性下
.PROPAGATION_REQUIRED--支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
• PROPAGATION_SUPPORTS--支持当前事务,如果当前没有事务,就以非事务方式执行。
• PROPAGATION_MANDATORY--支持当前事务,如果当前没有事务,就抛出异常。
• PROPAGATION_REQUIRES_NEW--新建事务,如果当前存在事务,把当前事务挂起。
• PROPAGATION_NOT_SUPPORTED--以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
• PROPAGATION_NEVER--以非事务方式执行,如果当前存在事务,则抛出异常。
• PROPAGATION_NESTED--如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则进行与PROPAGATION_REQUIRED类似的操作。
2、<tx:advice/> 有关的设置
通过 <tx:advice/> 标签来指定不同的事务性设置。默认的 <tx:advice/> 设置如下: /
• 事务传播设置是 REQUIRED
• 隔离级别是 DEFAULT
• 事务是 读/写
• 事务超时默认是依赖于事务系统的,或者事务超时没有被支持。
• 任何 RuntimeException 将触发事务回滚,但是任何 checked Exception 将不触发事务回滚
这些默认的设置当然也是可以被改变的。 <tx:advice/> 和 <tx:attributes/> 标签里的 <tx:method/> 各种属性设置总结如下:
<tx:method/> 有关的设置
属性 是否需要? 默认值 描述
---------------------------------------------------------------------------------------------------
name 是 与事务属性关联的方法名。通配符(*)可以用来指定一批关联到相同的事务属性的方法。 如:'get*'、'handle*'、'on*Event'等等。
---------------------------------------------------------------------------------------------------
propagation 不 REQUIRED 事务传播行为
---------------------------------------------------------------------------------------------------
isolation 不 DEFAULT 事务隔离级别
---------------------------------------------------------------------------------------------------
timeout 不 -1 事务超时的时间(以秒为单位)
---------------------------------------------------------------------------------------------------
read-only 不 false 事务是否只读?
---------------------------------------------------------------------------------------------------
rollback-for 不 将被触发进行回滚的 Exception(s);以逗号分开。 如:'com.foo.MyBusinessException,ServletException'
----------------------------------------------------------------------------------------------
no-rollback-for 不 不 被触发进行回滚的 Exception(s);以逗号分开。 如:'com.foo.MyBusinessException,ServletException'
-------------------------------------------------------------------------------------------------
isolation 不 DEFAULT 数据库提供了四种事务隔离级别
3、数据可以事务的隔离级别
# ISOLATION_DEFAULT 这是一个PlatfromTransactionManager默认的隔离级别,使用数据库默认的事务隔离级别.另外四个与JDBC的隔离级别相对应
# ISOLATION_READ_UNCOMMITTED 这是事务最低的隔离级别,它充许别外一个事务可以看到这个事务未提交的数据。这种隔离级别会产生脏读,不可重复读和幻像读。
# ISOLATION_READ_COMMITTED 保证一个事务修改的数据提交后才能被另外一个事务读取。另外一个事务不能读取该事务未提交的数据。这种事务隔离级别可以避免脏读出现,但是可能会出现不可重复读和幻像读。
# ISOLATION_REPEATABLE_READ 这种事务隔离级别可以防止脏读,不可重复读。但是可能出现幻像读。它除了保证一个事务不能读取另一个事务未提交的数据外,还保证了避免下面的情况产生(不可重复读)。
# ISOLATION_SERIALIZABLE 这是花费最高代价但是最可靠的事务隔离级别。事务被处理为顺序执行。除了防止脏读,不可重复读外,还避免了幻像读。
4、可以配置多个数据源,控制事务。需清除,配置的
<aop:pointcut id="transactionPointcut" expression="execution(* cn..*(..))" />
要拦截可以抛出异常的类。
***
需注意:
txManager配置的多个名称不可以相同,包括txAdvice名称也不可用相同,但是配置的aop 拦截的包可以是重复的,方便异常时回滚
分享到:
相关推荐
spring 事务处理
aop与spring事务处理
主要介绍了Spring事务处理原理步骤详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
NULL 博文链接:https://yizhenn.iteye.com/blog/2293339
针对Java Spring 框架的核心技术场景案例,AOP和事务处理部分的应用案例,涉及业务层接口万次执行效率,百度网盘密码数据兼容处理和转账业务追加日志三个案例
事务的4大特性 原子性(Atomicity) 一个事务要么全部提交成功,要么全部失败回滚,不能只执行其中的一部分操作。 一致性(Consistency) 一旦事务完成(不管成功还是失败),系统必须确保涉及的数据处于一致的状态,而...
事务简介 二、程序举例环境搭建 o1. 创建数据表 ...三、使用 Spring 的事务注解管理事务 o1. 声明事务管理器 o2. 开启注解驱动 o3. 完整Spring配置文件 o4. 业务层 public 方法加入事务属性 o5. 测试
spring-tx spring-transaction spring事务管理实例
spring声明式事务处理demo。myeclipse工程
主要介绍了Spring事务处理流程和原理详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Spring Hibernate 事务处理 详细说明 Spring Hibernate 事务处理 详细说明
这是在java里使用到spring的配置文件里,添加事务处理过程,以至于可以回滚事务,当中使用到拦截器。
1.1 Spring事务处理时自我调用的解决方案及一些实现方式的风险 1.2 我对AOP的理解 1.3 Spring开闭原则的表现-BeanPostProcessor的扩展点-1 1.4 我对IoC/DI的理解 1.5 SpringMVC + spring3.1.1 + hibernate4.1.0 集成...
spring整合hibernate实现事务处理 1.spring整合hibernate实现事务处理-注解方式 2.spring整合hibernate实现事务处理-XML方式
自己整理的spring事务处理文档,自我感觉比较全面。呵呵。
Spring事务管理4种方式 入门级 最简单demo PlatformTransactionManager TransactionTemplate
Spring源代码解析(六):Spring声明式事务处理.doc
NULL 博文链接:https://todd-liangt.iteye.com/blog/337274