实际项目开发中,如果涉及到多张表操作时,为了保证业务数据的一致性,大家一般都会采用事务机制,好多小伙伴可能只是简单了解一下,遇到事务失效的情况,便会无从下手,下面这篇文章主要给大家介绍了关于Spring事务失效场景的相关资料,需要的朋
1)未被Spring管理
使用Spring事务的前提是:对象要被Spring管理,事务方法所在的类要被加载为bean对象
如果事务方法所在的类没有被加载为一个bean,那么事务自然就失效了,示例:
//@Service
public class UserServiceImpl {
    @Transactional
    public void doTest() {
        // 业务代码
    }
}2)数据库引擎不支持事务
以MySQL为例,InnoDB引擎是支持事务的,而像MyISAM、MEMORY等是不支持事务的。
从MySQL5.5.5开始默认的存储引擎是InnoDB,之前默认都是MyISAM。所以在开发过程中发现事务失效,不一定是Spring的锅,最好确认一下数据库表是否支持事务。
3)事务方法没有被public修饰
众所周知,java的访问权限修饰符有:private、default、protected、public四种,
但是@Transactional注解只能作用于public修饰的方法上,
在AbstractFallbackTransactionAttributeSource类(Spring通过这个类获取@Transactional注解的配置属性信息)的computeTransactionAttribute方法中有个判断,如果目标方法不是public,则TransactionAttribute返回null,即不支持事务。
@Nullable
	protected TransactionAttribute computeTransactionAttribute(Method method, @Nullable Class<?> targetClass) {
		// Don't allow no-public methods as required.
		if (allowPublicMethodsOnly() && !Modifier.isPublic(method.getModifiers())) {
			return null;
		}
        //………………
	}其实想想动态代理的原理就很好理解了,动态代理是通过实现接口或者继承来实现的,所以目标方法必须是public修饰,并且不能是final修饰。
4)方法使用final修饰
如果一个方法不想被子类重写,那么我们就可以把他写成final修饰的方法
如果事务方法使用final修饰,那么aop就无法在代理类中重写该方法,事务就不会生效
同样的,static修饰的方法也无法通过代理变成事务方法
5)同一类中方法调用
假如在某个Service的方法中,调用了另外一个事务方法:
@Service
public class UserServiceImpl {
    @Autowired
    UserMapper userMapper;
    public void del(){
        doTest();
    }
    @Transactional
    public void doTest() {
        userMapper.deleteById(200108);
        int i = 10/0; //模拟发生异常
    }
}像上面的代码,doTest方法使用@Transactional注解标注,在del()方法中调用了doTest()方法,在外部调用del()方法时,事务也不会生效,因为这里del()方法中调用的是类本身的方法,而不是代理对象的方法。
那么如果确实有这样的需求怎么办呢?
引入自身bean
@Service
public class UserServiceImpl {
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserServiceImpl userServiceImpl;
    public void del(){
        userServiceImpl.doTest();
    }
    @Transactional
    public void doTest() {
        userMapper.deleteById(200112);
        int i = 10/0; //模拟发生异常
    }
}通过ApplicationContext引入bean
@Service
public class UserServiceImpl {
    @Autowired
    UserMapper userMapper;
    @Autowired
    ApplicationContext applicationContext;
    public void del(){
       ((UserServiceImpl)applicationContext.getBean("userServiceImpl")).doTest();
    }
    @Transactional
    public void doTest() {
        userMapper.deleteById(200112);
        int i = 10/0; //模拟发生异常
    }
}通过AopContext获取当前代理类
在启动类上添加注解@EnableAspectJAutoProxy(exposeProxy = true),表示是否对外暴露代理对象,即是否可以获取AopContext
@Service
public class UserServiceImpl {
    @Autowired
    UserMapper userMapper;
    @Autowired
    ApplicationContext applicationContext;
    public void del(){
        ((UserServiceImpl)AopContext.currentProxy()).doTest();
    }
    @Transactional
    public void doTest() {
        userMapper.deleteById(200112);
        int i = 10/0; //模拟发生异常
    }
}6)未开启事务
如果是SpringBoot项目,那么SpringBoot通过DataSourceTransactionManagerAutoConfiguration自动配置类帮我们开启了事务。
如果是传统的Spring项目,则需要我们自己配置
<!--        配置事务管理器-->
<bean id="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="insert*" propagation="REQUIRED"/> <!-- 所有insert开头的方法,以下同理 -->
    <tx:method name="update*" propagation="REQUIRED"/>
    <tx:method name="delete*" propagation="REQUIRED"/>
  </tx:attributes>
</tx:advice>
<!--        配置事务切面-->
<aop:config>
  <aop:pointcut id="AdviceAop" expression="execution(* com.yy.service..*(..))"/> <!--要执行的方法在哪个包,意思为:com.yy.service下的所有包里面的包含任意参数的所有方法-->
  <aop:advisor advice-ref="Advice" pointcut-ref="AdviceAop"/> <!-- 配置为AdviceAop执行哪个事务通知 -->
</aop:config>
这样在执行service包下的增删改操作的方法时,就开启事务了,或者使用注解的方式
<!--        配置事务管理器-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
                <property name="dataSource" ref="dataSource"/>
        </bean>
