18.Spring事务管理机制(事务处理交给spring的aop处理)

真正执行增删改查操作的方法是主要功能,在他们之前/之后开启事务,提交事务,回滚事务,把抽离出来的事务管理代码动态的植入到需要事务处理的service方法上

事务管理怎样做:
	1.找到切入点,用aop专属标签提供的execution指明哪些方法需要被织入
	2.创建事务代码(切面类),我们不需要写,正好是由spring提供,称为事务管理器
	3.使用增强器advisor,是由spring提供,称为事务拦截器
1)编程式事务管理(不常用)
	所谓编程式事务指的是通过编码方式实现事务。
2)声明式事务管理(常用)
在Spring配置文件中声明式的处理事务来代替代码式的处理事务
在spring中,声明式事务主要是通过【事务属性】来定义的,事务属性描述了事务策略如何应用到方法上面

声明式事务管理的配置方式通常以下几种:
注意:配置事务的方式都需要用到事务管理器(切面)和事务拦截器(advice),其实就是使用aop编程,把事务代码动态织入到需要使用的方法上

在这里插入图片描述

如何完成service层对事物进行操作?
1.spring+jdbc  事务配置在service方法上
	1.tx前缀的事务标签和aop前缀的标签结合,将切面(事务管理器)织入到切入点上
	2.使用注解进行事务配置
2.spring+mybatics  事务配置在service方法上
	1.tx前缀的事务标签和aop前缀的标签结合,将切面(事务管理器)织入到切入点上
	2.使用注解进行事务配置

spring+jdbc

1.aop标签结合tx标签

/*************************接口AccountService *******************/
public interface AccountService {
	void add(Account account);
	void update(Account account);
	void remove(Account account);
}
/*************AccountServiceImpl实现接口AccountService **********/
public class AccountServiceImpl implements AccountService{
	
	private AccountDao accountDao;
	
	public AccountDao getAccountDao() {
		return accountDao;
	}

	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}
	
	@Override
	public void add(Account account) {
		accountDao.save(account);
//		throw new RuntimeException("事务测试");
	}

	@Override
	public void update(Account account) {
		
	}

	@Override
	public void remove(Account account) {
		
	}
}

spring_mybatis.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: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-3.2.xsd
   		   http://www.springframework.org/schema/context 
   		   http://www.springframework.org/schema/context/spring-context-3.2.xsd
   		   http://www.springframework.org/schema/aop 
   		   http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
   		   http://www.springframework.org/schema/tx 
   		   http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">


	<!-- 配置数据源 -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName">
			<value>oracle.jdbc.driver.OracleDriver</value>
		</property>
		<property name="url">
			<value>jdbc:oracle:thin:@localhost:1521:XE</value>
		</property>
		<property name="username">
			<value>cao</value>
		</property>
		<property name="password">
			<value>cao</value>
		</property>
	</bean>

	<!-- 配置sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="typeAliasesPackage" value="com.briup.tran"></property>
        <property name="configurationProperties">
            <props>
                <prop key="cacheEnabled">true</prop> 
            </props>
        </property>
        <!-- 自动扫描mapping.xml文件 -->
        <property name="mapperLocations" value="classpath:com/briup/tran/mybatis/AccountMapper.xml" />
    </bean>	
    
    <!-- 自动扫描映射接口所在的包 -->
    <!-- 将来可以通过接口的名字首字母小写作为beanName,从spring容器中拿出自动生成的该接口的实现类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.briup.tran.dao" />
    </bean>

</beans>

