Spring事务失效的几种原因

 更新时间:2020年09月16日 10:32:53   作者:边锋  
在日常编码过程中常常涉及到事务,在前两天看到一篇文章提到了Spring事务,那么在此总结下在Spring环境下事务失效的几种原因.

数据库引擎不支持事务

在MySQL数据库中有几种引擎(InnoDB,MyISAM,Memory等等),仅仅InnoDB支持事务,如果数据库底层都不支持事务的话,那么再怎么折腾都是白搭.

@transactional加在private方法上

@Transactional只能加在public方法上,如果需要在private方法中加入事务,可以使用Aspect配transactionManager使用.

本类方法调本类另一个方法

例如:

@Service
public class UserServiceImpl implements UserService {

  @Transactional
  public void update(User user) {
  //check
    updateUserInfo(user);
  }

  @Transactional(propagation = Propagation.REQUIRES_NEW)
  public void updateUser(User user) {
    // update user
  }

}

@Transactional(propagation = Propagation.REQUIRES_NEW)是无效的,在Spring中是使用代理的方式实现事务,发生自身调用的时候,没有经过Spring的代理,自然事务失效.

不支持事务

@Service
public class UserServiceImpl implements UserService {

  @Transactional(propagation = Propagation.NOT_SUPPORTED)
  public void update(User user) {
  //do some action
  }

}

@Transactional(propagation = Propagation.NOT_SUPPORTED)表示如果当前存在事务就挂起,以没有事务的方式运行,主动不支持事务了,那么再怎么操作也是白搭. 此处贴下Spring的传播行为:

  /**
   * Support a current transaction, create a new one if none exists.
   * Analogous to EJB transaction attribute of the same name.
   * <p>This is the default setting of a transaction annotation.
   */
  REQUIRED(TransactionDefinition.PROPAGATION_REQUIRED),

  /**
   * Support a current transaction, execute non-transactionally if none exists.
   * Analogous to EJB transaction attribute of the same name.
   * <p>Note: For transaction managers with transaction synchronization,
   * PROPAGATION_SUPPORTS is slightly different from no transaction at all,
   * as it defines a transaction scope that synchronization will apply for.
   * As a consequence, the same resources (JDBC Connection, Hibernate Session, etc)
   * will be shared for the entire specified scope. Note that this depends on
   * the actual synchronization configuration of the transaction manager.
   * @see org.springframework.transaction.support.AbstractPlatformTransactionManager#setTransactionSynchronization
   */
  SUPPORTS(TransactionDefinition.PROPAGATION_SUPPORTS),

  /**
   * Support a current transaction, throw an exception if none exists.
   * Analogous to EJB transaction attribute of the same name.
   */
  MANDATORY(TransactionDefinition.PROPAGATION_MANDATORY),

  /**
   * Create a new transaction, and suspend the current transaction if one exists.
   * Analogous to the EJB transaction attribute of the same name.
   * <p><b>NOTE:</b> Actual transaction suspension will not work out-of-the-box
   * on all transaction managers. This in particular applies to
   * {@link org.springframework.transaction.jta.JtaTransactionManager},
   * which requires the {@code javax.transaction.TransactionManager} to be
   * made available to it (which is server-specific in standard Java EE).
   * @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager
   */
  REQUIRES_NEW(TransactionDefinition.PROPAGATION_REQUIRES_NEW),

  /**
   * Execute non-transactionally, suspend the current transaction if one exists.
   * Analogous to EJB transaction attribute of the same name.
   * <p><b>NOTE:</b> Actual transaction suspension will not work out-of-the-box
   * on all transaction managers. This in particular applies to
   * {@link org.springframework.transaction.jta.JtaTransactionManager},
   * which requires the {@code javax.transaction.TransactionManager} to be
   * made available to it (which is server-specific in standard Java EE).
   * @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager
   */
  NOT_SUPPORTED(TransactionDefinition.PROPAGATION_NOT_SUPPORTED),

  /**
   * Execute non-transactionally, throw an exception if a transaction exists.
   * Analogous to EJB transaction attribute of the same name.
   */
  NEVER(TransactionDefinition.PROPAGATION_NEVER),

  /**
   * Execute within a nested transaction if a current transaction exists,
   * behave like PROPAGATION_REQUIRED else. There is no analogous feature in EJB.
   * <p>Note: Actual creation of a nested transaction will only work on specific
   * transaction managers. Out of the box, this only applies to the JDBC
   * DataSourceTransactionManager when working on a JDBC 3.0 driver.
   * Some JTA providers might support nested transactions as well.
   * @see org.springframework.jdbc.datasource.DataSourceTransactionManager
   */
  NESTED(TransactionDefinition.PROPAGATION_NESTED);

