Skip to content

Commit 5446d60

Browse files
committed
PgSQL connection: skip commit/rollback operations when TX is already completed
This prevents cerain cases of "swollowing" original exception by the exception due to attempt to commit/rollback already completed transaction in 'finally' part
1 parent 12e2de9 commit 5446d60

1 file changed

Lines changed: 40 additions & 2 deletions

File tree

Orm/Xtensive.Orm.PostgreSql/Sql.Drivers.PostgreSql/Connection.cs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2009-2020 Xtensive LLC.
1+
// Copyright (C) 2009-2021 Xtensive LLC.
22
// This code is distributed under MIT license terms.
33
// See the License.txt file in the project root for more information.
44
// Created by: Denis Krjuchkov
@@ -9,6 +9,7 @@
99
using System.Data;
1010
using System.Data.Common;
1111
using Xtensive.Orm;
12+
using System;
1213

1314
namespace Xtensive.Sql.Drivers.PostgreSql
1415
{
@@ -45,6 +46,38 @@ public override void BeginTransaction(IsolationLevel isolationLevel)
4546
activeTransaction = underlyingConnection.BeginTransaction(SqlHelper.ReduceIsolationLevel(isolationLevel));
4647
}
4748

49+
public override void Commit()
50+
{
51+
EnsureIsNotDisposed();
52+
EnsureTransactionIsActive();
53+
54+
try {
55+
if (!IsTransactionCompleted()) {
56+
ActiveTransaction.Commit();
57+
}
58+
}
59+
finally {
60+
ActiveTransaction.Dispose();
61+
ClearActiveTransaction();
62+
}
63+
}
64+
65+
public override void Rollback()
66+
{
67+
EnsureIsNotDisposed();
68+
EnsureTransactionIsActive();
69+
70+
try {
71+
if (!IsTransactionCompleted()) {
72+
ActiveTransaction.Rollback();
73+
}
74+
}
75+
finally {
76+
ActiveTransaction.Dispose();
77+
ClearActiveTransaction();
78+
}
79+
}
80+
4881
/// <inheritdoc/>
4982
public override void MakeSavepoint(string name)
5083
{
@@ -90,9 +123,14 @@ protected override void ClearUnderlyingConnection()
90123
underlyingConnection = null;
91124
}
92125

126+
private bool IsTransactionCompleted()
127+
{
128+
return activeTransaction != null && activeTransaction.IsCompleted;
129+
}
130+
93131
// Constructors
94132

95-
[SecuritySafeCritical]
133+
[SecuritySafeCritical]
96134
public Connection(SqlDriver driver)
97135
: base(driver)
98136
{

0 commit comments

Comments
 (0)