Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,28 +78,32 @@ public async ValueTask PersistAsync(MessageEnvelope envelope, CancellationToken

var connection = (NpgsqlConnection)_originalDbContext.Database.GetDbConnection();

if (connection.State != System.Data.ConnectionState.Open)
try
{
await connection.OpenAsync(cancellationToken);
}
await _originalDbContext.Database.OpenConnectionAsync(cancellationToken);

await using var writer = new Utf8JsonWriter(_arrayWriter);
writer.WriteEnvelope(envelope);
writer.Flush(); // we know it's not async
await using var writer = new Utf8JsonWriter(_arrayWriter);
writer.WriteEnvelope(envelope);
writer.Flush(); // we know it's not async

// Execute the INSERT command
await using var command = connection.CreateCommand();
command.CommandText = _insertSql;
command.Parameters.AddWithValue("@id", NewVersion());
command.Parameters.Add(
new NpgsqlParameter("@envelope", NpgsqlDbType.Json) { Value = _arrayWriter.WrittenMemory });
await command.PrepareAsync(cancellationToken);
// Execute the INSERT command
await using var command = connection.CreateCommand();
command.CommandText = _insertSql;
command.Parameters.AddWithValue("@id", NewVersion());
command.Parameters.Add(
new NpgsqlParameter("@envelope", NpgsqlDbType.Json) { Value = _arrayWriter.WrittenMemory });
await command.PrepareAsync(cancellationToken);

await command.ExecuteNonQueryAsync(cancellationToken);
await command.ExecuteNonQueryAsync(cancellationToken);

if (_originalDbContext.Database.CurrentTransaction?.GetDbTransaction() is not NpgsqlTransaction)
if (_originalDbContext.Database.CurrentTransaction?.GetDbTransaction() is not NpgsqlTransaction)
{
_signal.Set();
}
}
finally
{
_signal.Set();
await _originalDbContext.Database.CloseConnectionAsync();
}
}
finally
Expand Down
Loading
Loading