jdbc_service1.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: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-3.2.xsd
   		   http://www.springframework.org/schema/context 
   		   http://www.springframework.org/schema/context/spring-context-3.2.xsd
   		   http://www.springframework.org/schema/aop 
   		   http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
   		   http://www.springframework.org/schema/tx 
   		   http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
   
   
    <!-- 配置service层对象 目标对象-->
	<bean name="service" class="com.briup.tran.service.AccountServiceImpl">
		<!-- 注入dao层对象 -->
		<property name="accountDao" ref="accountDao"></property>
	</bean>
	
	
	<!-- 配置jdbc的事务管理器 (切面类)-->
	<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 注入数据源  dataSource在spring_jdbc.xml-->
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- 配置事务拦截器(通知) -->
	<tx:advice id="transactionInterceptor" transaction-manager="transactionManager">
		<!-- 设置事务的属性 -->
		<tx:attributes>
			<!-- *代表所有的方法都要被拦截 -->
			<!-- 回滚产生一个Throwable对象 -->
			<tx:method name="*" propagation="REQUIRED" rollback-for="Throwable"/>
		</tx:attributes>
	</tx:advice>
	
	
	<!-- 配置aop -->
	<aop:config>
		<!-- 配置切入点 -->
		<aop:pointcut expression="execution(public * com.briup.tran.service.*.*(..))" id="myPointCut"/>
		<!-- 配置事务拦截器在哪一个切入点上起作用 -->
		<aop:advisor advice-ref="transactionInterceptor" pointcut-ref="myPointCut"/>
	</aop:config>
   
</beans>

测试:

@Test
public void transaction_jdbc(){
	try {
		//同时加载两个xml文件
		String path[] = {"com/briup/tran/service/jdbc_service1.xml"};
		ApplicationContext container = new ClassPathXmlApplicationContext(path);
		
		AccountService service = (AccountService)container.getBean("service");
		System.out.println(service.getClass());
		service.add(new Account(3,"tom3",3000));
		
	}catch(Exception e) {
		e.printStackTrace();
	}
}

2.注解方式

jdbc_service2.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: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-3.2.xsd
   		   http://www.springframework.org/schema/context 
   		   http://www.springframework.org/schema/context/spring-context-3.2.xsd
   		   http://www.springframework.org/schema/aop 
   		   http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
   		   http://www.springframework.org/schema/tx 
   		   http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
   
   	
    <!-- 配置service层对象 目标对象-->
	<bean name="service" class="com.briup.tran.service.AccountServiceImpl">
		<!-- 注入dao层对象 -->
		<property name="accountDao" ref="accountDao"></property>
	</bean>
	
	<!-- 配置jdbc的事务管理器 (切面类)-->
	<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 注入数据源 -->
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- 通知spring我们在目标对象中做了事务的注解,并指明使用哪一个事务管理器 -->
	<!-- 加入上这个标签后 去目标对象去加入相应的注解就可以了 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
   
</beans>

AccountServiceImpl.java,在类上面加上@Transactional(处理级别,回滚)

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.briup.tran.Account;
import com.briup.tran.dao.AccountDao;

@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Throwable.class)
public class AccountServiceImpl implements AccountService{
	
	private AccountDao accountDao;
	
	public AccountDao getAccountDao() {
		return accountDao;
	}

	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}
	
	@Override
	public void add(Account account) {
		accountDao.save(account);
//		throw new RuntimeException("事务测试");
	}

	@Override
	public void update(Account account) {
		
	}

	@Override
	public void remove(Account account) {
		
	}

}

测试:

@Test
public void transaction_jdbc(){
	try {
		//同时加载两个xml文件
		String path[] = {"com/briup/tran/jdbc/spring_jdbc.xml",
		 	"com/briup/tran/service/jdbc_service2.xml"};
		ApplicationContext container = new ClassPathXmlApplicationContext(path);
		
		AccountService service = (AccountService)container.getBean("service");
		System.out.println(service.getClass());
		service.add(new Account(2,"tom3",3000));
		
	}catch(Exception e) {
		e.printStackTrace();
	}
}

spring+mybatics

1.aop标签结合tx标签

