diff --git a/src/NServiceBus.SqlServer/SqlServerPollingDequeueStrategy.cs b/src/NServiceBus.SqlServer/SqlServerPollingDequeueStrategy.cs index bbfecd9b3..e73936a24 100644 --- a/src/NServiceBus.SqlServer/SqlServerPollingDequeueStrategy.cs +++ b/src/NServiceBus.SqlServer/SqlServerPollingDequeueStrategy.cs @@ -254,7 +254,7 @@ ReceiveResult TryReceiveWithNativeTransaction() try { - UnitOfWork.SetTransaction(connection.ConnectionString, transaction); + UnitOfWork.SetTransaction(transaction, connection.ConnectionString); if (tryProcessMessage(message)) { diff --git a/src/NServiceBus.SqlServer/SqlServerTransport.cs b/src/NServiceBus.SqlServer/SqlServerTransport.cs index 7aa572139..4d5fd9fa7 100644 --- a/src/NServiceBus.SqlServer/SqlServerTransport.cs +++ b/src/NServiceBus.SqlServer/SqlServerTransport.cs @@ -37,7 +37,7 @@ public override void Initialize() .ConnectionStrings .Cast() .Where(x => x.Name.StartsWith("NServiceBus/Transport/")) - .ToDictionary(x => x.Name.Replace("NServiceBus/Transport/", String.Empty), y => y.ConnectionString); + .ToDictionary(x => x.Name.Replace("NServiceBus/Transport/", String.Empty), y => y.ConnectionString); if (String.IsNullOrEmpty(defaultConnectionString)) { diff --git a/src/NServiceBus.SqlServer/UnitOfWork.cs b/src/NServiceBus.SqlServer/UnitOfWork.cs index d133cdf1a..6e25add04 100644 --- a/src/NServiceBus.SqlServer/UnitOfWork.cs +++ b/src/NServiceBus.SqlServer/UnitOfWork.cs @@ -4,9 +4,20 @@ using System.Collections.Generic; using System.Data.SqlClient; using System.Threading; + using Settings; public class UnitOfWork : IDisposable { + public UnitOfWork() + { + defaultConnectionString = SettingsHolder.Get("NServiceBus.Transport.ConnectionString"); + } + + public SqlTransaction Transaction + { + get { return GetTransaction(defaultConnectionString); } + } + public void Dispose() { //Injected @@ -17,19 +28,36 @@ public SqlTransaction GetTransaction(string connectionString) return currentTransactions.Value[connectionString]; } - public void SetTransaction(string connectionString, SqlTransaction transaction) + public void SetTransaction(SqlTransaction transaction) + { + SetTransaction(transaction, defaultConnectionString); + } + + public void SetTransaction(SqlTransaction transaction, string connectionString) { if (currentTransactions.Value.ContainsKey(connectionString)) + { throw new InvalidOperationException("Transaction already exists for connection"); + } currentTransactions.Value.Add(connectionString, transaction); } + public bool HasActiveTransaction() + { + return HasActiveTransaction(defaultConnectionString); + } + public bool HasActiveTransaction(string connectionString) { return currentTransactions.Value.ContainsKey(connectionString); } - + + public void ClearTransaction() + { + ClearTransaction(defaultConnectionString); + } + public void ClearTransaction(string connectionString) { currentTransactions.Value.Remove(connectionString); @@ -37,5 +65,7 @@ public void ClearTransaction(string connectionString) readonly ThreadLocal> currentTransactions = new ThreadLocal>(() => new Dictionary(StringComparer.InvariantCultureIgnoreCase)); + + string defaultConnectionString; } } \ No newline at end of file