Skip to content

Commit 7f318a9

Browse files
authored
Merge pull request #137 from davidroth/transactionscope-support
Add transaction scope support
2 parents 4e6712a + 93e1f60 commit 7f318a9

9 files changed

+1198
-1050
lines changed

src/Hangfire.PostgreSql/Hangfire.PostgreSql.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
<Reference Include="System.Configuration" />
6666
<Reference Include="System" />
6767
<Reference Include="Microsoft.CSharp" />
68+
<Reference Include="System.Transactions" />
6869
</ItemGroup>
6970

7071
<ItemGroup>

src/Hangfire.PostgreSql/PostgreSqlStorage.cs

+12-7
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,13 @@ public PostgreSqlStorage(NpgsqlConnection existingConnection, PostgreSqlStorageO
111111
if (existingConnection == null) throw new ArgumentNullException(nameof(existingConnection));
112112
if (options == null) throw new ArgumentNullException(nameof(options));
113113
var connectionStringBuilder = new NpgsqlConnectionStringBuilder(existingConnection.ConnectionString);
114-
if (connectionStringBuilder.Enlist)
115-
throw new ArgumentException(
116-
"Npgsql is not fully compatible with TransactionScope yet, only connections without Enlist = true are accepted.");
114+
115+
if (!options.EnableTransactionScopeEnlistment)
116+
{
117+
if (connectionStringBuilder.Enlist)
118+
throw new ArgumentException(
119+
$"TransactionScope enlistment must be enabled by setting {nameof(PostgreSqlStorageOptions)}.{nameof(options.EnableTransactionScopeEnlistment)} to `true`.");
120+
}
117121

118122
_existingConnection = existingConnection;
119123
_options = options;
@@ -127,7 +131,7 @@ public PostgreSqlStorage(NpgsqlConnection existingConnection)
127131
var connectionStringBuilder = new NpgsqlConnectionStringBuilder(existingConnection.ConnectionString);
128132
if (connectionStringBuilder.Enlist)
129133
throw new ArgumentException(
130-
"Npgsql is not fully compatible with TransactionScope yet, only connections without Enlist = true are accepted.");
134+
$"TransactionScope enlistment must be enabled by setting {nameof(PostgreSqlStorageOptions)}.{nameof(PostgreSqlStorageOptions.EnableTransactionScopeEnlistment)} to `true`.");
131135

132136
_existingConnection = existingConnection;
133137
_options = new PostgreSqlStorageOptions();
@@ -189,10 +193,11 @@ public override string ToString()
189193

190194
internal NpgsqlConnection CreateAndOpenConnection()
191195
{
192-
var connectionStringBuilder = new NpgsqlConnectionStringBuilder(_connectionString)
196+
var connectionStringBuilder = new NpgsqlConnectionStringBuilder(_connectionString);
197+
if (!_options.EnableTransactionScopeEnlistment)
193198
{
194-
Enlist = false //Npgsql is not fully compatible with TransactionScope yet.
195-
};
199+
connectionStringBuilder.Enlist = false;
200+
}
196201

197202
var connection = new NpgsqlConnection(connectionStringBuilder.ToString());
198203

src/Hangfire.PostgreSql/PostgreSqlStorageOptions.cs

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public TimeSpan TransactionSynchronisationTimeout
8484
public bool UseNativeDatabaseTransactions { get; set; }
8585
public bool PrepareSchemaIfNecessary { get; set; }
8686
public string SchemaName { get; set; }
87+
public bool EnableTransactionScopeEnlistment { get; set; }
8788

8889
private static void ThrowIfValueIsNotPositive(TimeSpan value, string fieldName)
8990
{

0 commit comments

Comments
 (0)