-->
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.  [ 4 posts ] 
Author Message
 Post subject: Best practice for forcing version updates
PostPosted: Wed Nov 22, 2006 9:32 pm 
Newbie

Joined: Wed Nov 22, 2006 9:13 pm
Posts: 3
Hi, I have a question about hibernate best practices, specifically, how one should force version updates. Hopefully this question isn't too long now, but I thought I should first describe my application, to give some background.

I have an application that uses a database to store a table of jobs and sub-jobs. Each job can be divided up into some number of sub-jobs (not necessarily always the same number). So there are two database tables and two corresponding entities.

Work is queried out of the sub-job table and marked first as 'working' and then finally 'done' (both the sub-job and job entities have columns for 'state'). When the very last sub-job for a job is marked as 'done', I want the corresponding job to be marked as 'done'.

Both the job entity and sub-job entity use optimistic locking/versioning with the @Version annotation.

It is critical that marking of the VERY LAST sub-job as 'done' must trigger the marking of the WHOLE job as 'done'. To ensure that this happens I force a version update on the JOB entity every time I mark a sub-job as done. That way I avoid the danger of the last two sub-job-finishing transactions completing at exactly the same time, both thinking that there is still another sub-job left to do the work of marking the job as done, and neither of them doing it. (Since every sub-job-finishing transaction updates the JOB entity, they will trip over each other's change to the Job if they should so happen to run at the same time.)

First of all, does that logic make sense? Second of all, what is the best way to force the version update? Currently I'm just updating a "last updated" column in the Job entity by setting it with new Date(). But I'm worried that that can fail in a clustered environment, if the exact timestamp value of new Date() should so happen to correspond with the value that is already in the entity. I thought another way would be to keep a boolean in the Job entity and then just always set it to its opposite.


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

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I won't comment the logic, but em.lock(obj, LockMode.WRITE) will force a version increase

_________________
Emmanuel


Top
 Profile  
 
 Post subject: EntityManager.lock()
PostPosted: Thu Nov 23, 2006 4:08 pm 
Newbie

Joined: Wed Nov 22, 2006 9:13 pm
Posts: 3
Yes, but that also locks the underlying database row, which I don't need (or desire)... is that the only way? I don't need a pessimistic lock in this scenario.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 23, 2006 5:58 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
By design, a version update will update a row, hence put a DB lock on it. There is nothing you can do about that.

_________________
Emmanuel


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