-->
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.  [ 6 posts ] 
Author Message
 Post subject: attach a session to existing connection
PostPosted: Thu Mar 20, 2008 3:43 am 
Newbie

Joined: Wed Jan 31, 2007 8:44 am
Posts: 9
I have the following problem, I'm working on a project where I need to use both nhibernate and standard ADO.NEt code.
I can create a session with SessionFactory.OpenSession passing an existing session, but if that session has a transaction open I get errors because the dbcommand used by nhibernate does not enlist itself in transaction.

Is there a possibility to tell the session to attach to an existing transaction?

I need to do these thing
have a connection and transaction opened elsewhere
doing some stuff with nhibernate on that connection and transaction
close the session
continue to work with connection and transaction.

Thanks in advance

Alk.


Top
 Profile  
 
 Post subject: attach a session to existing connection
PostPosted: Thu Mar 20, 2008 5:17 am 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi Alk,

NHibernate can use an existing connection using the overload on OpenSession() that takes an IDbConnection.

I do not believe that it can currently use an existing transaction though (as an aside, I'm not sure if this will change in NH2,since I think NH2 mandates transactions).

I could be wrong, but I think your only option is to open the transaction, then enlist the ADO.Net Command objects using ITransaction.Enlist(...). Perhaps something like:
Code:
    ISession session = sessionFactory.OpenSession();
    ITransaction transaction = session.BeginTransaction();
    ...
    transaction.Enlist(myAdoCommand);


Regards,
Richard


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 20, 2008 5:24 am 
Newbie

Joined: Wed Jan 31, 2007 8:44 am
Posts: 9
Yes, this is a viable solution, but this imply that is nhibernate that begin the Transaction, I already do this, but I wish to be able to enlis the session in existing transaction because I have some fragment of code that actually does some stuff with Standard ADO.NET, then maybe they should use nhibernate, so I wish to create the session if needed.

With this approach (that is the one I actually use) I always create session and begin transaction, then I use the connection and transaction from session to call ADO.NET code, but if the session is never needed it was created without reason. All works well, thanks for the response.

alk.


Top
 Profile  
 
 Post subject: attach a session to existing connection
PostPosted: Thu Mar 20, 2008 5:38 am 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi Alk,

ISession objects aren't expensive to create, so I imagine this will work well.

Regards,
Richard


Top
 Profile  
 
 Post subject: Re: attach a session to existing connection
PostPosted: Thu Mar 20, 2008 11:34 am 
Newbie

Joined: Wed Jan 31, 2007 8:44 am
Posts: 9
FlukeFan wrote:
Hi Alk,

ISession objects aren't expensive to create, so I imagine this will work well.

Regards,
Richard


Yes, and also this seems the only solution, except wrapping with TransactionScope, but this involves the DTC so i prefer to create the transaction from the session.

alk.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 20, 2008 2:55 pm 
Regular
Regular

Joined: Wed Aug 15, 2007 7:37 am
Posts: 73
Why wouldn't the session be able to use an existing transaction? Presumably beginning a transaction through the session isn't much different from beginning it in the ADO connection?

Steve


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