问题
If I make a call like this in Java code:
entityManager.lock(entity, LockModeType.PESSIMISTIC_WRITE);
is this enforced in code, by DBMS table/row-level locks, or either of those depending on the JPA implementation?
Thanks!
回答1:
JPA rely on dbms to acquire locking on resources. So, yes, it will do row locking on db implementation. That is why we have to specify our db dialect when using JpA.
回答2:
This is from the Oracle blog excerpt: The pessimistic lock modes lock a database row when data is read. This is the equivalent to the action taken in response to the SQL statement SELECT . . . FOR UPDATE [NOWAIT].
This is from WikiBooks: The SQL syntax for pessimistic locking is database specific, and different databases have different syntax and levels of support, so ensure your database properly supports your locking requirements.
There are other ways to implement pessimistic locking, it could be implemented at the application level, or through serializable transaction isolation.
来源:https://stackoverflow.com/questions/41242940/when-i-set-locks-in-jpa-code-are-they-enforced-in-code-or-in-dbms