When I set locks in JPA code, are they enforced in code or in DBMS?

那年仲夏 提交于 2019-12-24 08:49:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!