Skip to content

Commit b7bf846

Browse files
authored
Fix bug #81227: PDO::inTransaction reports false when in transaction (#14268)
1 parent 9aa3a0d commit b7bf846

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ PHP NEWS
179179

180180
- PDO_SQLITE:
181181
. Added class PdoSqlite. (danack, kocsismate)
182+
. Fixed bug #81227 (PDO::inTransaction reports false when in transaction).
183+
(nielsdos)
182184

183185
- PGSQL:
184186
. Added the possibility to have no conditions for pg_select. (OmarEmaraDev)

ext/pdo_sqlite/sqlite_driver.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,15 @@ static int pdo_sqlite_get_attribute(pdo_dbh_t *dbh, zend_long attr, zval *return
282282
return 1;
283283
}
284284

285+
static bool pdo_sqlite_in_transaction(pdo_dbh_t *dbh)
286+
{
287+
pdo_sqlite_db_handle* H = (pdo_sqlite_db_handle*) dbh->driver_data;
288+
/* It's not possible in sqlite3 to explicitly turn autocommit off other
289+
* than manually starting a transaction. Manual transactions always are
290+
* the mode of operation when autocommit is off. */
291+
return H->db && sqlite3_get_autocommit(H->db) == 0;
292+
}
293+
285294
static bool pdo_sqlite_set_attr(pdo_dbh_t *dbh, zend_long attr, zval *val)
286295
{
287296
pdo_sqlite_db_handle *H = (pdo_sqlite_db_handle *)dbh->driver_data;
@@ -733,7 +742,7 @@ static const struct pdo_dbh_methods sqlite_methods = {
733742
NULL, /* check_liveness: not needed */
734743
get_driver_methods,
735744
pdo_sqlite_request_shutdown,
736-
NULL, /* in transaction, use PDO's internal tracking mechanism */
745+
pdo_sqlite_in_transaction,
737746
pdo_sqlite_get_gc
738747
};
739748

ext/pdo_sqlite/tests/bug81227.phpt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
Bug #81227 (PDO::inTransaction reports false when in transaction)
3+
--EXTENSIONS--
4+
pdo_sqlite
5+
--FILE--
6+
<?php
7+
$db = new PDO("sqlite::memory:");
8+
var_dump($db->inTransaction());
9+
10+
$db->exec("BEGIN EXCLUSIVE TRANSACTION");
11+
var_dump($db->inTransaction());
12+
13+
try {
14+
$db->beginTransaction();
15+
} catch (PDOException $e) {
16+
echo $e->getMessage(), "\n";
17+
}
18+
19+
$db->commit();
20+
var_dump($db->inTransaction());
21+
22+
$db->beginTransaction();
23+
var_dump($db->inTransaction());
24+
?>
25+
--EXPECT--
26+
bool(false)
27+
bool(true)
28+
There is already an active transaction
29+
bool(false)
30+
bool(true)

0 commit comments

Comments
 (0)