gpt4 book ai didi

java - 保存实体时出现 ConcurrentModificationException

转载 作者:行者123 更新时间:2023-12-02 13:40:22 28 4
gpt4 key购买 nike

基于 Spring Boot、spring-data-jpa 的大型 REST Web 应用程序。我有 PersonEntity ,它与 ParentEntity 具有 ManyToOne 关系。关系的所有者是 PersonEntity,它的表中有 parentId

在执行多次SoapUI并发测试后,我们发现随机在一个地方抛出ConcurrentModificationException(随机,实际上我们看到一次并且应用程序运行了一年多,但我猜我们有来自另一个线程的一些并发访问)

出现问题的代码:

@Override
@Transactional
public void disconnectPersonFromParent(final String parentId, final String personId) {
//throw NotFound when null from repo
final PersonEntity personEntity = personService.getPerson(personId);
//throw NotFound when null from repo
final ParentEntity parentEntity = parentService.getParent(parentId);
if(parentEntity.equals(personEntity.getParent())) {
personEntity.setParent(null);
//Addresses comes from parent, when we disconnecting we have disconnect addresses also
personEntity.setAddresses(new HashSet<>());
personRepository.save(personEntity);
}
}

堆栈跟踪:

||ERROR|http-nio-auto-1-exec-487|error|[TransactionSystemExceptionMapper:353]|Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction
Caused by: javax.persistence.RollbackException: Error while committing the transaction
at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:94)
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:517)
... 130 more
Caused by: java.util.ConcurrentModificationException
at java.util.LinkedHashMap$LinkedHashIterator.nextNode(LinkedHashMap.java:719)
at java.util.LinkedHashMap$LinkedEntryIterator.next(LinkedHashMap.java:752)
at java.util.LinkedHashMap$LinkedEntryIterator.next(LinkedHashMap.java:750)
at org.hibernate.internal.util.collections.IdentityMap.entryArray(IdentityMap.java:163)
at org.hibernate.internal.util.collections.IdentityMap.concurrentEntries(IdentityMap.java:75)
at org.hibernate.event.internal.AbstractFlushingEventListener.postFlush(AbstractFlushingEventListener.java:379)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:57)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1258)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)
at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:77)
... 131 more

是不是代码有问题?我怎样才能避免它?

最佳答案

向数据库中的所有表添加 VERSION 列,并在实体中使用 @Version 对其进行注释,如下所示。

@Version
private Long version

一旦版本列就位,JPA/Hibernate 会在保存实体之前自动检查版本。如果实体加载和保存之间存在任何版本不匹配,您将收到 javax.persistence.OptimisticLockException。您可以捕获异常并采取任何必要的操作,例如记录日志、通知用户等。网上有各种关于 @Version 实现的文章。

希望有帮助。

您可以在 Repo 调用周围的 Service 方法中进行异常处理或添加@COntrollerAdvice 来捕获异常并共同处理它们。下面的示例是所有 REST 调用发送 500 错误并以“自定义消息”作为响应。

@ControllerAdvice
public class ExceptionHandlerAdvice extends ResponseEntityExceptionHandler {

@ExceptionHandler(value = { Exception.class })
protected ResponseEntity<Object> handleAllExcecption(Exception ex, WebRequest request) {
// Do what ever you want. Log the error message, send notification etc
return handleExceptionInternal(ex, logId.longValue(), new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR,
request);
}

}

关于java - 保存实体时出现 ConcurrentModificationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42764394/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com