Skip to content

Commit db52ac3

Browse files
authored
Lock persistence journal table on write (#6639)
1 parent e4ac8fd commit db52ac3

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/contrib/persistence/Akka.Persistence.Sql.Common/Journal/QueryExecutor.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -676,8 +676,11 @@ public virtual async Task<long> SelectHighestSequenceNrAsync(DbConnection connec
676676
/// <returns>TBD</returns>
677677
public virtual async Task InsertBatchAsync(DbConnection connection, CancellationToken cancellationToken, WriteJournalBatch write)
678678
{
679-
using (var command = GetCommand(connection, InsertEventSql))
680-
using (var tx = connection.BeginTransaction())
679+
using var command = GetCommand(connection, InsertEventSql);
680+
// Isolation.Serializable: Enforce a table lock during the transaction when we're writing the event to database
681+
// This is to prevent a racy condition when a high volume write is coupled with eager reading of the table
682+
// that results in an event row missing on the read side because 2 transactions were completed out of order
683+
using (var tx = connection.BeginTransaction(IsolationLevel.Serializable))
681684
{
682685
command.Transaction = tx;
683686

0 commit comments

Comments
 (0)