xref: /PHP-8.4/ext/pdo_mysql/tests/bug79375.phpt (revision 4bb75d56)
1--TEST--
2Bug #79375: mysqli_store_result does not report error from lock wait timeout
3--EXTENSIONS--
4pdo_mysql
5--SKIPIF--
6<?php
7if (getenv("SKIP_SLOW_TESTS")) die("skip slow test");
8require_once __DIR__ . '/inc/mysql_pdo_test.inc';
9MySQLPDOTest::skip();
10?>
11--FILE--
12<?php
13require_once __DIR__ . '/inc/mysql_pdo_test.inc';
14
15function createDB(): PDO {
16    $db = MySQLPDOTest::factory();
17    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
18    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
19    return $db;
20}
21
22$db = createDB();
23$db2 = createDB();
24$db->query('CREATE TABLE test_79375 (first INT) ENGINE = InnoDB');
25$db->query('INSERT INTO test_79375 VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9)');
26
27function testNormalQuery(PDO $db, string $name) {
28    $db->exec("SET innodb_lock_wait_timeout = 1");
29    $db->exec("START TRANSACTION");
30    $query = "SELECT first FROM test_79375 WHERE first = 1 FOR UPDATE";
31    echo "Running query on $name\n";
32    try {
33        $stmt = $db->query($query);
34        echo "Got {$stmt->rowCount()} for $name\n";
35    } catch (PDOException $e) {
36        echo $e->getMessage()."\n";
37    }
38}
39
40function testPrepareExecute(PDO $db, string $name) {
41    $db->exec("SET innodb_lock_wait_timeout = 1");
42    $db->exec("START TRANSACTION");
43    $query = "SELECT first FROM test_79375 WHERE first = 1 FOR UPDATE";
44    echo "Running query on $name\n";
45    $stmt = $db->prepare($query);
46    try {
47        $stmt->execute();
48        echo "Got {$stmt->rowCount()} for $name\n";
49    } catch (PDOException $e) {
50        echo $e->getMessage()."\n";
51    }
52}
53
54function testUnbuffered(PDO $db, string $name) {
55    $db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
56    $db->exec("SET innodb_lock_wait_timeout = 1");
57    $db->exec("START TRANSACTION");
58    $query = "SELECT first FROM test_79375 WHERE first = 1 FOR UPDATE";
59    echo "Running query on $name\n";
60    $stmt = $db->prepare($query);
61    $stmt->execute();
62    try {
63        $rows = $stmt->fetchAll();
64        $count = count($rows);
65        echo "Got $count for $name\n";
66    } catch (PDOException $e) {
67        echo $e->getMessage()."\n";
68    }
69}
70
71testNormalQuery($db, 'first connection');
72testNormalQuery($db2, 'second connection');
73unset($db);
74unset($db2);
75echo "\n";
76
77$db = createDB();
78$db2 = createDB();
79testPrepareExecute($db, 'first connection');
80testPrepareExecute($db2, 'second connection');
81unset($db);
82unset($db2);
83echo "\n";
84
85$db = createDB();
86$db2 = createDB();
87testUnbuffered($db, 'first connection');
88testUnbuffered($db2, 'second connection');
89unset($db);
90unset($db2);
91echo "\n";
92
93?>
94--CLEAN--
95<?php
96require_once __DIR__ . '/inc/mysql_pdo_test.inc';
97$db = MySQLPDOTest::factory();
98$db->exec('DROP TABLE IF EXISTS test_79375');
99?>
100--EXPECT--
101Running query on first connection
102Got 1 for first connection
103Running query on second connection
104SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction
105
106Running query on first connection
107Got 1 for first connection
108Running query on second connection
109SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction
110
111Running query on first connection
112Got 1 for first connection
113Running query on second connection
114SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction
115