mybatis_service1.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: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-3.2.xsd
   		   http://www.springframework.org/schema/context 
   		   http://www.springframework.org/schema/context/spring-context-3.2.xsd
   		   http://www.springframework.org/schema/aop 
   		   http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
   		   http://www.springframework.org/schema/tx 
   		   http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
   		   
   	
   	
    <!-- 配置service层对象 目标对象-->
	<bean name="service" class="com.briup.tran.service.AccountServiceImpl">
		<!-- 注入dao层对象 -->
		<property name="accountDao" ref="accountDao"></property>
	</bean>
	
	<!-- 配置jdbc的事务管理器 (切面类) 适用于mybatis-->
	<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 注入数据源 -->
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- 配置事务拦截器 -->
	<tx:advice id="transactionInterceptor" transaction-manager="transactionManager">
		<tx:attributes>
			<!-- *代表所有的方法 -->
			<tx:method name="*" propagation="REQUIRED" rollback-for="Throwable"/>
		</tx:attributes>
	</tx:advice>
	
	<!-- 配置aop -->
	<aop:config>
		<!-- 配置切入点 -->
		<aop:pointcut expression="execution(public * com.briup.tran.service.*.*(..))" id="myPointCut"/>
		<!-- 配置事务拦截器在哪一个切入点上起作用 -->
		<aop:advisor advice-ref="transactionInterceptor" pointcut-ref="myPointCut"/>
	</aop:config>
   
</beans>

测试:

@Test
public void transaction_mybatis(){
	try {
		String path[] = {"com/briup/tran/mybatis/spring_mybatis.xml",
				"com/briup/tran/service/mybatis_service1.xml"};
		ApplicationContext container = 
				new ClassPathXmlApplicationContext(path);
		
		AccountService service = (AccountService)container.getBean("service");
//			System.out.println(service.getClass());
		service.add(new Account(5,"tom4",5000));
	}catch(Exception e) {
		e.printStackTrace();
	}
}

2.注解方式

mybatis_service2.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: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-3.2.xsd
   		   http://www.springframework.org/schema/context 
   		   http://www.springframework.org/schema/context/spring-context-3.2.xsd
   		   http://www.springframework.org/schema/aop 
   		   http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
   		   http://www.springframework.org/schema/tx 
   		   http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
	

	<!-- 配置service层对象 目标对象 -->
	<bean name="service" class="com.briup.tran.service.AccountServiceImpl">
		<!-- 注入dao层对象 -->
		<property name="accountDao" ref="accountDao"></property>
	</bean>

	<!-- 配置jdbc的事务管理器 (切面类) 适用于mybatis -->
	<bean name="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 注入数据源 -->
		<property name="dataSource" ref="dataSource"></property>
	</bean>

	<!-- 通知spring我们在目标对象中做了事务的注解,并指明使用哪一个事务管理器 -->
	<!-- 加入上这个标签后 去目标对象去加入相应的注解就可以了 -->
	<tx:annotation-driven transaction-manager="transactionManager" />

</beans>
@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Throwable.class)
public class AccountServiceImpl implements AccountService{
	
	private AccountDao accountDao;
	
	public AccountDao getAccountDao() {
		return accountDao;
	}

	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}
	
	@Override
	public void add(Account account) {
		accountDao.save(account);
//		throw new RuntimeException("事务测试");
	}

	@Override
	public void update(Account account) {
		
	}

	@Override
	public void remove(Account account) {
		
	}

}

测试:

@Test
public void transaction_mybatis(){
	try {
		String path[] = {"com/briup/tran/mybatis/spring_mybatis.xml",
				"com/briup/tran/service/mybatis_service2.xml"};
		ApplicationContext container = 
				new ClassPathXmlApplicationContext(path);
		
		AccountService service = (AccountService)container.getBean("service");
//			System.out.println(service.getClass());
		service.add(new Account(6,"tom4",5000));
	}catch(Exception e) {
		e.printStackTrace();
	}
}
了解事务属性包含的五个方面分别是什么:

