Java 8、Hibernate 5.1.0.Final、Guice 4.1.0、Jersey 1.19を使用します。私はデータベースからロードされた同じオブジェクトエンティティを変更するいくつかのRESTメソッドを持っています。彼らは同時に実行し、同じ項目を変更すると、私が手:異なるRESTメソッドが同じオブジェクトを変更します - RollbackException、OptimisticLockException、StaleStateException
javax.persistence.RollbackException: Error while committing the transaction
at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:86)
Caused by: javax.persistence.OptimisticLockException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
Caused by: org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
考えられる解決策:
OptimisticLockException
の機会を低減するために、オブジェクトの状態を更新するREST方式でのオブジェクトによって
- 同期(5 Common Hibernate Exceptions and How to Fix Them)
OptimisticLockException
(JPA: pattern for handling OptimisticLockException)の後に再試行する方法
その他のオプションはありますか?この問題を処理する最善の方法は何ですか?
public void changeItemName(Long id, String name){
Item item = itemDAO.find(id);
item.setName(name);
try {
itemDAO.save(item);
} catch (RollbackException | OptimisticLockException | StaleStateException e) {
logger.warn("Retry method after " + e.getClass().getName());
changeItemName(id, name);
}
}
しかし、私は得る:REST方式で
オブジェクトによってjavax.persistence.RollbackException: Error while committing the transaction
at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:86)
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not update: [com.example.Item#1]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1692)
Caused by: org.hibernate.exception.GenericJDBCException: could not update: [com.example.Item#1]
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)
Caused by: org.postgresql.util.PSQLException: This statement has been closed.
at org.postgresql.jdbc2.AbstractJdbc2Statement.checkClosed(AbstractJdbc2Statement.java:2653)
はlibにhttps://vladmihalcea.com/2013/11/15/optimistic-locking-retry-with-jpa/で再試行しようとしましたが、それはdidnのようにする代わりに、あなたが何かを行うことができます仕事はありません。さて、例外のtry-catchを追加してメソッドを再試行しましたが、別の問題に直面しました。再試行したときにオブジェクトがキャッシュから取得され、バージョンがリフレッシュされず、無限ループになります。 – Justas
再試行する前にリフレッシュ、マージ、エビクト、クリアを試みましたが、助けにはなりませんでした。 – Justas
私はあなたが例外をキャッチする必要があると思います(私はそれが動作するはずだと思います)オブジェクトをもう一度(entityManager.refresh(エンティティ))を取得し、変更を適用し、保存します。何とかバージョンをコピーしないことを確認してください。 – StanislavL