-->
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.  [ 1 post ] 
Author Message
 Post subject: Locking objects with multiple sessions
PostPosted: Tue Jun 15, 2004 5:07 am 
Newbie

Joined: Tue Jun 15, 2004 4:48 am
Posts: 1
Hello,

i'm trying to keep the locking state of an hibernate object synchronized across two sessions. I want to lock an object in the first session and check in the second session whether this object is locked or not. The aim is to prevent multiple users to change the same object.

Code to check / set lock:

Code:
if (HibernateAccessor.getInstance().getLockMode((Feld)getUserObject()) != LockMode.UPGRADE) {
HibernateAccessor.getInstance().setLockMode((Feld)getUserObject(), LockMode.UPGRADE);
   // [object is now locked]
} else {      
   // [object was locked in other session]          
}

class HibernateAccessor {

public LockMode getLockMode(Object objectToGetLockModeFor) {
LockMode lockMode = null;
try {
lockMode = session.getCurrentLockMode(objectToGetLockModeFor);
} catch (HibernateException e) {
e.printStackTrace();
}
return lockMode;
}
   
public void setLockMode(Object objectToSetLockMode, LockMode lockMode) {
try {
session.lock(objectToSetLockMode, lockMode);
} catch (HibernateException e) {
e.printStackTrace();
}
}
}



Locking the object in the first session works. But asking, if the object is locked in the second session fails: getCurrentLockMode() always returns LockMode.READ, indicating that this object is not locked. But when trying to lock the object again in the second session the session.lock() blocks. No Error/Exception is thrown. After closing the first session, the blocking session.lock() continues.

How do I handle this concurrency issue in a correct way?

I'm using Hibernate 2.1.2 on an Oracle 8.1.7 Database


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.