From: <jul...@us...> - 2010-12-01 16:03:38
|
Revision: 5285 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=5285&view=rev Author: julian-maughan Date: 2010-12-01 16:03:31 +0000 (Wed, 01 Dec 2010) Log Message: ----------- Fix to correctly set the IsInActiveTransaction property of a Session's TransactionContext, on TransactionCompletion. This prevent an exception from being thrown by ConnectionManager.Disconnect. Occurs when a database connection enlisted in a distributed transaction is passed to the NHibernate Session (ref. NH-2420) Modified Paths: -------------- trunk/nhibernate/src/NHibernate/Transaction/AdoNetWithDistributedTransactionFactory.cs trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj Added Paths: ----------- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2420/ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2420/DummyEnlistment.cs trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2420/Fixture.cs trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2420/Mappings.hbm.xml trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2420/MyTable.cs Modified: trunk/nhibernate/src/NHibernate/Transaction/AdoNetWithDistributedTransactionFactory.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Transaction/AdoNetWithDistributedTransactionFactory.cs 2010-11-29 12:59:16 UTC (rev 5284) +++ trunk/nhibernate/src/NHibernate/Transaction/AdoNetWithDistributedTransactionFactory.cs 2010-12-01 16:03:31 UTC (rev 5285) @@ -42,6 +42,8 @@ { using (new SessionIdLoggingContext(session.SessionId)) { + ((DistributedTransactionContext)session.TransactionContext).IsInActiveTransaction = false; + bool wasSuccessful = false; try { @@ -134,7 +136,7 @@ using (new SessionIdLoggingContext(sessionImplementor.SessionId)) { logger.Debug("committing DTC transaction"); - // we have nothing to do here, since it is the actual + // we have nothing to do here, since it is the actual // DB connection that will commit the transaction enlistment.Done(); IsInActiveTransaction = false; Added: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2420/DummyEnlistment.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2420/DummyEnlistment.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2420/DummyEnlistment.cs 2010-12-01 16:03:31 UTC (rev 5285) @@ -0,0 +1,30 @@ +using System; +using System.Transactions; + +namespace NHibernate.Test.NHSpecificTest.NH2420 +{ + public class DummyEnlistment : IEnlistmentNotification + { + public static readonly Guid Id = new Guid("E2D35055-4187-4ff5-82A1-F1F161A008D0"); + + public void Prepare(PreparingEnlistment preparingEnlistment) + { + preparingEnlistment.Prepared(); + } + + public void Commit(Enlistment enlistment) + { + enlistment.Done(); + } + + public void Rollback(Enlistment enlistment) + { + enlistment.Done(); + } + + public void InDoubt(Enlistment enlistment) + { + enlistment.Done(); + } + } +} \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2420/Fixture.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2420/Fixture.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2420/Fixture.cs 2010-12-01 16:03:31 UTC (rev 5285) @@ -0,0 +1,69 @@ +using System.Data; +using System.Data.SqlClient; +using System.Transactions; + +using NHibernate.Criterion; +using NHibernate.Dialect; +using NUnit.Framework; + +namespace NHibernate.Test.NHSpecificTest.NH2420 +{ + [TestFixture] + public class Fixture : BugTestCase + { + public override string BugNumber + { + get { return "NH2420"; } + } + + protected override bool AppliesTo(NHibernate.Dialect.Dialect dialect) + { + return (dialect is NHibernate.Dialect.MsSql2008Dialect || dialect is NHibernate.Dialect.MsSql2005Dialect); + } + + [Test] + [Explicit("Requires the Microsoft DTC service. Also, the exception thrown by this test when it fails is on a ThreadPool thread which is not visible to NUnit. So although the test accurately reproduces the issue, it passes anyway.")] + public void Bug() + { + string connectionString = cfg.GetProperty("connection.connection_string"); + int id = -1; + + using (TransactionScope ts = new TransactionScope()) + { + // Enlisting DummyEnlistment as a durable resource manager will start + // a DTC transaction + System.Transactions.Transaction.Current.EnlistDurable( + DummyEnlistment.Id, + new DummyEnlistment(), + EnlistmentOptions.None); + + using (IDbConnection connection = new SqlConnection(connectionString)) + { + connection.Open(); + using (ISession s = Sfi.OpenSession(connection)) + { + id = (int)s.Save(new MyTable() { String = "hello!" }); + } + connection.Close(); + } + + // Prior to the patch, an InvalidOperationException exception would occur in the + // TransactionCompleted delegate at this point with the message, "Disconnect cannot + // be called while a transaction is in progress". Although the exception can be + // seen reported in the IDE, NUnit fails to see it, and the test passes. The + // TransactionCompleted event fires *after* the transaction is committed and so + // it doesn't affect the success of the transaction + ts.Complete(); + } + } + + protected override void OnTearDown() + { + using (ISession s = OpenSession()) + { + s.Delete("from MyTable"); + s.Flush(); + } + } + } +} \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2420/Mappings.hbm.xml =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2420/Mappings.hbm.xml (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2420/Mappings.hbm.xml 2010-12-01 16:03:31 UTC (rev 5285) @@ -0,0 +1,14 @@ +<?xml version="1.0" encoding="utf-8" ?> +<hibernate-mapping + xmlns="urn:nhibernate-mapping-2.2" + assembly="NHibernate.Test" + namespace="NHibernate.Test.NHSpecificTest.NH2420"> + + <class name="MyTable" table="MyTable"> + <id name="Id"> + <generator class="native" /> + </id> + <property name="String"/> + </class> + +</hibernate-mapping> \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2420/MyTable.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2420/MyTable.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2420/MyTable.cs 2010-12-01 16:03:31 UTC (rev 5285) @@ -0,0 +1,10 @@ +using System; + +namespace NHibernate.Test.NHSpecificTest.NH2420 +{ + public class MyTable + { + public virtual int Id { get; private set; } + public virtual string String { get; set; } + } +} \ No newline at end of file Modified: trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2010-11-29 12:59:16 UTC (rev 5284) +++ trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2010-12-01 16:03:31 UTC (rev 5285) @@ -556,6 +556,9 @@ <Compile Include="NHSpecificTest\NH2409\Message.cs" /> <Compile Include="NHSpecificTest\NH2409\MessageReading.cs" /> <Compile Include="NHSpecificTest\NH2409\User.cs" /> + <Compile Include="NHSpecificTest\NH2420\DummyEnlistment.cs" /> + <Compile Include="NHSpecificTest\NH2420\Fixture.cs" /> + <Compile Include="NHSpecificTest\NH2420\MyTable.cs" /> <Compile Include="PolymorphicGetAndLoad\Domain.cs" /> <Compile Include="PolymorphicGetAndLoad\PolymorphicGetAndLoadTest.cs" /> <Compile Include="TypesTest\CharClass.cs" /> @@ -1849,6 +1852,7 @@ <EmbeddedResource Include="NHSpecificTest\NH1869\Mappings.hbm.xml" /> <EmbeddedResource Include="NHSpecificTest\NH2392\Mappings.hbm.xml" /> <EmbeddedResource Include="NHSpecificTest\NH2409\Mappings.hbm.xml" /> + <EmbeddedResource Include="NHSpecificTest\NH2420\Mappings.hbm.xml" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\NHibernate.ByteCode.Castle\NHibernate.ByteCode.Castle.csproj"> @@ -2715,6 +2719,7 @@ <EmbeddedResource Include="DynamicEntity\Tuplizer\Customer.hbm.xml" /> </ItemGroup> <ItemGroup> + <Folder Include="NHSpecificTest\NH2420" /> <Folder Include="Properties\" /> </ItemGroup> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |