开发者

How to release a locked row using JPA?

I'm using the EclipseLink implementation of the JPA 2.0 which allows pessimistic locking. I k开发者_运维百科now how to lock an entity but how do I release the lock? At first I thought this was all taken care of within a transaction (in other words, the entity is locked until you commit the transaction), but that does not seem to be the case.

I tried a quick google search (seems like this should be pretty obvious), but I haven't found anything...


After getting some sleep... and doing some more testing in the morning, I believe I have figured out my problem.

So the lock is actually taken care of within a transaction. However, when I was testing my code, I was able to retrieve a locked row using the EntityManager.find(Class, key) method (no locking strategy specified).

I erroneously thought that by putting a lock on a row, the row could not be read, period. However, I reread the JPA definitions of PESSIMISTIC_READ and PESSIMISTIC_WRITE and noticed my problem:

PESSIMISTIC_READ - The Entity is locked on the database, prevents any other transaction from acquiring a PESSIMISTIC_WRITE lock. PESSIMISTIC_WRITE - The Entity is locked on the database, prevents any other transaction from acquiring a PESSIMISTIC_READ or PESSIMISTIC_WRITE lock.

The lock doesn't necessarily prevent all reads, it just prevents another transaction from putting a READ or WRITE lock on the row.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