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: Threading issue
PostPosted: Sat Feb 11, 2006 2:09 pm 
Newbie

Joined: Sat Feb 11, 2006 1:56 pm
Posts: 6
Hi,

I am using version 1.0.1.0 of NHibernate, and I am using NHibernate on a second thread. Inside the Threads executing method, I have a loop:

try {
for(int i=0; i < callsToMake; i++){
CreateSession();
GetSession().SaveOrUpdate(GetObjectToSave());
CloseSession();
Sleep();
}
}
catch(Exception e) {
Trace.WriteLine(e.Message);
}


CreateSession just creates a vanilla session.
CloseSession flushes the session a and then closes it.
Sleep puts the thread into sleep mode.

Typically the first two iterations around the loop succeed, and then after that either the Thread.Sleep call fails with the error Thread Aborted, or the thread just ends up inside the catch. Is there anything I am doing wrong here? I have tried this with transactions, but that was even worse so I pulled them out.

Thanks

Nick.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 12, 2006 7:13 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Can you post full code?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 12, 2006 9:08 am 
Newbie

Joined: Sat Feb 11, 2006 1:56 pm
Posts: 6
Hi, here is the thread code - the CustomerData class is a simple value object, with getters and setters.

public class ActiveUser
{
public ActiveUser(int customerID, ISessionFactory sessionFactory)
{
_customerID = customerID;
_activityThread = new Thread(new ThreadStart(_Execute));
_activityThread.Name = customerID.ToString();
_sessionFactory = sessionFactory;
}

public void Start()
{
_activityThread.Start();
}

private void _Execute()
{
_running = true;
try
{
Random r = new Random((int)DateTime.Now.Ticks);
_callsToMake = r.Next(1000);
try
{
for(_callsMade = 0; _callsMade < _callsToMake; _callsMade++)
{
CreateSession();
GetSession().SaveOrUpdate(GetObjectToSave());
CloseSession();
Sleep();
}
}
catch(Exception e)
{
Trace.WriteLine(e.Message);
}
}
finally
{
_running = false;
}
}

public bool IsRunning
{
get { return _running; }
}

public void WaitTillFinished()
{
while(IsRunning)
{
Thread.Sleep(10);
}
}

public int CallsMade
{
get
{
return _callsMade;
}
}

private void CreateSession()
{
_session = _sessionFactory.OpenSession();
}

private ISession GetSession()
{
return _session;
}

private void CloseSession()
{
_session.Flush();
_session.Close();
}

private void Sleep()
{
Thread.Sleep(500);
}

private CustomerRealtimeData GetObjectToSave()
{
return new CustomerRealtimeData(_customerID, _callsToMake - _callsMade, -1);
}

private int _customerID;
private Thread _activityThread;
private bool _running;
private int _callsMade;
private ISessionFactory _sessionFactory;
private ISession _session;
private int _callsToMake;
}


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 12, 2006 6:14 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Hmm, I can't see anything wrong in here, if it works with a single thread, then it should work with multiple threads too. Try printing the full exception stack trace, including inner exceptions. And also try enabling logging and look at the output.


Top
 Profile  
 
 Post subject: Fixed, well sort of.
PostPosted: Tue Dec 26, 2006 12:44 pm 
Newbie

Joined: Tue Dec 26, 2006 11:32 am
Posts: 4
Hello,


I happen to run into the same issue.
As always with the multitheading issue, the description of the problem is somewhat complex.

My application is a web application in ASP.NET 2.0 using Nhibernate 1.0.3 + Sqlite.
The purpose of the program is to be able to retreive code from various source control system (CVS and SVN). This operation could be long so we decided to go multithreaded and spawns various processes (svn.exe + cvs.exe) with the appropriate command line options. The checkout is know to be state=in progress.

The application registers a call back when the processes end/fail and update the state of the checkouts (state=done/failed)

Everything ran fine until we introduced Nhibernate to handle our persistance.
I love Nhibernate. Used many times before. But I have to recognize that the mix mutlithreading+nhibernate+ado.net didn't work out.

When using Nh, the call back failed to be triggered. We tried everything. And only removing Nh and re-introducing it little by little helped.

So we narrowed down the problem to a saveOrUpdate called before the new process is launched. We use a HTTP module to manage the session/session factories and took extra care not to mess with sessions and threads.

We were desperate until we found the following article in msdn and give the aspcompat a try.

http://support.microsoft.com/?scid=kb%3 ... 7&x=9&y=11

And it worked !

I'm not sure I understood the very causes of the pb, but, hell, now everything is fine.

Go figure,
Bertrand.


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.