-->
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: Problem with OpenSession
PostPosted: Thu Jun 08, 2006 7:24 am 
Newbie

Joined: Wed Apr 26, 2006 3:18 pm
Posts: 8
I am attempting to create an Interceptor in order to do some auditing (similar to the example shown in 'Hibernate in Action'.

I have statement similar to this:

ISession auditSession = NHibUtil.SessionFactory.OpenSession(session.Connection);


Where session is the main outer session which I pass in when instantiating the interceptor. I am using SQL Server.

However, when I come to use the auditSession (such as auditSession.Save(anObject) for example). I get the following exception:

System.InvalidOperationException: ExecuteReader requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized.

I suspect this is because the auditSession does not have a reference to the outer sessions transaction.

What am I doing wrong? Has anybody got a solution to this problem?

Thanks in advance ......


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 09, 2006 8:34 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
You have to open a new connection for the new session as well, using the same connection won't work.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 09, 2006 8:42 am 
Newbie

Joined: Wed Apr 26, 2006 3:18 pm
Posts: 8
Ok ... thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 13, 2006 6:35 am 
Newbie

Joined: Wed Apr 26, 2006 3:18 pm
Posts: 8
Is it not possible to use the underlying session connection at all then i.e. if I wanted to use ADO.NET in the same transaction as the session?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 13, 2006 7:06 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
It is possible if that's what you want. Your example didn't work because it essentially tried to do two transactions on one connection simultaneously and ADO.NET doesn't support that.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 13, 2006 10:03 am 
Newbie

Joined: Wed Apr 26, 2006 3:18 pm
Posts: 8
How do I get the underlying ADO.NET transaction from the session so that my subsequent ADO.NET command can execute in the same transaction?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 14, 2006 3:04 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
ISession.Connection contains the IDbConnection object that the session is using. If you're using a transaction and are executing an external command you'll need to enlist the command in the connection's transaction using ITransaction.Enlist(command).

Cheers,

Symon.


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.