【问题标题】:javax.persistence.RollbackException: Error while committing the transaction] with root cause java.lang.StackOverflowError: nulljavax.persistence.RollbackException: Error while committing the transaction] 根本原因 java.lang.StackOverflowError: null
【发布时间】:2018-12-07 00:20:33
【问题描述】:

我有一个使用 Spring Data REST 框架的 Spring Boot API(从 spring-boot-starter-parent 2.1.0.RELEASE 继承的依赖项)。我正在尝试执行 PUT 或 PATCH 请求来更新实体,但似乎都不起作用,抛出以下错误消息:

[请求处理失败;嵌套异常是 org.springframework.transaction.TransactionSystemException:无法提交 JPA 事务;嵌套异常是 javax.persistence.RollbackException: Error while committing the transaction] 根本原因 java.lang.StackOverflowError: null

我尝试更新的实体具有以下结构:

@Getter
@Setter
@Entity
@Table(name = "entity_a")
public class EntityA extends BaseEntity {
    @Column(name = "name", nullable = false, length = 100)
    private String name

    @OneToMany(mappedBy = "entityA")
    private Set<EntityB> entitiesB;
}

BaseEntity 拥有 ID 和审计信息。

我正在向以下路径发出 PUT/PATCH 请求:

http://localhost:8080/api/v1/entitiesA/the_uuid

body 有效载荷为

{ "名称": "新名称" }

由于这是一个堆栈溢出错误,我的第一个想法是递归正在发生。我注释掉了 Set 字段(连同 @OneToMany 注释),但我仍然遇到了错误。有没有人遇到过这个错误?

【问题讨论】:

  • 你使用什么数据库?似乎事务提交错误与 db 有关。
  • 另外,您需要发布整个跟踪堆栈以准确查看此错误发生在哪一行代码中。
  • 这是一个托管在 AWS 上的 MySQL RDS 实例(版本 5.7.22)。对于开发环境,它通过 SSH 隧道连接。
  • 这里是堆栈跟踪的链接:pastebin.com/WWVb4C12 我注意到 com.api.config.AuditorAwareConfiguration.getCurrentAuditor(AuditorAwareConfiguration.java:27) 被一遍又一遍地调用,也就是这一行代码:userDao.findByUsername(username);

标签: java spring-data-rest


【解决方案1】:

问题与我实现 AuditorAware 接口的方式有关。我使用的 userDao 方法导致了递归调用。我仍然不知道为什么会这样,但是查看this forum,我将 getCurrentAuditor() 的实现从:

@Override
public Optional<User> getCurrentAuditor() {
    String username = SecurityContextHolder.getContext().getAuthentication().getName();
    User user = userDao.findByUsername(username);
    return Optional.ofNullable(user);
}

到:

@Override
public Optional<User> getCurrentAuditor() {
    User auditor = null;
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication != null) {
        Object principal = authentication.getPrincipal();
        if (principal instanceof User) {
            auditor = (User) principal;
        }
    }
    return Optional.ofNullable(auditor);
}

一切都按预期进行。

【讨论】:

  • 在这种情况下 createdBy 无效但 LastModifiedBy 有效
猜你喜欢
  • 2019-02-26
  • 1970-01-01
  • 2022-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多