Skip to content

Commit 6097c3f

Browse files
authored
Merge pull request #260 from stawr93/isolation-level
Fixed an issue #258
2 parents 4245c95 + 74e2a64 commit 6097c3f

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/Hangfire.PostgreSql/PostgreSqlStorage.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,8 @@ internal T UseTransaction<T>(DbConnection dedicatedConnection,
279279
[InstantHandle] Func<DbConnection, IDbTransaction, T> func,
280280
IsolationLevel? isolationLevel = null)
281281
{
282-
isolationLevel ??= IsolationLevel.ReadCommitted;
282+
// Use isolation level of an already opened transaction in order to avoid isolation level conflict
283+
isolationLevel ??= Transaction.Current?.IsolationLevel ?? IsolationLevel.ReadCommitted;
283284

284285
if (!EnvironmentHelpers.IsMono())
285286
{

tests/Hangfire.PostgreSql.Tests/PostgreSqlStorageFacts.cs

+15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Transactions;
45
using Hangfire.PostgreSql.Tests.Utils;
56
using Hangfire.Server;
67
using Hangfire.Storage;
@@ -145,6 +146,20 @@ public void CreateAndOpenConnection_ThrowsAnException_WithExistingConnectionFact
145146
Assert.Throws<ArgumentException>(() => storage.CreateAndOpenConnection());
146147
}
147148

149+
[Fact]
150+
public void CanUseTransaction_WithDifferentTransactionIsolationLevel()
151+
{
152+
using TransactionScope scope = new(TransactionScopeOption.Required,
153+
new TransactionOptions() { IsolationLevel = IsolationLevel.Serializable });
154+
155+
PostgreSqlStorage storage = new(new DefaultConnectionFactory(), _options);
156+
NpgsqlConnection connection = storage.CreateAndOpenConnection();
157+
158+
bool success = storage.UseTransaction(connection, (_, _) => true);
159+
160+
Assert.True(success);
161+
}
162+
148163
private PostgreSqlStorage CreateStorage()
149164
{
150165
return new PostgreSqlStorage(ConnectionUtils.GetConnectionString(), _options);

0 commit comments

Comments
 (0)