-->
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.  [ 3 posts ] 
Author Message
 Post subject: How to increment a counter properly?
PostPosted: Tue Mar 17, 2009 7:51 am 
Newbie

Joined: Thu Mar 12, 2009 3:41 pm
Posts: 9
Hi,

I have the following problem and I hope someone can provide me some hints or a solution.

I have an entity with a composite id containing a string and a counter.
The entity is called order. The key contains the section for this order and an incremental counter.
Like this:

Order (entity)
|- OrderPK (Serializable)
…..|- Section (String)
…..|- Id (Long)


On my normal entities I let the database increment the counter with <generator class=”increment”/>. However this is not a possible solution as the Id should be incremented for each section (to see how many orders for this selection were made already).


The only way I see currently is to load the last number from the database (for example by select max(..)….) and increment it in my own business code and save the new order.

And here comes my questions:

1) is there an easier solution than mine

2) which possibilities do I have to avoid problems with concurrent threads? (Let’s say worker A reads the last number for section “france” and worker B does the same exactly the same moment. Both sessions increase the counter by one and try to save their new orders. The second one will get an exception.)


Any help would be great.


Best regards
Tom


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 17, 2009 8:23 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Quote:
1) is there an easier solution than mine

I am afraid there is no easier solution. See these, they may give you some pointers.
http://opensource.atlassian.com/project ... e/HHH-2060
http://forum.hibernate.org/viewtopic.php?p=2374438

Quote:
2) which possibilities do I have to avoid problems with concurrent threads? (Let’s say worker A reads the last number for section “france” and worker B does the same exactly the same moment. Both sessions increase the counter by one and try to save their new orders. The second one will get an exception.)

I think in a loop you can keep on trying incrementing the count till the save is successful.

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 18, 2009 3:40 am 
Newbie

Joined: Thu Mar 12, 2009 3:41 pm
Posts: 9
As always great and fast support here ;-)

I can see I have to increment my counter "by hand".

I'm currently thinking of introducing a persistence class with section and current counter in it.
I could query that class with hibernate and increment its counter.
So I don't have to use max(..) in my query.

One additional question there:
what does Session.lock do and could I use it for the concurrenxcy problem?

Maybe I could avoid the concurrent problem by simply locking the persistence class which holds my counter value.
I would update the counter and write the change to database.

But I'm not sure how it is technically handled by hibernate. Are database locks used and what happens in clustered environments.


best regards
tom


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