-->
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.  [ 9 posts ] 
Author Message
 Post subject: Multi Threading - error please help
PostPosted: Wed Jul 05, 2006 1:42 am 
Newbie

Joined: Wed Jul 05, 2006 1:35 am
Posts: 9
Hi i am using hibernate in a multi threading application. One Session obj
per thread. I am doing a batch insert and i got this error

006-07-05 13:10:44,921 ERROR [org.hibernate.AssertionFailure] an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
org.hibernate.AssertionFailure: possible nonthreadsafe access to session
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:55)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.lta.sep.mms.persistence.SepTxnStore.commit(SepTxnStore.java:78)
at com.lta.sep.mms.MmsMsgProcessor.process(MmsMsgProcessor.java:159)
at com.lta.sep.mms.MmsMessageListener$MmsThread.run(MmsMessageListener.java:134)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:650)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:675)
at java.lang.Thread.run(Thread.java:595)

Please help me


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 05, 2006 9:55 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
To hard to help. You know the session is not multi-thread safe. You say you have only one thread accessing the session. So there should not be an issue but.... make sure this is the case. Often we use thread locals to guarantee thats session cannot be co-accessed. I also assume your not re-using the session once the thread is done with it.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 06, 2006 9:03 pm 
Newbie

Joined: Wed Jul 05, 2006 1:35 am
Posts: 9
Any suggestion i can do multi threading using hibernate.. or sample code

need your help very badly...

thanks

abe


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 06, 2006 10:31 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Its fine if you remember that the Session is not thread safe (but the SessionFactory is thread safe). Once you have a Session make sure its isolated. The best way is to store it in a thread local so only that thread will be able to get at the session when it requests it. There are examples on the wiki. You issue will most likely be related to the way your code is structured.
Build a simple example and test it using multiple threads.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 06, 2006 11:57 pm 
Newbie

Joined: Wed Jul 05, 2006 1:35 am
Posts: 9
Hi again, i cant find a sample code of multi threading. Can you
give me a simple approached that i can play arroung. Just the algothim
would do..

thanks

abe


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 08, 2006 12:57 am 
Newbie

Joined: Wed Jul 05, 2006 1:35 am
Posts: 9
Hi i was able to put my session in threadlocal when i tried
to run a batch insert i got this error in the session.flush -

possible nonthreadsafe access to session

how can i solve this??


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 09, 2006 9:04 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
You need to show a small snipit of code that represents what you are doing.
Show where you start the TX (get the session) through to Commit/rollback.
There is no other way for people on this forum to help you further.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 09, 2006 11:22 pm 
Newbie

Joined: Wed Jul 05, 2006 1:35 am
Posts: 9
david wrote:
You need to show a small snipit of code that represents what you are doing.
Show where you start the TX (get the session) through to Commit/rollback.
There is no other way for people on this forum to help you further.


HI David..

Here's the picture of what i am doing..

My session is in threadlocal..

Each thread process will process will have this process

process(Session session){
tx = session.beginTransaction();

for(i=0;i<500;i++)
{
getEmployeeFromFile(fileList);
session.save(Employee);
}

session.flush();
session.clear();
tx.commit();
session.close();
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 10, 2006 3:02 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
This pseudo code is thread safe and it must work for you:

Code:
process(SessionFactory factory){

Session session = factory.openSession(); // local
try{

Transaction tx = session.beginTransaction(); //  local 
boolean dirty = true;
  try{

  //your code

  tx.commit();
  dirty = false;

  }finally{

  if(dirty){
    tx.roolback();

  }

  }

}finally{
   session.close();
}
}


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