Java – JPA: how does read lock work?

I'm trying to understand how to call entitymanager The effect of lock (entity, lockmodetype. Read) API documentation sounds confusing to me

If I have to concurrent threads and thread 1 calls lock (entity, lockmodetype. Read), can thread 2 still read and write entities?

What have I learned so far:

The lock type read in jpa1 is the same as the optimal in JPA2 If such a lock is set, the entitymanager checks the version property before committing the transaction, but does not update it I found the explanation of the optimal locking mode: link Search for an example of optimal (read) lockmode As I understand, setting a read lock in thread 1 has no effect on threads 2... N All other threads can still read and write entities However, when a transaction in thread 1 commits and another thread updates the entity, the transaction in thread 1 will be rolled back

I understand. Is that right?

Solution

In any case, reading just for your understanding is very deprecated:

The read lock will ensure that the state of the object will not change on commit, because the read lock allows other transactions to update or delete it. Then if thread 1 makes some changes and commits it, it first checks the state (version) entity. If it checks, it commits. If not, it is not allowed,

So basically your understanding is correct

And optimization_ Read this is a modern way to use it (also _write)

UPDATE

OK, article helps me understand. I hope it helps

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>