<< 我收藏的链接5 | 首页 | Commons Logging自动发现日志工具过程和Log4j的初始化 >>

Hibernate optimistic locking exception

HibernateOptimisticLockingFailureException,The error occurs because at least 2 transactions are working on the same record(s). If a record is read by 2 transactions, and if the record is saved by one transaction first, and then by the second one, an optimistic locking exception is thrown in the second transaction, because the assumption that nobody else was going to modify the record, doesn't hold. (Optimistic locking = You are optimistic about the facts that nobody else is going to need the same record)

Are you using multithreading in batch mode? Probably multiple transactions are modifying the same record at the same moment.

There are different solutions:
1) make sure (by design) that records that are going to be updated, are touched by only a single transaction at any given moment
2) instead of optimistic locking, use pessimistic locking (problem: reduces scalability, and added complexity in application)
3) retry the transaction: make sure that no non-transactional state is kept between retries.

标签 : ,



发表评论 发送引用通报