记录:Spring事务 Aop配置

说明:

    所谓事务就是对数据库事务的操作,为了保证数据的一致性,最经典的例子是取钱的例子,相关原理这里不多说了,只记录一下项目中的简单使用和配置。

    相关链接:Spring事务详解

 一、非注解方式

测试工程目录


1.在spring-aop中添加一下配置

<!-- 事务配置 -->
<bean name="transactionManager"
	class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource" ref="DataSource" />
</bean>
<!-- 配置切面 把增强用到方法上 -->
<tx:advice id="advice" transaction-manager="transactionManager">
	<tx:attributes>
		<tx:method name="add*" propagation="REQUIRED" isolation="DEFAULT"
			timeout="-1" read-only="false" rollback-for="java.lang.Exception" />
		<tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT"
			timeout="-1" read-only="false" rollback-for="java.lang.Exception" />
		<tx:method name="insert*" propagation="REQUIRED" isolation="DEFAULT"
			timeout="-1" read-only="false" rollback-for="java.lang.Exception" />
		<tx:method name="create*" propagation="REQUIRED" isolation="DEFAULT"
			timeout="-1" read-only="false" rollback-for="java.lang.Exception" />
		<tx:method name="select*" propagation="REQUIRED" isolation="DEFAULT"
			timeout="-1" read-only="false" rollback-for="java.lang.Exception" />
		<tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT"
			timeout="-1" read-only="false" rollback-for="java.lang.Exception" />
		<tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT"
			timeout="-1" read-only="false" rollback-for="java.lang.Exception" />
		<tx:method name="remove*" propagation="REQUIRED" isolation="DEFAULT"
			timeout="-1" read-only="false" rollback-for="java.lang.Exception" />
		<tx:method name="get*" propagation="REQUIRED" isolation="DEFAULT"
			timeout="-1" read-only="false" rollback-for="java.lang.Exception" />
		<tx:method name="*" read-only="true" />
	</tx:attributes>
</tx:advice>

<!-- 配置切点 实际要增强的方法 -->
<aop:config>
	<aop:pointcut expression="execution(* com.chni.service.*.*(..))"
		id="pointcut" />
	<aop:advisor advice-ref="advice" pointcut-ref="pointcut" />
</aop:config>

execution表达式配置的时候一定要是最后一级包名

错误示例:

execution(* com.chni.*.*(..))  

测试类:

package com.chni.test;

import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.chni.service.Emp;

/**
 * 
 * @Description: 测试类 
 * @author 天高任鸟飞
 * @date 2018年5月4日 上午10:47:06
 * @Transactional 注释标签是表明此测试类的事务启用,这样所有的测试方案都会自动的 rollback,即您不用自己清除自己所做的任何对数据库的变更了
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "/applicationContext.xml" })
public class Test {

    @Autowired
    private EmpService empService;
	
    @org.junit.Test
    public void testSpringAop() { 
	empService.add();
    } 
}

EmpService类:

package com.chni.service;

import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.chni.dao.EmpMapper;
import com.chni.dao.StuMapper;
import com.chni.pojo.Stu;

@Service
public class Emp {

	@Autowired
	private EmpMapper empMapper;
	@Autowired
	private StuMapper stuMapper;
	
	public void add(){//方法名一定要以配置文件中的配置规则开头
		com.chni.pojo.Emp emp = new com.chni.pojo.Emp();
		emp.setEmpId(UUID.randomUUID().toString());
		emp.setEmpName("事务");
		Stu stu = new Stu();
		stu.setStuNameId(UUID.randomUUID().toString());
		stu.setStuName("事务");
		empMapper.insertEmp(emp);//对Emp表进行插入操作
		System.out.println(1/0);//手动添加一个异常
		stuMapper.insertStu(stu);//对Stu表进行插入操作
	}
}

执行测试方法观察数据库会发现emp表和stu表都没有插入新的数据,则配置成功


二、注解配置

在spring-aop中添加以下配置

<!-- 事务配置 -->
<bean name="transactionManager"
	class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource" ref="DataSource" />
</bean>
<!-- 注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />

测试方法不变

在EmpService类中需要事务的方法上添加@Transactional注解即可

package com.chni.service;

import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.chni.dao.EmpMapper;
import com.chni.dao.StuMapper;
import com.chni.pojo.Stu;

@Service
// @Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true)
public class Emp {

	@Autowired
	private EmpMapper empMapper;
	@Autowired
	private StuMapper stuMapper;

	@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, readOnly = false)
	public void addStu() {
		com.chni.pojo.Emp emp = new com.chni.pojo.Emp();
		emp.setEmpId(UUID.randomUUID().toString());
		emp.setEmpName("事务");
		Stu stu = new Stu();
		stu.setStuNameId(UUID.randomUUID().toString());
		stu.setStuName("事务");
		empMapper.insertEmp(emp);
		System.out.println(1 / 0);
		stuMapper.insertStu(stu);
	}
}

三、手动回滚

    突然发现当我对异常进行处理的时候事务的配置就失去了作用,不在进行回滚操作,一下代码便可解决

try {
    System.out.println(1 / 0);
} catch (Exception e) {
    System.out.println("我是异常!哈哈哈哈!!!");
    TransactionAspectSupport.currentTransactionStatus()
	.setRollbackOnly();//回滚
}
在捕获异常之后根据需要添加回滚即可
测试项目代码下载地址:https://download.csdn.net/download/lwcyd/10391991


猜你喜欢

转载自blog.csdn.net/lwcyd/article/details/80193935
今日推荐