-->
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: Passing IDbTransaction to NHibernate
PostPosted: Mon Feb 12, 2007 2:04 pm 
Newbie

Joined: Mon Feb 12, 2007 10:53 am
Posts: 6
Hi,

I have the following situation:

1) Create an OracleConnection (or an IDbConnection)
2) Create an OracleTransaction (or an IDbTransaction)
3) Execute a command in that transaction's context
4) Create an NHibernate ISession using the IDbConnection (ISessionFactory.OpenSession(IDbConnection))
5) Save some entity using NHibernate's Session (ISession.Save(Object))
6) Commit the transaction (IDbTransaction or NHibernate.ITransaction)

This doesn't work! I get an error (in step 5) complaining about a Transaction that's not associated with the Command object.

So, how can i create an IDbTransaction and share that transaction with NHibernate? This must be possible, right?

Regards...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 12, 2007 3:17 pm 
Regular
Regular

Joined: Mon Mar 20, 2006 10:49 pm
Posts: 59
One option is to do the following

1) Create an OracleConnection (or an IDbConnection)
2) Create an NHibernate ISession using the IDbConnection (ISessionFactory.OpenSession(IDbConnection))
3) Create a NHibernate transaction (ISession.BeginTransaction())
4) Create a command in that transaction's context using something like the code snippet below
5) Save some entity using NHibernate's Session (ISession.Save(Object))
6) Commit the transaction (IDbTransaction or NHibernate.ITransaction)

Code:
            IDbCommand cmd = session.Connection.CreateCommand();
            if (session.Transaction.IsActive)
                session.Transaction.Enlist(cmd);


Mike A.

_________________
Mike Abraham


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 12, 2007 3:20 pm 
Regular
Regular

Joined: Mon Mar 20, 2006 10:49 pm
Posts: 59
One option is to do the following

1) Create an OracleConnection (or an IDbConnection)
2) Create an NHibernate ISession using the IDbConnection (ISessionFactory.OpenSession(IDbConnection))
3) Create a NHibernate transaction (ISession.BeginTransaction())
4) Create a command in that transaction's context using something like the code snippet below
5) Save some entity using NHibernate's Session (ISession.Save(Object))
6) Commit the transaction (IDbTransaction or NHibernate.ITransaction)

Code:
            IDbCommand cmd = session.Connection.CreateCommand();
            if (session.Transaction.IsActive)
                session.Transaction.Enlist(cmd);


Mike A.

_________________
Mike Abraham


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 13, 2007 6:22 am 
Newbie

Joined: Mon Feb 12, 2007 10:53 am
Posts: 6
Ok, but that doesn't solve my problem. I simply have an IDbTransaction (that comes from other places) and i want to use that transaction within NHibernate. Isn't this possible?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 14, 2007 6:13 pm 
Beginner
Beginner

Joined: Thu Oct 19, 2006 1:03 pm
Posts: 29
Marco Mendonça wrote:
Ok, but that doesn't solve my problem. I simply have an IDbTransaction (that comes from other places) and i want to use that transaction within NHibernate. Isn't this possible?


Perhaps this article helps:
http://www.codeproject.com/csharp/compl ... _trans.asp


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 15, 2007 2:30 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Quote:
I simply have an IDbTransaction (that comes from other places) and i want to use that transaction within NHibernate. Isn't this possible?

No, this is not possible currently. Feel free to file a JIRA issue.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 16, 2007 3:35 pm 
Newbie

Joined: Wed Feb 14, 2007 6:24 pm
Posts: 5
This would be a good enhancement. It would allow me to write an auditor interceptor and have the audit entries commit in the same transaction as the changes being audited.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 19, 2007 5:41 pm 
Beginner
Beginner

Joined: Thu Nov 02, 2006 5:11 pm
Posts: 32
Location: Toronto
I have filed a JIRA issue for this (NH-917), since no one seems to have created one yet.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 20, 2007 10:33 pm 
Beginner
Beginner

Joined: Mon Aug 15, 2005 11:09 pm
Posts: 23
Hey,

I just ran into this with regards to auditing as well. I've basically made my own audit component, but it's conceptional the same as the suggested hibernate auditing method: http://www.hibernate.org/318.html.

At the moment I'm using a separate session to grab the existing db data. But locking on dirty objects that got flushed more than once per transaction was stopping me grabbing the previous state data. Essentially the app was deadlocking. So I've had to change my transaction isolation level from ReadCommitted to ReadUncommitted which I'm not that happy about. I'd rather create a separate session which piggy backed on the same connection and transaction. That way I could set the isolation level to ReadCommitted.


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.