Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sequence generator starts at the wrong number #168

Closed
ktisdell opened this issue Jul 8, 2013 · 6 comments
Closed

Sequence generator starts at the wrong number #168

ktisdell opened this issue Jul 8, 2013 · 6 comments

Comments

@ktisdell
Copy link
Contributor

ktisdell commented Jul 8, 2013

When the SEQUENCE_GENERATOR table is populated the actual starting point for generating values is some number less than that. For example for OrderImpl, if your starting point in SEQUENCE_GENERATOR is 1000, then the actual starting point is 950.

@apazzolini
Copy link

These three commits should have targeted #178. The commit message is incorrect

@phillipuniverse
Copy link
Contributor

@ktisdell unsure where the bug relies. This seems like it might be something out of our control. Any additional thoughts?

@ktisdell
Copy link
Contributor Author

ktisdell commented Nov 1, 2013

Not sure. I assumed that since we implement the org.broadleafcommerce.common.persistence.IdOverrideTableGenerator, that it might be in this code. It just doesn't make sense that if we specify that a sequence should start at 1000, with a batch size of 50, that the first sequence generated is 950. If there's nothing that we can do about it, then we should document it. It's just something that I noticed and logged.

@jefffischer
Copy link
Member

Somewhere along the way, Hibernate changed their default behavior to use a high range for the segment value, rather than an lo (which is what you expected). Both are acceptable as long as you know what the behavior is. It can be left as the default, or switched (I suggest leaving it, since it’s Hibernate’s default behavior). The lo behavior can be achieved by editing persistence.xml. I did so by editing the persistence.xml in the core module of demosite as follows:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">

    <persistence-unit name="blPU" transaction-type="RESOURCE_LOCAL">
        <non-jta-data-source>jdbc/web</non-jta-data-source>
        <exclude-unlisted-classes/>
        <properties>
            <property name="hibernate.id.optimizer.pooled.prefer_lo" value="true"/>
        </properties>
    </persistence-unit>

    <persistence-unit name="blSecurePU" transaction-type="RESOURCE_LOCAL">
        <non-jta-data-source>jdbc/webSecure</non-jta-data-source>
        <exclude-unlisted-classes/>
        <properties>
            <property name="hibernate.id.optimizer.pooled.prefer_lo" value="true"/>
        </properties>
    </persistence-unit>

    <persistence-unit name="blCMSStorage" transaction-type="RESOURCE_LOCAL">
        <non-jta-data-source>jdbc/cmsStorage</non-jta-data-source>
        <exclude-unlisted-classes/>
        <properties>
            <property name="hibernate.id.optimizer.pooled.prefer_lo" value="true"/>
        </properties>
    </persistence-unit>
</persistence>

As a result, this is not a bug, but does deserve documentation. Another issue will be opened for this documentation enhancement.

@elbertbautista
Copy link
Member

referencing the Hibernate issue where this was documented: https://hibernate.atlassian.net/browse/HHH-5218

@elbertbautista
Copy link
Member

Just adding a summary of the algorithm used by hibernate to generate the ID's using the default (high strategy) PooledOptimizer, given the record in the DB is "1000" for the entity before the app starts and the batch allocation size is set to "50"

  1. JVM starts
  2. First attempt of creating a new entity. It fetches the "1000" from the Sequence Generator
  3. 1000 is the hi (by default), so hibernate will reserve a cache of 50 id's, and it will start with 951 and go to 1000
  4. As part of grabbing this batch, it sets the hi in the SG table to a new value 50 ahead (i.e. 1050) so that another server will have that new hi when it needs to get a batch. Therefore, another server will go from 1001 to 1050.

Here is the actual PooledOptimizer class:
https://github.com/hibernate/hibernate-orm/blob/a2287b6c6d43b44f656632503401b7968c42bd86/hibernate-core/src/main/java/org/hibernate/id/enhanced/PooledOptimizer.java

Here is the actual PooledLoOptimizer class:
https://github.com/hibernate/hibernate-orm/blob/a2287b6c6d43b44f656632503401b7968c42bd86/hibernate-core/src/main/java/org/hibernate/id/enhanced/PooledLoOptimizer.java

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants