-->
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: Best strategy for high use shared data (concurrency)
PostPosted: Wed Jan 02, 2008 5:20 am 
Senior
Senior

Joined: Sat Sep 03, 2005 12:54 am
Posts: 139
Hi,

I have reviewed the docs and am unsure about the best approach for concurrency issues in my scenario.

I want to track page impressions in a daily log file by incrementing a counter. If the log file for today has not yet been created then the request should create a new log entry and set the counter to 1.

For updates, I could add a verson column to the class but I don't want to actually throw a staleobjectsexception for this use case as I am just incrementing a counter. I could trap the exception and retry the whole transaction by selecting the object again, but I guess this could happen repeatedly and I would need to throw an exception at some point rather than just looping forever!

What about preventing 2 daily log entries being created by 2 requests that both think there isn't one there yet?

Any help with this is really appreciated.

Thanks,

Jason


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 02, 2008 2:53 pm 
Beginner
Beginner

Joined: Thu Nov 02, 2006 5:11 pm
Posts: 32
Location: Toronto
Regarding preventing two daily entries from being created concurrently, you just need to either a) use the "assigned" id strategy, where you assign the object id yourself, and somehow use the current date to create an object id, or b) create another column to store today's date, and put a unique constraint on it. Either way you will get a unique constraint violation, and you can catch that exception and deal with it.

However, I really think the whole "incrementing a counter" problem is better suited to a direct SQL solution. For example, check out the TableGenerator class in the NH source code.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 04, 2008 3:52 pm 
Senior
Senior

Joined: Sat Sep 03, 2005 12:54 am
Posts: 139
Thanks for the info. The unique constraint would work well and I can then re-try the whole transaction, updating the new entries instead of trying to insert them.

I did think about using an SQL implementation, possibly using a stored procedure to perform some appropriate locking...or is locking to be avoided at all costs!?


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.