简体   繁体   English

如何在 Hibernate 中使用 pooled-lo 优化器

[英]How to use the pooled-lo optimizer with Hibernate

I have a JBoss 7.1.1 running an EAR that uses the JPA.我有一个运行使用 JPA 的 EAR 的 JBoss 7.1.1。 With the JPA annotation, I use the strategy GenerationType.Table which is mapped to the org.hibernate.id.enhanced.TableGenerator .通过 JPA 注释,我使用映射到org.hibernate.id.enhanced.TableGenerator的策略GenerationType.Table

Does anyone know how to configure the persistence.xml when using the "pooled-lo" optimizer?有谁知道在使用“pooled-lo”优化器时如何配置persistence.xml

TABLE generator is a terrible choice TABLE 生成器是一个糟糕的选择

Now, before I start explaining how you can configure the pooled or pooled-lo optimizers, you should know that the TABLE generator is a terrible choice as not only that it's 10 times slower, but it can congest your database connection pool since it requires an extra connection to fetch the identifier.现在,在我开始解释如何配置pooledpooled-lo优化器之前,您应该知道TABLE生成器是一个糟糕的选择,因为它不仅慢了 10 倍,而且会使您的数据库连接池拥塞,因为它需要一个获取标识符的额外连接。

More, because it uses row-level locks to allocate the next identifier, this can lead to bottlenecks in your application.此外,因为它使用行级锁来分配下一个标识符,这可能会导致应用程序出现瓶颈。 For more details about the perils of TABLE generator.有关TABLE生成器的危险的更多详细信息。

Sequence-based optimizers基于序列的优化器

Starting with Hibernate 5, the pooled optimizer is used whenever you are setting the allocationSize attribute of the JPA @SequenceGenerator annotation to a value that's greater than 1 .从 Hibernate 5 开始,只要您将 JPA @SequenceGenerator注释的allocationSize属性设置为大于1的值,就会使用pooled优化器。

For Hibernate 4 or 3, to use the pooled or pooled-lo optimizers, you have to enable the following Hibernate property:对于 Hibernate 4 或 3,要使用pooledpooled-lo优化器,您必须启用以下 Hibernate 属性:

 <property name="hibernate.id.new_generator_mappings" value="true"/>

It's worth noting that the pooled and pooled-lo optimizers are only available for SEQUENCE and TABLE generators as the goal of these optimizers is to reduce the number of database roundtrips needed to fetch the next entity identifiers.值得注意的是, pooledpooled-lo优化器仅可用于 SEQUENCE 和 TABLE 生成器,因为这些优化器的目标是减少获取下一个实体标识符所需的数据库往返次数。

Pooled optimizer池化优化器

The pooled optimizer is very easy to set up. pooled化优化器非常容易设置。 All you need to do is set the allocationSize of the JPA @SequenceGenerator annotation, and Hibernate is going to switch to using the pooled optimizer:您需要做的就是设置 JPA @SequenceGenerator注释的allocationSize ,并且 Hibernate 将切换到使用pooled优化器:

@Id
@GeneratedValue(
    strategy = GenerationType.SEQUENCE,
    generator = "post_sequence"
)
@SequenceGenerator(
    name = "post_sequence",
    sequenceName = "post_sequence",
    allocationSize = 3
)
private Long id;

Hibernate 池化优化器

Since this mapping is more straightforward, you can switch to pooled-lo instead of pooled if you also provide this Hibernate configuration property:由于此映射更直接,如果您还提供此 Hibernate 配置属性,则可以切换到pooled-lo而不是pooled

<property name="hibernate.id.optimizer.pooled.preferred" value="pooled-lo" />

Pooled-lo optimizer池化优化器

To use the pooled-lo optimizer, the entity identifier mapping will look as follows:要使用 pooled-lo 优化器,实体标识符映射将如下所示:

@Id
@GeneratedValue(
    strategy = GenerationType.SEQUENCE,
    generator = "pooled-lo"
)
@GenericGenerator(
    name = "pooled-lo",
    strategy = "sequence",
    parameters = {
        @Parameter(
            name = "sequence_name",
            value = "post_sequence"
        ),
        @Parameter(
            name = "initial_value",
            value = "1"
        ),
        @Parameter(
            name = "increment_size",
            value = "3"
        ),
        @Parameter(
            name = "optimizer",
            value = "pooled-lo"
        )
    }
)

To understand how the pooled-lo works, check out this diagram:要了解 pooled-lo 的工作原理,请查看此图:

Hibernate pooled-lo 优化器

If you have been using the legacy hilo optimizer, you might want to switch to using pooled or pooled-lo , as hilo is not interoperable with other clients that might not be aware of the hilo identifier allocation strategy.如果您一直在使用旧的hilo优化器,您可能希望切换到使用pooledpooled-lo ,因为hilo无法与可能不知道hilo标识符分配策略的其他客户端互操作。

In the meantime i got an answer for my question.与此同时,我得到了我的问题的答案。

When you add the following line to the <properties> section in your persistent.xml file, hibernate will use the "pooled-lo" optimizer.当您将以下行添加到persistent.xml 文件的<properties>部分时,hibernate 将使用“pooled-lo”优化器。

<property name="hibernate.id.optimizer.pooled.prefer_lo" value="true" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM