Skip to content

Commit 2a709e4

Browse files
authored
Merge pull request #332 from Tinyakov/fix/TransactionScope-enlistment
Fix new jobs signalling in case of ambient transaction #331
2 parents ae22240 + cf9eab8 commit 2a709e4

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/Hangfire.PostgreSql/PostgreSqlWriteOnlyTransaction.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,26 @@ public PostgreSqlWriteOnlyTransaction(
5252
public override void Commit()
5353
{
5454
_storage.UseTransaction(_dedicatedConnectionFunc(), (connection, _) => {
55+
RegisterNewJobsEventWithTransactionCompletedEvent();
5556
foreach (Action<IDbConnection> command in _commandQueue)
5657
{
5758
command(connection);
5859
}
5960
}, CreateTransactionScope);
60-
61-
// Triggers signals for all queues to which jobs have been added in this transaction
62-
_queuesWithAddedJobs.ForEach(PostgreSqlJobQueue._queueEventRegistry.Set);
63-
_queuesWithAddedJobs.Clear();
61+
}
62+
63+
private void RegisterNewJobsEventWithTransactionCompletedEvent()
64+
{
65+
// TransactionCompleted event is required here, because if this TransactionScope is enlisted
66+
// within an ambient TransactionScope, the ambient TransactionScope controls when the TransactionScope completes.
67+
Transaction.Current.TransactionCompleted += (_, args) => {
68+
if (args.Transaction.TransactionInformation.Status == TransactionStatus.Committed)
69+
{
70+
// Triggers signals for all queues to which jobs have been added in this transaction
71+
_queuesWithAddedJobs.ForEach(PostgreSqlJobQueue._queueEventRegistry.Set);
72+
_queuesWithAddedJobs.Clear();
73+
}
74+
};
6475
}
6576

6677
private TransactionScope CreateTransactionScope()

0 commit comments

Comments
 (0)