`
zhanghongliang_cyj
  • 浏览: 49155 次
  • 性别: Icon_minigender_1
  • 来自: 邯郸
社区版块
存档分类
最新评论

spring事务处理

阅读更多
<?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&amp;useUnicode=true&amp;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 拦截的包可以是重复的,方便异常时回滚

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics