From 0bad6f31399af3e5a95a341be7e345b14fb60e7a Mon Sep 17 00:00:00 2001 From: Ramon Smits Date: Thu, 24 Dec 2015 10:40:09 +0100 Subject: [PATCH 1/4] Enforcing atleast NServiceBus v5.2.8, fixes #140 --- packaging/nuget/nservicebus.nhibernate.nuspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/nuget/nservicebus.nhibernate.nuspec b/packaging/nuget/nservicebus.nhibernate.nuspec index 7d7828446..c2f25a43d 100644 --- a/packaging/nuget/nservicebus.nhibernate.nuspec +++ b/packaging/nuget/nservicebus.nhibernate.nuspec @@ -17,7 +17,7 @@ - + From 5753761cd435a048d89f3d53e4439fe9c0c6e1a0 Mon Sep 17 00:00:00 2001 From: Ramon Smits Date: Tue, 29 Dec 2015 15:07:56 +0100 Subject: [PATCH 2/4] Issue #142 --- .../SharedSession/OpenSessionBehavior.cs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/NServiceBus.NHibernate/SharedSession/OpenSessionBehavior.cs b/src/NServiceBus.NHibernate/SharedSession/OpenSessionBehavior.cs index a50f8c584..ab4fb6c45 100644 --- a/src/NServiceBus.NHibernate/SharedSession/OpenSessionBehavior.cs +++ b/src/NServiceBus.NHibernate/SharedSession/OpenSessionBehavior.cs @@ -59,6 +59,8 @@ public void Invoke(IncomingContext context, Action next) void InnerInvoke(BehaviorContext context, Action next, Func connectionRetriever) { + Lazy lazyTransaction = null; + var lazySession = new Lazy(() => { var session = SessionCreator != null @@ -69,9 +71,11 @@ void InnerInvoke(BehaviorContext context, Action next, Func conne if (Transaction.Current == null) { - var tx = session.BeginTransaction(); - context.Set(LazyNHibernateTransactionKey, tx); + lazyTransaction = new Lazy(() => session.BeginTransaction()); + lazyTransaction.Value.ToString(); + context.Set(LazyNHibernateTransactionKey, lazyTransaction); } + return session; }); @@ -83,9 +87,9 @@ void InnerInvoke(BehaviorContext context, Action next, Func conne if (lazySession.IsValueCreated) { lazySession.Value.Flush(); - ITransaction tx; - if (context.TryGet(LazyNHibernateTransactionKey, out tx)) + if (lazyTransaction != null && lazyTransaction.IsValueCreated) { + var tx = lazyTransaction.Value; tx.Commit(); tx.Dispose(); } @@ -95,9 +99,9 @@ void InnerInvoke(BehaviorContext context, Action next, Func conne { if (lazySession.IsValueCreated) { - ITransaction tx; - if (context.TryGet(LazyNHibernateTransactionKey, out tx)) + if (lazyTransaction != null && lazyTransaction.IsValueCreated) { + var tx = lazyTransaction.Value; tx.Dispose(); } lazySession.Value.Dispose(); From 7c6fc4566683cb49ac498ee49b265275cd8f4c5c Mon Sep 17 00:00:00 2001 From: SzymonPobiega Date: Mon, 22 Feb 2016 07:00:01 +0100 Subject: [PATCH 3/4] Preventing OutboxCleaner from killing an endpoint in case of persistent connection problems. --- src/NServiceBus.NHibernate/Outbox/NHibernateOutboxStorage.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NServiceBus.NHibernate/Outbox/NHibernateOutboxStorage.cs b/src/NServiceBus.NHibernate/Outbox/NHibernateOutboxStorage.cs index 2bcc0937d..1f37641c7 100644 --- a/src/NServiceBus.NHibernate/Outbox/NHibernateOutboxStorage.cs +++ b/src/NServiceBus.NHibernate/Outbox/NHibernateOutboxStorage.cs @@ -106,7 +106,7 @@ void PerformCleanup(object state) if (cleanupFailures >= 10) { criticalError.Raise("Failed to clean expired Outbox records after 10 consecutive unsuccessful attempts. The most likely cause of this is connectivity issues with your database.", ex); - throw; + cleanupFailures = 0; } } } From 9f20f1619c2815a0a97d30e5ee3b6aa0de5abed0 Mon Sep 17 00:00:00 2001 From: SzymonPobiega Date: Mon, 22 Feb 2016 11:01:27 +0100 Subject: [PATCH 4/4] Reverted previous fix for #142 and applied another that changes NHibernateStorageContext --- ...rviceBus.NHibernate.AcceptanceTests.csproj | 1 + ...sing_NHibernate_persistence_without_DTC.cs | 80 +++++++++++++++++++ .../NHibernateStorageContext.cs | 12 +-- .../SharedSession/OpenSessionBehavior.cs | 16 ++-- 4 files changed, 93 insertions(+), 16 deletions(-) create mode 100644 src/NServiceBus.NHibernate.AcceptanceTests/When_using_NHibernate_persistence_without_DTC.cs diff --git a/src/NServiceBus.NHibernate.AcceptanceTests/NServiceBus.NHibernate.AcceptanceTests.csproj b/src/NServiceBus.NHibernate.AcceptanceTests/NServiceBus.NHibernate.AcceptanceTests.csproj index f5db7075c..6c76aafb5 100644 --- a/src/NServiceBus.NHibernate.AcceptanceTests/NServiceBus.NHibernate.AcceptanceTests.csproj +++ b/src/NServiceBus.NHibernate.AcceptanceTests/NServiceBus.NHibernate.AcceptanceTests.csproj @@ -211,6 +211,7 @@ + diff --git a/src/NServiceBus.NHibernate.AcceptanceTests/When_using_NHibernate_persistence_without_DTC.cs b/src/NServiceBus.NHibernate.AcceptanceTests/When_using_NHibernate_persistence_without_DTC.cs new file mode 100644 index 000000000..5aed90b89 --- /dev/null +++ b/src/NServiceBus.NHibernate.AcceptanceTests/When_using_NHibernate_persistence_without_DTC.cs @@ -0,0 +1,80 @@ +namespace NServiceBus.AcceptanceTests.Basic +{ + using System; + using NServiceBus.AcceptanceTesting; + using NServiceBus.AcceptanceTests.EndpointTemplates; + using NServiceBus.Persistence.NHibernate; + using NUnit.Framework; + + public class When_using_NHibernate_persistence_without_DTC : NServiceBusAcceptanceTest + { + [Test] + public void Should_be_able_to_retrieve_database_transaction() + { + var context = new Context + { + Id = Guid.NewGuid() + }; + + Scenario.Define(context) + .WithEndpoint(b => b.Given((bus, c) => + { + bus.Send(m=> + { + m.Id = c.Id; + }); + })) + .WithEndpoint() + .Done(c => c.Received) + .Run(); + } + + public class Context : ScenarioContext + { + public bool Received { get; set; } + + public Guid Id { get; set; } + } + + public class Sender : EndpointConfigurationBuilder + { + public Sender() + { + EndpointSetup() + .AddMapping(typeof(Receiver)); + } + } + + public class Receiver : EndpointConfigurationBuilder + { + public Receiver() + { + EndpointSetup(c => + { + c.Transactions().DisableDistributedTransactions(); + }); + } + + public class MyMessageHandler : IHandleMessages + { + public Context Context { get; set; } + + public NHibernateStorageContext StorageContext { get; set; } + + public void Handle(MyMessage message) + { + if (Context.Id != message.Id) + return; + + Assert.IsNotNull(StorageContext.DatabaseTransaction); + Context.Received = true; + } + } + } + + public class MyMessage : IMessage + { + public Guid Id { get; set; } + } + } +} diff --git a/src/NServiceBus.NHibernate/NHibernateStorageContext.cs b/src/NServiceBus.NHibernate/NHibernateStorageContext.cs index f77144180..92e023586 100644 --- a/src/NServiceBus.NHibernate/NHibernateStorageContext.cs +++ b/src/NServiceBus.NHibernate/NHibernateStorageContext.cs @@ -45,10 +45,10 @@ public IDbTransaction DatabaseTransaction { using (var command = Connection.CreateCommand()) { - Lazy lazy; - if (pipelineExecutor.CurrentContext.TryGet(string.Format("LazyNHibernateTransaction-{0}", connectionString), out lazy)) + ITransaction transaction; + if (pipelineExecutor.CurrentContext.TryGet(string.Format("LazyNHibernateTransaction-{0}", connectionString), out transaction)) { - lazy.Value.Enlist(command); + transaction.Enlist(command); return command.Transaction; } } @@ -80,10 +80,10 @@ public ITransaction Transaction { get { - Lazy lazy; - if (pipelineExecutor.CurrentContext.TryGet(string.Format("LazyNHibernateTransaction-{0}", connectionString), out lazy)) + ITransaction transaction; + if (pipelineExecutor.CurrentContext.TryGet(string.Format("LazyNHibernateTransaction-{0}", connectionString), out transaction)) { - return lazy.Value; + return transaction; } throw new InvalidOperationException("No transaction available"); diff --git a/src/NServiceBus.NHibernate/SharedSession/OpenSessionBehavior.cs b/src/NServiceBus.NHibernate/SharedSession/OpenSessionBehavior.cs index ab4fb6c45..a50f8c584 100644 --- a/src/NServiceBus.NHibernate/SharedSession/OpenSessionBehavior.cs +++ b/src/NServiceBus.NHibernate/SharedSession/OpenSessionBehavior.cs @@ -59,8 +59,6 @@ public void Invoke(IncomingContext context, Action next) void InnerInvoke(BehaviorContext context, Action next, Func connectionRetriever) { - Lazy lazyTransaction = null; - var lazySession = new Lazy(() => { var session = SessionCreator != null @@ -71,11 +69,9 @@ void InnerInvoke(BehaviorContext context, Action next, Func conne if (Transaction.Current == null) { - lazyTransaction = new Lazy(() => session.BeginTransaction()); - lazyTransaction.Value.ToString(); - context.Set(LazyNHibernateTransactionKey, lazyTransaction); + var tx = session.BeginTransaction(); + context.Set(LazyNHibernateTransactionKey, tx); } - return session; }); @@ -87,9 +83,9 @@ void InnerInvoke(BehaviorContext context, Action next, Func conne if (lazySession.IsValueCreated) { lazySession.Value.Flush(); - if (lazyTransaction != null && lazyTransaction.IsValueCreated) + ITransaction tx; + if (context.TryGet(LazyNHibernateTransactionKey, out tx)) { - var tx = lazyTransaction.Value; tx.Commit(); tx.Dispose(); } @@ -99,9 +95,9 @@ void InnerInvoke(BehaviorContext context, Action next, Func conne { if (lazySession.IsValueCreated) { - if (lazyTransaction != null && lazyTransaction.IsValueCreated) + ITransaction tx; + if (context.TryGet(LazyNHibernateTransactionKey, out tx)) { - var tx = lazyTransaction.Value; tx.Dispose(); } lazySession.Value.Dispose();