-->
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.  [ 5 posts ] 
Author Message
 Post subject: Multithreading
PostPosted: Mon Aug 30, 2010 6:41 am 
Newbie

Joined: Mon Aug 30, 2010 3:57 am
Posts: 4
Hello,
is it save to use this? If not, is there any other solution how to sumbit object to a different thread?

Quote:
// get session, begin transaction
final SomeObject someObject = (SomeObject) session.get(SomeObject.class, id);

// modifying object, updating to DB

new Thread(new Runnable() {
public void run() {
// only reading object properties, no modifying, no saving to DB
System.out.println(someObject.getId());
}
}).start();

// commit transaction, close session


Thank you


Top
 Profile  
 
 Post subject: Re: Multithreading
PostPosted: Mon Aug 30, 2010 1:14 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Well... if you only read the id (or other properties) of the object it should be safe. It is not safe if you read other values which may trigger lazy initialization of associations or collections.


Top
 Profile  
 
 Post subject: Re: Multithreading
PostPosted: Mon Aug 30, 2010 4:47 pm 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
Hibernate and especially its session is not thread safe. Even just accessing the ID of an instance may cause lazy loading or similar things to happen, which might cause problem when running in parallel in multiple threads.

If you want the entity to be used in a different thread you have various options:

* detach the entity from the session, by removing it from the session or closing the session
* copy it in a new instance, e.g. a transfer object
*make sure only one thread uses a session at any given time, including all entities attached to that session.


Top
 Profile  
 
 Post subject: Re: Multithreading
PostPosted: Mon Aug 30, 2010 5:03 pm 
Newbie

Joined: Mon Aug 30, 2010 3:57 am
Posts: 4
schauder wrote:
Hibernate and especially its session is not thread safe. Even just accessing the ID of an instance may cause lazy loading or similar things to happen, which might cause problem when running in parallel in multiple threads.


In case I'm accessing regular properties there should be no lazy loading or similar things, am I right? (I'm using session.get())

schauder wrote:
If you want the entity to be used in a different thread you have various options:

* detach the entity from the session, by removing it from the session or closing the session
* copy it in a new instance, e.g. a transfer object
*make sure only one thread uses a session at any given time, including all entities attached to that session.


OK, thanks for your suggestions. Cloning an object seems to be easiest solution in my case.


Top
 Profile  
 
 Post subject: Re: Multithreading
PostPosted: Mon Aug 30, 2010 5:08 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
Even just accessing the ID of an instance may cause lazy loading


No, it doesn't, as is documented in http://docs.jboss.org/hibernate/stable/ ... ng-proxies (final part just before section 20.1.4)
And since the session.get() method is used this should mean that at least the "someObject" entity is initialized so it should be safe to read other properties than the id (assuming that no properties have been declared as lazy).

Quote:
detach the entity from the session, by removing it from the session or closing the session


This will not work if accessing uninitialized proxies or collections. If the entity is detached it will result in a LazyInitializationException. The code example in the original post seems to suggest that the transaction is committed and the session is closed. So the thread actually has a reference to a detached object. And this is probably a "good" thing since a LazyInitializationException is easier to fix than some strange errors that only happen sometimes due to threading issues.
It might be better to move the commit and close to before the thread is started though.


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