JPA和默认锁定模式 [英] JPA and default locking mode

查看:472
本文介绍了JPA和默认锁定模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用JPA,我们可以手动使用 OPTIMISTIC PESSIMISTIC 锁定来处理事务中的实体更改。

With JPA, we can use manually OPTIMISTIC or PESSIMISTIC locking to handle entity changes in transactions.

我想知道如果我们没有指定这两种模式中的一种,JPA如何处理锁定?
没有使用锁定模式?

I wonder how JPA handles locking if we don't specify one of these 2 modes ? No locking mode is used?

如果我们没有定义显式锁定模式,数据库完整性会丢失吗?

If we don't define an explicit locking mode, can the database integrity be lost?

谢谢

推荐答案

我已经扫描了第3.4.4节锁定模式 of Java Persistence API 2.0最终版本规范,虽然我找不到任何特定的(它没有声明这个是默认的或类似的东西)但是有一个脚注说明如下。

I've scanned through section 3.4.4 Lock Modes of the Java Persistence API 2.0 Final Release specification and while I couldn't find anything specific (it doesn't state that this is the default or anything like that) there is a footnote which says the following.


锁定模式类型NONE可以指定为锁定模式
参数的值,并且还提供注释的默认值。

The lock mode type NONE may be specified as a value of lock mode arguments and also provides a default value for annotations.

该部分是关于 LockModeType 可用的值和t继承人使用并描述了哪种方法采用了这种方式和诸如此类的论据。

The section is about the kinds of LockModeType values available and their usages and describes which methods takes an argument of this kind and whatnot.

因此,正如它所说 LockModeType.NONE 是注释的默认值(JPA,注释左右)我想当你使用 EntityManager.find(Class,Object)默认 LockModeType

So, as it said LockModeType.NONE is default for annotations (JPA, annotations left and right) I guess when you use EntityManager.find(Class, Object) the default LockModeType is used.

还有其他一些微妙的提示,以强化这一点。 3.1.1节 EntityManager接口

There are some other, subtle, hints to reinforce this. Section 3.1.1 EntityManager interface.


find方法(前提是它在没有锁定的情况下被调用或者被调用
LockModeType.NONE)和getReference方法不需要在事务上下文中调用

The find method (provided it is invoked without a lock or invoked with LockModeType.NONE) and the getReference method are not required to be invoked within a transaction context.

这说得通。例如,如果您使用MySQL作为数据库并且您选择的数据库引擎是InnoDB,那么(默认情况下)您的表将使用 REPEATABLE READ ,如果您使用其他一些RDBMS或其他数据库引擎,则可能会发生变化。

It makes sense. For example if you use MySQL as your database and your database engine of choice is InnoDB then (by default) your tables will use REPEATABLE READ, if you use some other RDBMS or other database engines this could change.

现在我不确定隔离级别与JPA锁定模式有什么关系(虽然看起来就是这样),但我的观点是不同的数据库系统不同所以JPA无法决定你(至少根据规范)默认使用什么锁模式,所以如果不这样做,它将使用 LockModeType.NONE 另外说明。

Right now I'm not exactly sure that isolation levels has anything to do with JPA lock modes (although it seems that way), but my point is that different database systems differ so JPA can't decide for you (at least according to the specification) what lock mode to use by default, so it'll use LockModeType.NONE if you don't instruct it otherwise.

我还发现有关隔离级别和锁定模式的文章,您可能需要阅读它。

I've also found an article regarding isolation levels and lock modes, you might want to read it.

哦,并回答你的上一个问题。

Oh, and to answer your last question.


如果我们没有定义显式锁定模式,可以数据库
完整性会丢失吗?

If we don't define an explicit locking mode, can the database integrity be lost?

取决于,但如果你有并发事务那么答案可能是

It depends, but if you have concurrent transactions then the answer is probably yes.

这篇关于JPA和默认锁定模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