1. spring对事务的支持
1.1 概念
n 事务是一组操作的执行单元,相对于数据库操作来讲,事务管理的是一组SQL指令,比如增加,修改,删除等,事务的一致性,要求,这个事务内的操作必须全部执行成功,如果在此过程种出现了差错,比如有一条SQL语句没有执行成功,那么这一组操作都将全部回滚
n 事务特性(ACID)
• Atomic(原子性):要么都成功,要么都失败
• Consistent(一致性):数据应该不被破坏
• Isolate(隔离性):用户间操作不相混淆
• Durable(持久性):永久保存
•
程序中两种事务管理方式
n 编程式事务管理
n 编写程序式的事务管理可以清楚的定义事务的边界,可以实现细粒度的事务控制,比如你可以通过程序代码来控制你的事务何时开始,何时结束等,与后面介绍的声明式事务管理相比,它可以实现细粒度的事务控制,例如jdbc,hibernate,spring中不提倡使用。
JDBC事务控制:
con.setAutoCommite(false); 设置事务手动提交
Hibernate中事务控制:
session.beginTransaction(); 开启事务
优缺点:
1. 事务控制精确
2. 事务代码,与业务逻辑处理代码,耦合在一起!
事务代码,不能共用! 重新写事务控制操作!
开发效率低,不便于维护! (不想用事务,要改代码!)
声明式事务管理 (在Spring中使用)
如果你并不需要细粒度的事务控制,你可以使用声明式事务,在Spring中,你只需要在Spring配置文件中做一些配置,即可将操作纳入到事务管理中,解除了和代码的耦合,这是对应用代码影响最小的选择,从这一点再次验证了Spring关于AOP的概念。当你不需要事务管理的时候,可以直接从Spring配置文件中移除该设置
特点:
1. Spring提供的声明式事务管理,用到Aop概念!
2. 对指定的方法添加事务控制,这里只需要配置即可!
3. 修改事务控制实现或删除事务控制操作,只需要移除xml事务相关配置!
注意:
只能对某个方法应用事务! (因为“切入点表达式”拦截的是方法,控制不了方法内部代码!)
所以,Spring声明式事务管理,即为粗粒度的事务控制!
声明式事务管理器类:(PlatformTransactionManager
)
Jdbc:
DataSourceTransactionManager 管理jdbc中事务控制
Hibernate:
HibenateTransactionManager 管理hibernate中事务控制
1.2 spring管理事务—DataSourceTransactionManager
1.2.1 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:p="http://www.springframework.org/schema/p"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 1. 数据源配置 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///hib_demo"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="6"></property>
</bean>
<!-- 2. JdbcTemplate配置 , 注入数据源-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 3. dao实例,注入jdbcTemplate -->
<bean id="deptDao" class="cn.***.a_tx_jdbc.DeptDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<!-- 4. Service实例,注入dao实例 -->
<bean id="deptService" class="cn.***.a_tx_jdbc.DeptService">
<property name="deptDao" ref="deptDao"></property>
</bean>
<!-- 5. Spring声明式事务管理配置 -->
<!-- 5.1 配置事务管理器类 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 5.2 事务通知配置, 拦截到指定的方法后如何管理事务 -->
<!-- find* find开头的方法,是只读的事务 -->
<!-- * 上面所有的方法都不满足时候,采用的事务控制规则 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="*" read-only="false"/>
</tx:attributes>
</tx:advice>
<!-- 5.3 事务Aop配置 = 切入点表达式 + 应用上面的事务通知 -->
<aop:config>
<aop:pointcut expression="execution(* cn.***.a_tx_jdbc.*Service.*(..))" id="pt"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
</aop:config>
</beans>
1.2.2 注解方式实现
步骤:
1.引入aop相关包
2.开启事务注解
<tx:annotation-driven transaction-manager="txManager"/>
3.使用@Transactional 注解
在需要添加事务控制的方法上写这个注解
@Transactional
写到方法上, 表示当前方法应用事务控制
写到类上, 表示当前类的所有方法都会应用事务
写到父类上, 当执行父类的这个方法时候才应用事务!
事务属性
// 当前方法应用事务
@Transactional(
readOnly=false, // 读写的事务,当修改数据时候用;如果查询就设置为true
isolation=Isolation.DEFAULT, // 事务隔离级别
timeout=-1, // 事务执行的超时时间, -1 表示不超时
noRollbackFor=ArithmeticException.class, // 遇到指定的异常不回滚
propagation=Propagation.REQUIRES_NEW // 事务传播行为
)
1.3 Hibernate 管理事务---HibernateTransactionManager
Spring与hibernate的整合: 在spring中使用HibernateTransactionManager来管理事务
1. 数据库连接池: java.sql.DataSource
2. 配置sessionFactory,以及相关hibernate配置在LocalSessionFactoryBean类里(设置hibernate相关配置获取sessionFactory)
3. 将配置好的sessionFactory注入到HibenateTransactionManager(配置事务管理器)
Xml:
4. 配置Aop 的pointCut
5. <tx: advice >配置事务传播属性和事务隔离级别等。
注解:
4.<aop:aspectj-autoproxy></aop:aspectj-autoproxy>: 开启Aop注解 @Before( )…
5.<tx:annotation-driventransaction-manager="transactionManager"/>: 开启事务管理。@Transactional( )
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 开启注解扫描 -->
<context:component-scanbase-package="packageName"></context:component-scan>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/student_web"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 引入数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- hibernate相关配置 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
</props>
</property>
<!-- 开启实体注解扫描 -->
<property name="packagesToScan">
<list>
<value>packageName</value>
</list>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 采用声明式容器管理事务,一般在service配置 -->
<aop:config>
<aop:pointcut expression="execution(* com.test.student.service.impl.*.*(..))" id="txPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
<!-- 事务 -->
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update" propagation="REQUIRED"/>
<tx:method name="remove" propagation="REQUIRED"/>
<tx:method name="find" read-only="true"/>
<tx:method name="list" read-only="true"/>
<tx:method name="get" read-only="true"/>
</tx:attributes>
</tx:advice></beans>
</beans>
1.4 传播属性(Propagation)
Propagation: key属性确定代理应该给哪个方法增加事务行为。这样的属性最重要的部分是传播行为。PROPAGATION_REQUIRED最为常见。
PROPAGATION_REQUIRED---支持当前事务,如果当前没有事务,就新建一个事务。
PROPAGATION_REQUIRED_NEW---新建事务,如果当前存在事务,就把当前事务挂起。
PROPAGATION_NOT_SUPPORTED---以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
PROPAGATION_SUPPORTS---支持当前事务,如果当前没有事务,就以非事务方式执行。
PROPAGATION_MANDATORY---支持当前事务,如果当前没有事务,就抛出异常。
PROPAGATION_NEVER---以非事务方式执行,如果当前存在事务,则抛出异常。