Skip to content

Commit 3109800

Browse files
authored
Fix GH-23962: Don't roll back a persistent PDO handle still in use (#23963)
1 parent cba864d commit 3109800

3 files changed

Lines changed: 36 additions & 1 deletion

File tree

‎NEWS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ PHP NEWS
9292
column index. (Ilia Alshanetsky)
9393
. Fixed PDOStatement::bindColumn() registering a binding for a column name
9494
that is not in the result set. (Ilia Alshanetsky)
95+
. Fixed bug GH-23962 (Destroying a persistent PDO instance rolls back a
96+
transaction still in use by another instance). (Lazizbek Ergashev)
9597

9698
- Readline:
9799
. Fixed a heap over-read in the interactive shell prompt when cli.prompt is

‎ext/pdo/pdo_dbh.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1521,7 +1521,9 @@ static void pdo_dbh_free_storage(zend_object *std)
15211521
return;
15221522
}
15231523

1524-
if (dbh->driver_data && dbh->methods && dbh->methods->rollback && pdo_is_in_transaction(dbh)) {
1524+
/* The persistent list holds one reference, other objects may hold the rest */
1525+
if (dbh->driver_data && dbh->methods && dbh->methods->rollback
1526+
&& (!dbh->is_persistent || dbh->refcount <= 2) && pdo_is_in_transaction(dbh)) {
15251527
dbh->methods->rollback(dbh);
15261528
dbh->in_txn = false;
15271529
}

‎ext/pdo_sqlite/tests/gh23962.phpt‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
GH-23962 (Destroying a persistent PDO instance rolls back a transaction still in use by another instance)
3+
--CREDITS--
4+
BoyeMagnus
5+
--EXTENSIONS--
6+
pdo_sqlite
7+
--FILE--
8+
<?php
9+
$dsn = 'sqlite::memory:';
10+
11+
$a = new PDO($dsn, null, null, [PDO::ATTR_PERSISTENT => true]);
12+
$b = new PDO($dsn, null, null, [PDO::ATTR_PERSISTENT => true]);
13+
14+
$b->beginTransaction();
15+
unset($a);
16+
var_dump($b->inTransaction());
17+
var_dump($b->commit());
18+
19+
$b->beginTransaction();
20+
$b->exec('CREATE TABLE test (a int)');
21+
unset($b);
22+
23+
$c = new PDO($dsn, null, null, [PDO::ATTR_PERSISTENT => true]);
24+
var_dump($c->inTransaction());
25+
var_dump($c->query("SELECT count(*) FROM sqlite_master WHERE name = 'test'")->fetchColumn());
26+
?>
27+
--EXPECT--
28+
bool(true)
29+
bool(true)
30+
bool(false)
31+
int(0)

0 commit comments

Comments
 (0)