Skip to content

Commit 90ff86d

Browse files
committed
Do not return connections to the pool if an error occurs in ROLLBACK
1 parent 01efa4c commit 90ff86d

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

src/conn/mod.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,8 +1230,13 @@ impl Conn {
12301230
/// Requires that `self.inner.tx_status != TxStatus::None`
12311231
pub(crate) async fn rollback_transaction(&mut self) -> Result<()> {
12321232
debug_assert_ne!(self.inner.tx_status, TxStatus::None);
1233-
self.inner.tx_status = TxStatus::None;
1234-
self.query_drop("ROLLBACK").await
1233+
let tx_status = mem::replace(&mut self.inner.tx_status, TxStatus::None);
1234+
if let Err(e) = self.query_drop("ROLLBACK").await {
1235+
// Reset tx_status if ROLLBACK fails so that the connection is considered dirty
1236+
self.inner.tx_status = tx_status;
1237+
return Err(e);
1238+
}
1239+
Ok(())
12351240
}
12361241

12371242
/// Returns `true` if `SERVER_MORE_RESULTS_EXISTS` flag is contained
@@ -1282,23 +1287,22 @@ impl Conn {
12821287
/// The purpose of this function, is to cleanup the connection while returning it to a [`Pool`].
12831288
async fn cleanup_for_pool(mut self) -> Result<Self> {
12841289
loop {
1285-
let result = if self.has_pending_result() {
1286-
self.drop_result().await
1290+
if self.has_pending_result() {
1291+
// The connection was dropped and we assume that it was dropped intentionally,
1292+
// so we'll ignore non-fatal errors during cleanup (also there is no direct caller
1293+
// to return this error to).
1294+
if let Err(err) = self.drop_result().await {
1295+
if err.is_fatal() {
1296+
// This means that connection is completely broken
1297+
// and shouldn't return to a pool.
1298+
return Err(err);
1299+
}
1300+
}
12871301
} else if self.inner.tx_status != TxStatus::None {
1288-
self.rollback_transaction().await
1302+
// If an error occurs during rollback, don't reuse the connection.
1303+
self.rollback_transaction().await?;
12891304
} else {
12901305
break;
1291-
};
1292-
1293-
// The connection was dropped and we assume that it was dropped intentionally,
1294-
// so we'll ignore non-fatal errors during cleanup (also there is no direct caller
1295-
// to return this error to).
1296-
if let Err(err) = result {
1297-
if err.is_fatal() {
1298-
// This means that connection is completely broken
1299-
// and shouldn't return to a pool.
1300-
return Err(err);
1301-
}
13021306
}
13031307
}
13041308
Ok(self)

0 commit comments

Comments
 (0)