Skip to content
Permalink
Browse files

Fix prepare-related exception handling behavior

Fixes #2665
Fixes #2820
  • Loading branch information
roji committed Jan 24, 2020
1 parent 7d4ca8f commit afe2ac769f0a4d9b6024a6ec181ac5bbeeab29f0
Showing with 242 additions and 154 deletions.
  1. +20 −3 src/Npgsql/NpgsqlCommand.cs
  2. +175 −151 src/Npgsql/NpgsqlDataReader.cs
  3. +30 −0 test/Npgsql.Tests/AutoPrepareTests.cs
  4. +17 −0 test/Npgsql.Tests/PrepareTests.cs
@@ -585,11 +585,15 @@ async Task PrepareLong()

// Loop over statements, skipping those that are already prepared (because they were persisted)
var isFirst = true;
foreach (var statement in _statements)
for (var i = 0; i < _statements.Count; i++)
{
if (statement.PreparedStatement?.State == PreparedState.BeingPrepared)
var statement = _statements[i];
var pStatement = statement.PreparedStatement;
if (pStatement?.State != PreparedState.BeingPrepared)
continue;

try
{
var pStatement = statement.PreparedStatement;
if (pStatement.StatementBeingReplaced != null)
{
Expect<CloseCompletedMessage>(await connector.ReadMessage(async), connector);
@@ -619,6 +623,19 @@ async Task PrepareLong()
pStatement.CompletePrepare();
isFirst = false;
}
catch
{
// The statement wasn't prepared successfully, update the bookkeeping for it and
// all following statements
for (; i < _statements.Count; i++)
{
pStatement = _statements[i].PreparedStatement;
if (pStatement?.State == PreparedState.BeingPrepared)
pStatement.CompleteUnprepare();
}

throw;
}
}

Expect<ReadyForQueryMessage>(await connector.ReadMessage(async), connector);

0 comments on commit afe2ac7

Please sign in to comment.
You can’t perform that action at this time.