-->
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.  [ 7 posts ] 
Author Message
 Post subject: Sharing Hibernate Session between mutiple thread
PostPosted: Wed Nov 26, 2008 10:18 am 
Newbie

Joined: Tue Nov 18, 2008 4:50 am
Posts: 3
Location: Ahmedabad
Hi,

Can i share hibernate session between multiple thread to save data?like...

ArrayList<Thread> threadList = new ArrayList<Thread>();

Transaction tx = session.beginTransaction(); // Start Transaction
for(Object object : objectList ){
PersistantThread persistantThread = new PersistantThread(session,object);
threadList.add(persistantThread);
persistantThread.start();
}
for(Thread thread : threadList){
thread.join();
}
session.flush();
tx.commit();

class PersistantThread extends Thread {
Object object = null;
Session session = null;

public PersistantThread(Session session,Object object){
this.session = session;
this.object = object;
}

public void run(){
try{
session.save(object);
}catch(Exception ex){
ex.printStackTrace();
}
}
}

In this case i am getting error like
sometime it is "org.hibernate.AssertionFailure: entity was persistent" and sometime "org.hibernate.AssertionFailure: object already associated, but no entry was found".

Can anyone have any idea how can we share the hibernate session with mutiple thread.

Thanks in Advance.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 26, 2008 4:08 pm 
Beginner
Beginner

Joined: Mon Nov 19, 2007 4:40 pm
Posts: 46
To my knowledge, the session was not designed that way and I really think you should rethink your design. If you go forward with this, I would strongly suggest that you wrap the the hibernate Session and synchronize methods. Expect to have problems though.

My advice is rethink your design.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 27, 2008 12:23 am 
Newbie

Joined: Tue Nov 18, 2008 4:50 am
Posts: 3
Location: Ahmedabad
pppoole wrote:
To my knowledge, the session was not designed that way and I really think you should rethink your design. If you go forward with this, I would strongly suggest that you wrap the the hibernate Session and synchronize methods. Expect to have problems though.

My advice is rethink your design.


Thanks pppoole.

So i need to either synchronize the code block or to make it single threaded. like this, right ?

public void run(){
try{
synchronized (session){
session.save(object);
}

}catch(Exception ex){
ex.printStackTrace();
}
}

Is there any other way to achieve multiple threading ?

Or any other way to speedup the db inserts?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 28, 2008 12:59 am 
Beginner
Beginner

Joined: Mon Nov 19, 2007 4:40 pm
Posts: 46
What I really meant by wrapper was to create an object that encapsulates the session and provides synchronized methods that delegate the calls to the hibernate session...

Code:
MySession {
   private Session;
   ....
   public synchronized save(...) {
      session.save();
   }
   ....
}


Again, I would recommend you rethink the design and not try to access the session with multiple threads. I can't see how you will easily coordinate the object states between the two states...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 28, 2008 4:36 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I have to agree with pppoole. Don't try to make sessions work in multiple threads. It will give you problems sooner or later. Some things like lazy loading are also hard to synchronize, since it may happen anytime you call a getter method on an object.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 03, 2008 3:12 pm 
Beginner
Beginner

Joined: Mon Nov 19, 2007 4:40 pm
Posts: 46
atulprajapati - if you have found my answers helpful, please rate it. It is frustrating to answer posts and then not get any credit...

Thanks.


Top
 Profile  
 
 Post subject: Re: Sharing Hibernate Session between mutiple thread
PostPosted: Wed Jul 31, 2013 8:12 am 
Newbie

Joined: Tue May 18, 2010 5:39 am
Posts: 19
@Beginner : If you have to use synchronized that way, then you better not use multiple threads. Its as good as a single thread.

Lazy initialization may be a problem, but then, in case of bulk inserts/updates for e.g., this is very efficient. You probably may need to make sure that the objects you spread across the threads have no linkling to each other. If they have, they could overrun each other results unpredictably without any notice.

So, its a good idea to do it, but firstly you need to know really good what you are doing, and do it in exceptional cases (e.g. write behinds, batch processing, etc.)

_________________
AG
http://anshuiitk.blogspot.com


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