-->
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 dirty objects with LockMode.UPGRADE_NOWAIT
PostPosted: Mon Jun 04, 2007 2:41 am 
Newbie

Joined: Mon Jun 04, 2007 1:50 am
Posts: 4
Hi,
My system architecture is 3 tiers with long-lived conversation with session-per-request-with-detached-objects pattern.
So my objects is loaded in one session and saved in another session.

My application model is very complicate and includes a lot of objects graphs. The root object for each object graph declared with a version tag/field (the Customer in the example below).

If I want to ensure that when I'm saving a specific object graph, no another transaction will update the specific object graph, it's not enough to rely on the version field and I want to lock the record in the DB (select for update no wait) in the beginning of the transaction, before doing the business logic validations and saving the objects.

But when the object has dirty collections, I'm getting HibernateException: reassociated object has dirty collection.

How can I perform the locking of a dirty object?
An answer would be appreciated.
Yifat


Session session = null;
try {
// first request / session
session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Customer customer = (Customer) session.load(Customer.class, new Long(1));
Hibernate.initialize(customer);
Hibernate.initialize(customer.getAddressList());
session.getTransaction().commit();

// changing the object outside the session
customer.addAddress(new Address("myAddress"));

// second request / session
session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.lock(customer, LockMode.UPGRADE_NOWAIT);
session.saveOrUpdate(customer);
session.getTransaction().commit();

} catch (RuntimeException e) {
e.printStackTrace();
if(session != null) {
session.getTransaction().rollback();
}
}


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.