1)事务传播行为
规定了如果有新的事务应该被启动还是被挂起,或者方法是否需要在事务中运行。

TransactionDefinition.PROPAGATION_REQUIRED:如果当前存在事务,则加入该事务;如果当前没有事务,则创建一个新的事务。
TransactionDefinition.PROPAGATION_REQUIRES_NEW:创建一个新的事务,如果当前存在事务,则把当前事务挂起。
TransactionDefinition.PROPAGATION_SUPPORTS:如果当前存在事务,则加入该事务;如果当前没有事务,则以非事务的方式继续运行。
TransactionDefinition.PROPAGATION_NOT_SUPPORTED:以非事务方式运行,如果当前存在事务,则把当前事务挂起。
TransactionDefinition.PROPAGATION_NEVER:以非事务方式运行,如果当前存在事务,则抛出异常。
TransactionDefinition.PROPAGATION_MANDATORY:如果当前存在事务,则加入该事务;如果当前没有事务,则抛出异常。
TransactionDefinition.PROPAGATION_NESTED:如果当前存在事务,则创建一个事务作为当前事务的嵌套事务来运行;如果当前没有事务,则该取值等价于TransactionDefinition.PROPAGATION_REQUIRED。



2)事务隔离级别
定义了一个事务可能受其他并发事务影响的程度。
隔离级别是指若干个并发的事务之间的隔离程度。TransactionDefinition 接口中定义了五个表示隔离级别的常量:

TransactionDefinition.ISOLATION_DEFAULT:这是默认值,表示使用底层数据库的默认隔离级别。对大部分数据库而言,通常这值就是TransactionDefinition.ISOLATION_READ_COMMITTED。

TransactionDefinition.ISOLATION_READ_UNCOMMITTED:该隔离级别表示一个事务可以读取另一个事务修改但还没有提交的数据。该级别不能防止脏读和不可重复读,因此很少使用该隔离级别。
TransactionDefinition.ISOLATION_READ_COMMITTED:该隔离级别表示一个事务只能读取另一个事务已经提交的数据。该级别可以防止脏读,这也是大多数情况下的推荐值。
TransactionDefinition.ISOLATION_REPEATABLE_READ:该隔离级别表示一个事务在整个过程中可以多次重复执行某个查询,并且每次返回的记录都相同。即使在多次查询之间有新增的数据满足该查询,这些新增的记录也会被忽略。该级别可以防止脏读和不可重复读。
TransactionDefinition.ISOLATION_SERIALIZABLE:所有的事务依次逐个执行,这样事务之间就完全不可能产生干扰,也就是说,该级别可以防止脏读、不可重复读以及幻读。但是这将严重影响程序的性能。通常情况下也不会用到该级别。

注意:不同的数据库所能支持的事务隔离级别以及默认的事务隔离级别有可能是不同的

3)事务的只读属性
定义了一个事务中是否是只读操作,如果设置只读那么数据库内部就可以对该操作进行合适的优化措施,只有传播行为是PROPAGATION_REQUIRED PROPAGATION_REQUIRES_NEW PROPAGATION_NESTED的时候只读设置才有意义,因为只读优化是在事务开始的时候由数据库实施的,而在这三个传播行为下才有可能启动一个新事务

4)事务超时
为了使应用程序可以很好的运行,事务不能运行太长的时间,所以这个属性就控制着这个时间.只有传播行为是PROPAGATION_REQUIRED PROPAGATION_REQUIRES_NEW PROPAGATION_NESTED的时候超时设置才有意义,因为超时时钟会在事务开始的时候启动,而在这三个传播行为下才有可能启动一个新事务.注意事务超时后会自动回滚.(单位是 秒)

5)事务的回滚规则
定义了哪些异常会导致事务回滚而哪些不会。默认情况下,事务在遇到运行时异常的时候才会回滚,而遇到检查时异常时不会回滚
-Exception表示有Exception抛出时,事务回滚. -代表回滚+就代表提交
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值