有什么区别:使用JPA @TableGenerator的序列ID,@ GeneratedValue与数据库Auto_Increment [英] What are the difference between: sequence id using JPA @TableGenerator, @GeneratedValue vs database Auto_Increment

查看:284
本文介绍了有什么区别:使用JPA @TableGenerator的序列ID,@ GeneratedValue与数据库Auto_Increment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Q1。:使用

A <在数据库中应用序列ID之间的区别是什么/ p>

A.

CREATE TABLE Person
(
   id long NOT NULL AUTO_INCREMENT
   ...
   PRIMARY KEY (id)
)

B。

@Entity
public class Person {
    @Id
    @TableGenerator(name="TABLE_GEN", table="SEQUENCE_TABLE", pkColumnName="SEQ_NAME",
        valueColumnName="SEQ_COUNT", pkColumnValue="PERSON_SEQ")
    @GeneratedValue(strategy=GenerationType.TABLE, generator="TABLE_GEN")
    private long id;
    ...
}

我的系统高度并发即可。由于我的数据库是Microsoft SQL服务器,我不认为它支持 @SequenceGenerator ,所以我必须留在 @TableGenerator 容易出现并发问题。

My system is highly concurrent. Since my DB is a Microsoft SQL server, I do not think it supports @SequenceGenerator, so I have to stay with @TableGenerator which is prone to concurrency issues.

Q2。此处此链接( http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Advanced_Sequencing )表明 B 可能会遇到并发问题,但是我不明白建议的解决方案。如果有人能向我解释如何避免使用 B 的并发问题,我将不胜感激。以下是他们解决方案的片段:

Q2. This link here (http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Advanced_Sequencing) suggests that B might suffer from concurrency issues, but I do not understand the proposed solution. I would greatly appreciate it if someone could explain to me how to avoid concurrency issues with B. Here is a snippet of their solution:

如果使用大的序列预分配大小,这就不再是问题,因为序列表很少访问。

Q2.1 :我们在这里讨论的分配大小是多少?我应该 allocationSize = 10 allocationSize = 100

Q2.1: How much allocation size are we talking about here? Should I do allocationSize=10 or allocationSize=100?

某些JPA提供程序使用单独的(非JTA)连接来分配序列ID,避免或限制此问题。在这种情况下,如果使用JTA数据源连接,则必须在persistence.xml中包含非JTA数据源连接。

Q2.2 :我使用EclipseLink作为我的提供者;我必须做上面建议的吗?

Q2.2: I use EclipseLink as my provider; do I have to do what it suggests above?

Q3。如果 B 遭遇并发问题,那么 A 受到同样的影响?

Q3. If B suffers from concurrency issues, does A suffer the same?

推荐答案

答:使用IDENTITY id生成, @ GeneratedValue(IDENTITY)

A: uses IDENTITY id generation, @GeneratedValue(IDENTITY)

B:使用TABLE id生成

B: uses TABLE id generation

JPA支持三类型, IDENTITY,SEQUENCE和TABLE。

JPA supports three types, IDENTITY, SEQUENCE and TABLE.

两者都存在权衡。

IDENTITY不允许预分配,因此在每次INSERT之后需要额外的SELECT,防止批量写入,并且需要刷新来访问id,这可能导致并发性差。

IDENTITY does not allow preallocation, so requires an extra SELECT after every INSERT, prevents batch writing, and requires a flush to access the id which may lead to poor concurrency.

TABLE允许预分配,但序列表上的锁可能会出现并发问题。

TABLE allows preallocation, but can have concurrency issues with locks on the sequence table.

技术上SEQUENCE id生成是最好的,但不是全部数据库支持它。

Technically SEQUENCE id generation is the best, but not all databases support it.

使用TABLE排序,如果你使用100的preallocaiton大小,那么只有100个内存rts将锁定序列表中的行,因此只要您通常不同时有100个插入,您就不会遭受任何并发损失。如果你的应用程序执行了大量的插入操作,可能使用1000或更大的值。

With TABLE sequencing if you use a preallocaiton size of 100, then only every 100 inserts will lock the row in the sequence table, so as long as you don't commonly have 100 inserts at the same time, you will not suffer any loss in concurrency. If you application does a lot of inserts, maybe use 1000 or larger value.

EclipseLink将使用单独的事务进行TABLE排序,因此任何并发问题都会锁定序列表将减少。如果您正在使用JTA,那么您需要指定一个非jta-datasource来执行此操作并在persistence.xml属性中配置序列连接池。

EclipseLink will use a separate transaction for TABLE sequencing, so any concurrency issue with locks to the sequence table will be reduced. If you are using JTA, then you need to specify a non-jta-datasource to do this and configure a sequence-connection-pool in your persistence.xml properties.

这篇关于有什么区别:使用JPA @TableGenerator的序列ID,@ GeneratedValue与数据库Auto_Increment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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