首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >REQUIRES_NEW: Spring外部事务回滚,而不是在内部回滚时持久化

REQUIRES_NEW: Spring外部事务回滚,而不是在内部回滚时持久化
EN

Stack Overflow用户
提问于 2021-03-22 15:59:30
回答 1查看 599关注 0票数 0

我有一个Spring应用程序,在Controller和服务之间有一个薄薄的层,它的唯一目的是尝试捕获,如果抛出异常,则使用JpaRepository持久化失败的实体,以供后续检查。我设计了我的“拦截器”

代码语言:javascript
运行
复制
    @Transactional
    public void upload(byte[] bytes) {
       try {
          service.upload(bytes);
       } catch (Exception e) {
           failRepo.save(new Failure(bytes, e)); // code trimmed for brevity 
           throw e;
       }
   }

我的服务方法看起来是:

代码语言:javascript
运行
复制
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
public void upload(byte[] bytes);

我期望的是,如果在服务中抛出异常,内部事务将被回滚,异常将弹出,外部事务将持久化我的数据,但是hibernate日志显示,出于某种原因,外部事务也会回滚,而失败数据不会被持久化。我是否还需要外层上的另一个传播层?

编辑:相关日志

代码语言:javascript
运行
复制
o.s.o.h.HibernateTransactionManager      : Creating new transaction with name [com.company.interceptor.upload]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
o.s.o.h.HibernateTransactionManager      : Opened new Session [SessionImpl(480607911<open>)] for Hibernate transaction
o.h.e.t.internal.TransactionImpl         : On TransactionImpl creation, JpaCompliance#isJpaTransactionComplianceEnabled == false
o.h.e.t.internal.TransactionImpl         : begin
o.s.o.h.HibernateTransactionManager      : Exposing Hibernate transaction as JDBC [org.springframework.orm.hibernate5.HibernateTransactionManager$$Lambda$1317/0x0000000800d0c440@52318830]
o.s.o.h.HibernateTransactionManager      : Found thread-bound Session [SessionImpl(480607911<open>)] for Hibernate transaction
o.s.o.h.HibernateTransactionManager      : Suspending current transaction, creating new transaction with name [com.company.service.upload]
o.s.o.h.HibernateTransactionManager      : Opened new Session [SessionImpl(1041560268<open>)] for Hibernate transaction
o.h.e.t.internal.TransactionImpl         : On TransactionImpl creation, JpaCompliance#isJpaTransactionComplianceEnabled == false
o.h.e.t.internal.TransactionImpl         : begin
o.s.o.h.HibernateTransactionManager      : Exposing Hibernate transaction as JDBC [org.springframework.orm.hibernate5.HibernateTransactionManager$$Lambda$1317/0x0000000800d0c440@778c9da3]

------ other irrelevant for us logs

o.s.o.h.HibernateTransactionManager      : Initiating transaction rollback
o.s.o.h.HibernateTransactionManager      : Rolling back Hibernate transaction on Session [SessionImpl(1041560268<open>)]
o.h.e.t.internal.TransactionImpl         : rolling back
o.s.o.h.HibernateTransactionManager      : Closing Hibernate Session [SessionImpl(1041560268<open>)] after transaction
o.s.o.h.HibernateTransactionManager      : Resuming suspended transaction after completion of inner transaction
stomAnnotationTransactionAttributeSource : Adding transactional method 'save' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
o.s.o.h.HibernateTransactionManager      : Found thread-bound Session [SessionImpl(480607911<open>)] for Hibernate transaction
o.s.o.h.HibernateTransactionManager      : Participating in existing transaction
org.hibernate.engine.spi.ActionQueue     : Executing identity-insert immediately
org.hibernate.SQL                        : insert into failure_table (content_type, created_time, exception_message, bytes, user_agent) values (?, ?, ?, ?, ?)
o.h.id.IdentifierGeneratorHelper         : Natively generated identity: 15
o.h.r.j.i.ResourceRegistryStandardImpl   : HHH000387: ResultSet's statement was not registered
o.s.o.h.HibernateTransactionManager      : Initiating transaction rollback
o.s.o.h.HibernateTransactionManager      : Rolling back Hibernate transaction on Session [SessionImpl(480607911<open>)]
o.h.e.t.internal.TransactionImpl         : rolling back
o.s.o.h.HibernateTransactionManager      : Closing Hibernate Session [SessionImpl(480607911<open>)] after transaction
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-23 07:45:45

问题解决方案由:https://stackoverflow.com/a/7125918/3214777帮助找到。

问题是在运行时异常默认情况下,Spring回滚,所以我需要一个

代码语言:javascript
运行
复制
@Transactional(noRollbackFor = Exception.class)

在我的拦截器上让它如我所料。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66749614

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档