Spring之事物处理
2018-03-22 21:51
302 查看
Spring之事物处理
事物分2种:
编程式事务控制自己手动控制事务,就叫做编程式事务控制。
Jdbc代码:
Conn.setAutoCommite(false); // 设置手动控制事务
Hibernate代码:
Session.beginTransaction(); // 开启一个事务 【细粒度的事务控制: 可以对指定的方法、指定的方法的某几行添加事务控制】 (比较灵活,但开发起来比较繁琐: 每次都要开启、提交、回滚.)
声明式事物控制
Spring提供了对事务的管理, 这个就叫声明式事务管理。
Spring提供了对事务控制的实现。用户如果想用Spring的声明式事务管理,只需要在配置文件中配置即可; 不想使用时直接移除配置。这个实现了对事务控制的最大程度的解耦。
Spring声明式事务管理,核心实现就是基于Aop。
【粗粒度的事务控制: 只能给整个方法应用事务,不可以对方法的某几行应用事务。】
(因为aop拦截的是方法。)
Spring声明式事务管理器类:
Jdbc技术:DataSourceTransactionManager Hibernate技术:HibernateTransactionManager
XML实现的方式:
模板<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> </beans>
C3P0的数据源:
<bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///ice?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"></property> <property name="user" value="root"></property> <property name="password" value="wen123"></property> <property name="initialPoolSize" value="3"></property> <property name="maxPoolSize" value="10"></property> <property name="maxStatements" value="100"></property> <property name="acquireIncrement" value="2"></property> </bean>
添加事物管理类
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="c3p0"></property> </bean>
配置事物管理类
<tx:advice transaction-manager="txManager" id="tx"> <tx:attributes > <tx:method name="get*" read-only="true" /> <tx:method name="*" read-only="false"/> </tx:attributes> </tx:advice><tx:advice transaction-manager="txManager" id="tx"> <tx:attributes > <tx:method name="get*" read-only="true" /> <tx:method name="*" read-only="false"/> </tx:attributes> </tx:advice>
创建Bean实例
<bean id="userDao" class="tx.Jdbc.UserDao"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <constructor-arg type="javax.sql.DataSource" ref="c3p0"></constructor-arg> </bean> <bean id="service" class="tx.Jdbc.ServiceAdmin"> <property name="userDao" ref="userDao"></property> </bean>
AOP配置
<aop:config proxy-target-class="true"> <aop:pointcut expression="execution(* tx.Jdbc.ServiceAdmin.*(..))" id="rf"/> <aop:advisor advice-ref="tx" pointcut-ref="rf"/> </aop:config>
注解
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> </beans>
C3P0的数据源:
C3P0的数据源: <bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///ice?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"></property> <property name="user" value="root"></property> <property name="password" value="wen123"></property> <property name="initialPoolSize" value="3"></property> <property name="maxPoolSize" value="10"></property> <property name="maxStatements" value="100"></property> <property name="acquireIncrement" value="2"></property> </bean>
添加事物管理类:
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="c3p0"></property> </bean>
JdbcTemplate工具类实例:
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="c3p0"></property> </bean>
注解扫描:
<context:component-scan base-package="auto.Jdbc"></context:component-scan>
事物注解声明:
<tx:annotation-driven transaction-manager="txManager"/>
UserDao
Service
Transactional注解是声明该类所有操控数据访问的事物
它可以在方法中使用
事物的属性说明:
@Transactional( readOnly = false, // 读写事务 timeout = -1, // 事务的超时时间不限制 noRollbackFor = ArithmeticException.class, // 遇到数学异常不回滚 isolation = Isolation.DEFAULT, // 事务的隔离级别,数据库的默认 propagation = Propagation.REQUIRED // 事务的传播行为 ) public void save(Dept dept){ deptDao.save(dept); int i = 1/0; deptDao.save(dept); }
事务传播行为:
Propagation.REQUIRED[/b]指定当前的方法必须在事务的环境下执行;如果当前运行的方法,已经存在事务, 就会加入当前的事务;
Propagation.REQUIRED_NEW
指定当前的方法必须在事务的环境下执行;如果当前运行的方法,已经存在事务: 事务会挂起; 会始终开启一个新的事务,执行完后; 刚才挂起的事务才继续运行。
举例:
Class Log{ Propagation.REQUIRED insertLog(); } Propagation.REQUIRED Void saveDept(){ insertLog(); // 加入当前事务 .. 异常, 会回滚 saveDept(); } Class Log{ Propagation.REQUIRED_NEW insertLog(); } Propagation.REQUIRED Void saveDept(){ insertLog(); // 始终开启事务 .. 异常, 日志不会回滚 saveDept(); }
相关文章推荐
- Spring+hibernate+mysql事物不回滚的原因以及处理
- Spring+hibernate+mysql事物不回滚的原因以及处理
- spring 事物处理机制
- Spring+hibernate+mysql事物不回滚的原因以及处理
- Spring事物处理
- Spring+hibernate+mysql事物不回滚的原因以及处理
- springmvc + mybatise 一个service 处理处理多个mapper 事物不回滚
- Spring+hibernate+mysql事物不回滚的原因以及处理
- Spring事物处理(代码实现)
- Spring-Java事物回滚失效处理
- spring事物处理异常
- Spring+hibernate+mysql事物不回滚的原因以及处理
- spring.net + nhibernate 多数据库的支持,以及跨数据库的事物处理的讨论
- spring三种事物处理方式
- Spring+hibernate+mysql事物不回滚的原因以及处理
- Spring+Mybatis+SpringMVC+Atomikos多数据源共存+不同数据库事物一致性处理
- Spring 事物处理
- spring事物处理
- spring, hibernate 事物处理,异常拦截
- spring中的事物处理1