-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 
Author Message
 Post subject: Oracle Seqeunce increment by 50
PostPosted: Tue Jun 06, 2006 6:21 pm 
Newbie

Joined: Tue Dec 06, 2005 8:47 am
Posts: 16
Location: London
I have just created my first EJB3 Entity bean using the oracle seqeunce. Every time i start the app server and insert a new record the id generated is incremented by 50 instead of using the seqeunce value from the database. I have noticed that the stategy being used is org.hibernate.id.SequenceHiLoGenerator. Is there any way of changing this to use the database sequence value instead of one provided by the app server or the entity manager.


Top
 Profile  
 
 Post subject: I have the same problem, more background
PostPosted: Wed Jun 21, 2006 9:41 pm 
Newbie

Joined: Wed Jun 21, 2006 9:20 pm
Posts: 2
After I switched to annotations from having mapping information in hibernate.cfg.xml, Hibernate isn't using the sequence generated number for the id. I followed the example in Chapter 1 of the Hibernate Reference Documentation, and when the mapping for the Event class was in hibernate.cfg.xml it worked just fine. As soon as I switched to annotations the objects get stored in the database with ids completely different from what are being generated by the sequence, and they increment by 50, as the previous poster experienced. I'm using hibernate-3.2 and hibernate-annotations-3.2.0.CR1. Here's the annotated class:

Code:
@Entity
public class Event {
    private long id;
    private String title;
    private Date eventDate;

    public Event() {}

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="eventIdSequence")
    @SequenceGenerator(name="eventIdSequence", sequenceName="EVENT_ID")
    public long getId() {
        return id;
    }
... other setters and getters


This is what I notice in the log:

Code:
DEBUG JDBCContext: - after transaction begin
DEBUG ThreadLocalSessionContext: - allowing proxied method [save] to proceed to real session
DEBUG DefaultSaveOrUpdateEventListener: - saving transient instance
DEBUG AbstractBatcher: - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
DEBUG SQL: - select EVENT_ID.nextval from dual
Hibernate: select EVENT_ID.nextval from dual
DEBUG AbstractBatcher: - preparing statement
DEBUG SequenceGenerator: - Sequence identifier generated: 41
DEBUG AbstractBatcher: - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
DEBUG AbstractBatcher: - closing statement
DEBUG SequenceHiLoGenerator: - new hi value: 41
DEBUG AbstractSaveEventListener: - generated identifier: 2050, using strategy: org.hibernate.id.SequenceHiLoGenerator
DEBUG AbstractSaveEventListener: - saving [event.Event#2050]


Notice that HIbernate is getting the right sequence number (41), but then is using SequenceHiLoGenerator, which makes the id 2050. If I run the example again the sequence will generate 42 and the id will be 2100. Any ideas on what I might be doing incorrectly?


Top
 Profile  
 
 Post subject: The Solution
PostPosted: Thu Jun 22, 2006 4:05 am 
Newbie

Joined: Tue Dec 06, 2005 8:47 am
Posts: 16
Location: London
I have posted this on the EJB3 forum and the response i received was to set the allocationSize=1 in the @SequenceGenerator(name="ID_SEQ",sequenceName="IDSEQ",allocationSize=1)


Top
 Profile  
 
 Post subject: Thanks
PostPosted: Thu Jun 22, 2006 6:53 pm 
Newbie

Joined: Wed Jun 21, 2006 9:20 pm
Posts: 2
That fixed it. Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 16, 2006 11:28 pm 
Newbie

Joined: Wed May 03, 2006 5:09 pm
Posts: 17
Is this bug going to be fixed?

I can't understand why it doesn't just pull the value from the Oracle sequence. When we reviewed the logs after getting the same error, it appeared to actually select the value from the sequence, then this AbstractSaveEventListener class picked a random value using strategy: org.hibernate.id.SequenceHiLoGenerator. Any information on why it behaves this way would be appreciated.

Drew


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 17, 2006 3:59 am 
Newbie

Joined: Tue Dec 06, 2005 8:47 am
Posts: 16
Location: London
I have been informed that this is not a bug but the behavior. The app server will set the sequence number to +50 every time the server is started. This is to avoid trips to the database for a sequence number.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 23, 2006 1:12 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
use @µSequenceGenerator(increment=1) if you don't want the default behavior

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Re: Oracle Seqeunce increment by 50
PostPosted: Mon Feb 01, 2010 2:20 pm 
Newbie

Joined: Tue Jan 01, 2008 3:09 pm
Posts: 2
Location: Warsaw, Poland
SequenceHiLoGenerator is the default sequence generator for JPA. Some guys reported this in Hibernate JIRA and it was declared not a bug, that is, SequenceHiLoGenerator is the default by design and will stay like this: http://opensource.atlassian.com/project ... se/ANN-354

Why has it been decided so?

Probably because it offers performance gains in large scale distributed environments.

Using the hi/lo algorithm allows the application on the given node (in a cluster) to avoid communicating with the database each time it needs a new identifier - depending on allocationSize.

For example, with the default allocationSize value of 50, it needs to select a next value from the sequence only every 50th generated identifier.

It works like this:

    1) Gets a "hi" value from the database sequence
    2) Multiplies the "hi" value by allocationSize (50 by default)
    3) Uses the resulting value as the first generated identifier.
    4) When a new identifier is needed, increments the value by 1 without contacting the database, until the pool of 50 available values is depleted. Note that these values are guaranteed to be unique because other nodes in the cluster will reserve other number pools, since they've multiplied other database sequence values by 50.
    5) When all 50 values have been used up, it's time to contact the database for a new sequence value that reserves another pool of 50 numbers

Of course, this clever scheme works only as a gentlemen's agreement between all application nodes in the cluster. If any of those starts using a different allocationSize value, the calculated identifiers will sooner or later start colliding.

So the most foolproof behaviour is with allocationSize = 1, which goes back to getting a sequence number from database for each identifier.


Top
 Profile  
 
 Post subject: Re: Oracle Seqeunce increment by 50
PostPosted: Mon Feb 01, 2010 2:20 pm 
Newbie

Joined: Tue Jan 01, 2008 3:09 pm
Posts: 2
Location: Warsaw, Poland
BTW, I've reported a JIRA on clarifying this in the JavaDOCs:

http://opensource.atlassian.com/project ... e/HHH-4871


Top
 Profile  
 
 Post subject: Re:
PostPosted: Tue Jul 06, 2010 11:10 am 
Newbie

Joined: Tue Jul 06, 2010 10:49 am
Posts: 1
emmanuel wrote:
use @µSequenceGenerator(increment=1) if you don't want the default behavior


Did you mean allocationSize instead of increment? I can't find any annotation that uses increment....


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.