异常被catch

@Service
public class UserServiceImpl implements UserService {

  @Transactional
  public void update(User user) {
  try{

  }catch(Exception e){
    log.error(e.getMessage(),e);
  }
  }

}

触发回滚的操作是被接收到异常,一般我们会在@Transactional后面加上rollbackFor或者noRollbackForClassName来指明触发回滚的异常,但是如果在代码中给catch了异常,那么对于Spring代理来说就这个方法从头到尾都没有问题,自然不会触发回滚.

异常类型错误

@Service
public class UserServiceImpl implements UserService {

  @Transactional
  public void update(User user) {
  try{

  }catch(Exception e){
    log.error(e.getMessage(),e);
    throw new Exception(e.getMessage());
  }
  }

}

以上方式throw new Exception(e.getMessage());事务也是无效的,主要原因是事务回滚的条件是throw 运行时异常(RunTimeException).如果需要其他异常也回滚,需要在@Transactional后面加上rollbackFor或者noRollbackForClassName来指明触发回滚的异常.

没有被Spring管理

不在Spring环境下,自然不受Spring的管理,事务管理器也当然失去了作用.

没有配置TransactionManager

需要对当前数据源配置事务管理器,尤其是在多数据源的情况下.

以上就是Spring事务失效的几种原因的详细内容,更多关于Spring事务失效的资料请关注脚本之家其它相关文章!

相关文章

  • Java下3中XML解析 DOM方式、SAX方式和StAX方式

    Java下3中XML解析 DOM方式、SAX方式和StAX方式

    目前我知道的JAVA解析XML的方式有:DOM, SAX, StAX;如果选用这几种,感觉还是有点麻烦;如果使用:JAXB(Java Architecture for XML Binding),个人觉得太方便了
    2013-04-04
  • Java(若依)如何读取Yml配置文件

    Java(若依)如何读取Yml配置文件

    文章介绍了如何在Java若依框架中读取Yml配置文件,包括定义配置字段、编写读取工具类以及注意事项,最后总结了个人经验
    2025-02-02
  • Java Array与ArrayList区别详解

    Java Array与ArrayList区别详解

    这篇文章主要介绍了Java Array与ArrayList区别详解的相关资料,需要的朋友可以参考下
    2017-01-01
  • 一文搞懂Java的SPI机制(推荐)

    一文搞懂Java的SPI机制(推荐)

    Java定义了一套JDBC的接口,但并未提供具体实现类,而是在不同云厂商提供的数据库实现包。这篇文章给大家介绍Java的SPI机制,感兴趣的朋友一起看看吧
    2021-11-11
  • Spring Cache自定义缓存key和过期时间的实现代码

    Spring Cache自定义缓存key和过期时间的实现代码

    使用 Redis的客户端 Spring Cache时,会发现生成 key中会多出一个冒号,而且有一个空节点的存在,查看源码可知,这是因为 Spring Cache默认生成key的策略就是通过两个冒号来拼接,本文给大家介绍了Spring Cache自定义缓存key和过期时间的实现,需要的朋友可以参考下
    2024-05-05
  • 浅谈SpringBoot Bean加载优先级的问题

    浅谈SpringBoot Bean加载优先级的问题

    这篇文章主要介绍了浅谈SpringBoot Bean加载优先级的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • mybatis教程之增删改查_动力节点Java学院整理

    mybatis教程之增删改查_动力节点Java学院整理

    这篇文章主要介绍了mybatis教程之增删改查,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09
  • java基于netty NIO的简单聊天室的实现

    java基于netty NIO的简单聊天室的实现

    这篇文章主要介绍了java基于netty NIO的简单聊天室的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • Java 可视化垃圾回收_动力节点Java学院整理

    Java 可视化垃圾回收_动力节点Java学院整理

    Ben Evans是一名资深培训师兼顾问,他在演讲可视化垃圾回收中从基础谈起讨论了垃圾回收。以下是对其演讲的简短总结。感兴趣的朋友一起学习吧
    2017-05-05
  • Netty实现自定义协议编解码器

    Netty实现自定义协议编解码器

    这篇文章主要为大家介绍了Netty实现自定义协议编解码器示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-02-02

最新评论