<!--        注解式事务声明配置-->
        <tx:annotation-driven transaction-manager="transactionManager" />7)多线程调用
@Service
public class UserServiceImpl {
    @Autowired
    UserMapper userMapper;
    @Transactional
    public void doTest() throws InterruptedException {
        userMapper.deleteById(200110);
        new Thread(()->{
            userMapper.deleteById(200112);
            int i = 10/0; //模拟发生异常
        }).start();
    }
}在事务方法doTest中,启动了一个新的线程,并在新的线程中发生了异常,这样doTest是不会回滚的。
因为两个操作不在一个线程中,获取到的数据库连接不一样,从而是两个不同的事务,所以也不会回滚。
8)错误的传播行为
Spring定义了7种传播行为,我们可以通propagation属性来指定传播行为参数,目前只有REQUIRED、REQUIRES_NEW、NESTED会创建新的事务,其他的则会以非事务的方式运行或者抛出异常
@Service
public class UserServiceImpl {
    @Autowired
    UserMapper userMapper;
    @Transactional(propagation = Propagation.NEVER)
    public void doTest() throws InterruptedException {
        userMapper.deleteById(200114);
        int i = 10/0; //模拟发生异常
    }
}9)自己try…catch…掉了异常
如果没有异常抛出,则Spring认为程序是正常的,就不会回滚
@Service
public class UserServiceImpl {
    @Autowired
    UserMapper userMapper;
    @Transactional
    public void doTest() {
        try{
            userMapper.deleteById(200115);
            int i = 10/0; //模拟发生异常
        }catch (Exception e){
            // 异常操作
        }
    }
}10)手动抛出了错误的异常
Spring默认只会回滚RuntimeException和Error对于普通的Exception,不会回滚
如果你想触发其他异常的回滚,需要在注解上配置一下,如:@Transactional(rollbackFor = Exception.class)
@Service
public class UserServiceImpl {
    @Autowired
    UserMapper userMapper;
    @Transactional
    public void doTest() throws Exception {
        try{
            userMapper.deleteById(200116);
            int i = 10/0; //模拟发生异常
        }catch (Exception e){
            // 异常操作
            throw new Exception();
        }
    }
}11)自定义回滚异常
rollbackFor 用于指定能够触发事务回滚的异常类型,可以指定多个异常类型。
默认是在RuntimeException和Error上回滚。
若异常非配置指定的异常类,则事务失效
@Service
public class UserServiceImpl {
    @Autowired
    UserMapper userMapper;
    @Transactional(rollbackFor = NullPointerException.class)
    public void doTest() throws MyException {
        userMapper.deleteById(200118);
        throw new MyException();
    }
}即使rollbackFor有默认值,但阿里巴巴开发者规范中,还是要求开发者重新指定该参数。
因为如果使用默认值,一旦程序抛出了Exception,事务不会回滚,这会出现很大的bug。所以,建议一般情况下,将该参数设置成:Exception或Throwable。
12)嵌套事务回滚多了
@Service
public class UserServiceImpl {
    @Autowired
    UserMapper userMapper;
    @Transactional
    public void doTest() {
        userMapper.deleteById(200118);
        ((UserServiceImpl)AopContext.currentProxy()).test02();
    }
    @Transactional(propagation = Propagation.NESTED)
    public void test02(){
        userMapper.deleteById(200119);
        int i = 10 / 0; //模拟发生异常
    }
}test02()方法出现了异常,没有手动捕获,会继续往上抛,到外层doTest()方法的代理方法中捕获了异常。所以,这种情况是直接回滚了整个事务,不只回滚单个保存点。
如果只回滚单个保存点,可以将内部嵌套事务放在try/catch中,类似于上面的自己try…catch…掉异常,并且不继续往上抛异常。这样就能保证,如果内部嵌套事务中出现异常,只回滚内部事务,而不影响外部事务。
@Service
public class UserServiceImpl {
    @Autowired
    UserMapper userMapper;
    @Transactional
    public void doTest() {
        userMapper.deleteById(200118);
        try{
            ((UserServiceImpl)AopContext.currentProxy()).test02();
        }catch (Exception e){
            
        }
    }
    @Transactional(propagation = Propagation.NESTED)
    public void test02(){
        userMapper.deleteById(200119);
        int i = 10 / 0; //模拟发生异常
    }
}到此这篇关于Spring详细讲解事务失效的场景的文章就介绍到这了,更多相关Spring事务失效内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!
本文标题为:Spring详细讲解事务失效的场景
				
        
 
            
        - Spring Security权限想要细化到按钮实现示例 2023-03-07
 - JSP 制作验证码的实例详解 2023-07-30
 - 深入了解Spring的事务传播机制 2023-06-02
 - Springboot整合minio实现文件服务的教程详解 2022-12-03
 - SpringBoot使用thymeleaf实现一个前端表格方法详解 2023-06-06
 - 基于Java Agent的premain方式实现方法耗时监控问题 2023-06-17
 - Java实现顺序表的操作详解 2023-05-19
 - JSP页面间传值问题实例简析 2023-08-03
 - ExecutorService Callable Future多线程返回结果原理解析 2023-06-01
 - Java中的日期时间处理及格式化处理 2023-04-18
 
						
						
						
						
						
				
				
				
				