Don't use Transactionscope for distributed transactions as it is not
supported and will fail.
The only thing that works but isn't a full distributed transaction is
to do something like:
using (var nhSession1 = OpenSession())
using (var nhSession2 = OpenSession())
using (var txSession1 = nhSession1.BeginTransaction())
using (var txSession2 = nhSession1.BeginTransaction())
{
//perform operations and flush session
//perform operations and flush session
// Any flush errors from previous lines would result in a rollback
txSession1.Commit();
txSession2.Commit();
}
Still... commit could fail but this probability is very small. If you
need a real distributed transaction that don't use NHibernate or hack
into the NHibernate objects with reflection to let the underlying
connection be part of a distributed transaction.
--
Ramon
On Oct 16, 5:26 pm, Tyler Burd <[EMAIL PROTECTED]> wrote:
> I'm trying to use System.Transactions with NHibernate (actually, I'm trying
> to use NHibernate/ActiveRecord with NServiceBus, which surrounds message
> handling with a TransactionScope). I am running off of the trunk. I'm
> having a few issues, unfortunately.
>
> Basically, I'm trying to do this (pseudo code):
>
> using (var tx = new TransactionScope())
> {
> using (var nhSession1 = OpenSession())
> {
> //perform operations and flush session
> }
> using (var nhSession2 = OpenSession())
> {
> //perform operations and flush session
> }
>
> }
>
> So a System.Transactions.TransactionScope already exists when I open and
> close my NH sessions. Ideally, I'd like those sessions to take part in the
> larger distributed transaction provided by the ambient TransactionScope.
> Whenever a session is disposed of, though, I get this exception:
>
> "System.InvalidOperationException: Disconnect cannot be called while a
> transaction is in progress.
> at NHibernate.AdoNet.ConnectionManager.Disconnect() in
> d:\oss\nhibernate\src\NHibernate\AdoNet\ConnectionManager.cs:line 156
> at NHibernate.AdoNet.ConnectionManager.Close() in
> d:\oss\nhibernate\src\NHibernate\AdoNet\ConnectionManager.cs:line 122
> at NHibernate.Impl.SessionImpl.Close() in
> d:\oss\nhibernate\src\NHibernate\Impl\SessionImpl.cs:line 314
> at NHibernate.Impl.SessionImpl.Dispose(Boolean isDisposing) in
> d:\oss\nhibernate\src\NHibernate\Impl\SessionImpl.cs:line 1373
> at NHibernate.Impl.SessionImpl.Dispose() in
> d:\oss\nhibernate\src\NHibernate\Impl\SessionImpl.cs:line 1349
> at
> Castle.ActiveRecord.Framework.SessionFactoryHolder.ReleaseSession(ISession
> session) in
> d:\oss\castle-trunk\ActiveRecord\Castle.ActiveRecord\Framework\SessionFacto
> ryHolder.cs:line 258
> at Castle.ActiveRecord.ActiveRecordBase.FindByPrimaryKey(Type targetType,
> Object id, Boolean throwOnNotFound) in
> d:\oss\castle-trunk\ActiveRecord\Castle.ActiveRecord\Framework\ActiveRecord
> Base.cs:line 1161
> at Castle.ActiveRecord.ActiveRecordMediator.FindByPrimaryKey(Type
> targetType, Object id, Boolean throwOnNotFound) in
> d:\oss\castle-trunk\ActiveRecord\Castle.ActiveRecord\Framework\ActiveRecord
> Mediator.cs:line 54
> at Rhino.Commons.ARRepository`1.Load(Object id) in
> d:\oss\rhino-tools\rhino-commons\Rhino.Commons.ActiveRecord\Repositories\AR
> Repository.cs:line 59"
>
> I have tried setting the connection.release_mode property to "on_close", and
> I have even tried nesting a TransactionScope with TransactionOption.Suppress
> (so there should be NO ambient transaction), but NHibernate still gives me
> that exception.
>
> I see from the NHibernate.Test.SystemTransactions.TransactionFixture tests
> that opening a TransactionScope within an open session works, but shouldn't
> opening and closing a session within a TransactionScope also work? Using raw
> ADO I can open and close connections within a TransactionScope and everything
> works fine.
>
> I'd greatly appreciate it if someone could help me identify whether a bug
> exists in NH or if I'm simply overlooking something. Thanks.
>
> -tyler
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---