-->
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: Concurrency problem leaving database in inconsistent state.
PostPosted: Tue Nov 17, 2009 3:30 pm 
I'm having a problem that's not Hibernate specific, though I'm using Hibernate and dont know if it has something builtin that can help. If this doesn't belong here then I apologize.

In general the problem is the following.

I have the following tables

TABLE A
-----------
AMMOUNT : NUMBER

TABLE B
------------
PAYMENT_AMMOUNT : NUMBER

I would be adding payments to table B that have a many to one relationship with table A. The SUM of payments added cannot exceed AMMOUNT from table A. So before inserting I have to sum the payments I have thuse far and compare with AMMOUNT, this is fine but if another user makes an insert around the same time i'll potentially end up in an inconsistent state with payments exceeding my ammount.

My questions is how can I handle this is general and/or specifically with Hibernate.

I need a nudge in the right direction.

Regards,
Daniel


Top
  
 
 Post subject: Re: Concurrency problem leaving database in inconsistent state.
PostPosted: Tue Nov 17, 2009 4:23 pm 
Newbie

Joined: Wed Jun 14, 2006 7:40 pm
Posts: 8
You need to obtain pessimistic lock on the master row in TABLE A before doing anything on TABLE B - and you have to do that always when you want to insert/update PAYMENT_AMOUNT in table B


Top
 Profile  
 
 Post subject: Re: Concurrency problem leaving database in inconsistent state.
PostPosted: Tue Nov 17, 2009 4:24 pm 
Newbie

Joined: Wed Jun 14, 2006 7:40 pm
Posts: 8
P.S. You can use pessimistic features like SELECT FOR UPDATE if your database allows it.


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.