-->
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.  [ 6 posts ] 
Author Message
 Post subject: SequenceStyleGenerator is thread safe ?
PostPosted: Sun Sep 14, 2008 10:08 pm 
Newbie

Joined: Fri Sep 12, 2008 3:53 pm
Posts: 4
Hibernate version:
3.2.4.sp1

Hello All,

during load testing I found that our application (Hibernate + Spring + Oracle) fails with unique constraint violation errors. I found that id generator for primary keys we use generates same id for different threads.

Quote:
......
<id name="tid" type="java.lang.Long">
<column name="TID" precision="16" scale="0" />
<generator class="org.hibernate.id.enhanced.SequenceStyleGenerator">
<param name="sequence_name">TEST_SEQ</param>
<param name="initial_value">1</param>
<param name="increment_size">100</param>
<param name="optimizer">hilo</param>
</generator>
.......
</id>



I started to dig in into hibernate code and it looks to me that "SequenceStyleGenerator" is not thread safe (I hope that I'm wrong because I could not find anything on this forum related to this problem). I don't see the other way how to make this class thread safe but to create my own implementation.


So my question to hibernate gurus : is SequenceStyleGenerator thread safe? If answer is not what is common practice for id generators in multithreaded environment.

tnx,
timour


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 14, 2008 10:23 pm 
Regular
Regular

Joined: Wed Apr 25, 2007 11:44 pm
Posts: 59
dude i have used that with hibernate 3.0 n had no problem in stress testing or heavy load on the server. check that there are no issues at your end


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 15, 2008 9:01 am 
Newbie

Joined: Fri Sep 12, 2008 3:53 pm
Posts: 4
I hope it's a problem on my side , but here is what I see in hibernate code ...

1. stack trace for the SequenceStyleGenerator.generate method:

Code:
at org.hibernate.id.enhanced.SequenceStyleGenerator.generate( ... )
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:99)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)


I don't see synchronization on SequenceStyleGenerator instance on the stack.

2. SequenceStyleGenerator instance is bound to SessionFactory not Session, so it is shared across multiple threads.

3. Only one instance of SequenceStyleGenerator is created per entity, so again instance is shared across multiple threads per entity type.


what am i missing ?


tnx.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 20, 2008 5:50 am 
Newbie

Joined: Fri Sep 12, 2008 5:40 am
Posts: 4
Hi buddy,

We've the same problem in our system, and we found that the problem is because we've used both Hibernate and Native SQL to get Sequence value.

Assume the sequence start from 0 and increment by "100", if you issue a SQL statement to get a sequence and insert a record into the database, the value should be 100. Subsequently, if you use Hibernate to insert another record, you'll find that the SequenceStyleGenerator get 200 from the sequence, buy it'll use 100 to 199, which casued the unique constraint violation problem.

You may have to implement another generator yourself, or, if skipping number is not a problem to your system, you may set the database increment by N, and set the SequenceStyleGenerator increment by N-1.

Good luck.

ALan Cheung


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 20, 2008 9:47 am 
Newbie

Joined: Fri Sep 12, 2008 3:53 pm
Posts: 4
We moved to Hibernate 3.3.1 and I can't reproduce this problem.

I believe this issue was fixed in 3.3.0.CR1. Here is , I think, the actual fix:
http://viewvc.jboss.org/cgi-bin/viewvc.cgi/hibernate/core/branches/Branch_3_2/src/org/hibernate/id/enhanced/OptimizerFactory.java?r1=11765&r2=14182&pathrev=14182
.... both methods are synchronized now.


ALan,
which version of hibernate are you using ? Can you reproduce this problem with 3.3.1 ?

tnx,
Timour


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 24, 2008 10:53 pm 
Newbie

Joined: Fri Sep 12, 2008 5:40 am
Posts: 4
hi,

We're using 3.3.0 SP1. Yes, you're right, I believed that the generator is thread safe. We just have the problem if we have something directly using JDBC to insert records with the same database sequence of the generator.

Before reading the code, I thought the generator will use 51 to 100 if it gets 100 from the database sequence. (given the "increment by" is 50). However, the fact is, it's using 50 to 99.

This behavior should have no problem if we just use hibernate, but you'll have a problem if you mix JDBC and Hibernate together.

just my two cents.

ALan Cheung


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.