-->
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.  [ 2 posts ] 
Author Message
 Post subject: Can Nhibernate participate in existing transactions?
PostPosted: Sun Dec 11, 2005 6:26 am 
Newbie

Joined: Sat Sep 17, 2005 12:48 am
Posts: 4
Hi,

I have a scenario where I would like to use both a stored procedure (using ADO.NET) and Nhibernate to perform some data manipulation.
Is it possible to perform these activities within the same transaction:

For example,

Code:
SqlConnection conn;
SqlTransaction trans;
ISession session;

try
{
   conn = new SqlConnection(...);
   conn.open();
   trans = conn.BeginTransaction();

   // ***** STORED PROCEDURE CODE *******
   // ...(call stored procedure and do some work)...


   // ***** HIBERNATE CODE *******   
   // How do I tell hibernate to enlist itself inside the transaction created above???
   // I thought the following would work:

   session = sessionFactory.CreateSession(conn);
   session.Save(obj);
   session.Flush(); // But this fails.
   
   trans.Commit();
}
catch(Exception)
{
   // I want to rollback changes made via the stored procedure and hibernate.
   trans.Rollback();
}
finally
{
   session.close();
   conn.close();
}



I hope you can understand what I mean. Any help will be greatly appreciated.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 12, 2005 4:42 am 
Regular
Regular

Joined: Fri Jun 11, 2004 6:27 am
Posts: 81
Location: Yaroslavl, Russia
Most likely, your code would work as is.
From the other hand, if you want NH to know about the transaction, you could first create a session, then start the transaction with ISession.BeginTransaction, then do your call to SP (obtain connection from session) and then save and flush.

_________________
Best,
Andrew Mayorov // BYTE-force


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