summaryrefslogtreecommitdiff
path: root/ext/pdo_mysql/tests/bug79375.phpt
blob: c7905a7018809eb6543b9f05eba276466382c3f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
--TEST--
Bug #79375: mysqli_store_result does not report error from lock wait timeout
--SKIPIF--
<?php
if (!extension_loaded('pdo') || !extension_loaded('pdo_mysql')) die('skip not loaded');
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'skipif.inc');
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
MySQLPDOTest::skip();
?>
--FILE--
<?php
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');

function createDB(): PDO {
    $db = MySQLPDOTest::factory();
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    return $db;
}

$db = createDB();
$db2 = createDB();
$db->query('DROP TABLE IF EXISTS test');
$db->query('CREATE TABLE test (first int) ENGINE = InnoDB');
$db->query('INSERT INTO test VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9)');

function testNormalQuery(PDO $db, string $name) {
    $db->exec("SET innodb_lock_wait_timeout = 1");
    $db->exec("START TRANSACTION");
    $query = "SELECT first FROM test WHERE first = 1 FOR UPDATE";
    echo "Running query on $name\n";
    try {
        $stmt = $db->query($query);
        echo "Got {$stmt->rowCount()} for $name\n";
    } catch (PDOException $e) {
        echo $e->getMessage()."\n";
    }
}

function testPrepareExecute(PDO $db, string $name) {
    $db->exec("SET innodb_lock_wait_timeout = 1");
    $db->exec("START TRANSACTION");
    $query = "SELECT first FROM test WHERE first = 1 FOR UPDATE";
    echo "Running query on $name\n";
    $stmt = $db->prepare($query);
    try {
        $stmt->execute();
        echo "Got {$stmt->rowCount()} for $name\n";
    } catch (PDOException $e) {
        echo $e->getMessage()."\n";
    }
}

function testUnbuffered(PDO $db, string $name) {
    $db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
    $db->exec("SET innodb_lock_wait_timeout = 1");
    $db->exec("START TRANSACTION");
    $query = "SELECT first FROM test WHERE first = 1 FOR UPDATE";
    echo "Running query on $name\n";
    $stmt = $db->prepare($query);
    $stmt->execute();
    try {
        $rows = $stmt->fetchAll();
        $count = count($rows);
        echo "Got $count for $name\n";
    } catch (PDOException $e) {
        echo $e->getMessage()."\n";
    }
}

testNormalQuery($db, 'first connection');
testNormalQuery($db2, 'second connection');
unset($db);
unset($db2);
echo "\n";

$db = createDB();
$db2 = createDB();
testPrepareExecute($db, 'first connection');
testPrepareExecute($db2, 'second connection');
unset($db);
unset($db2);
echo "\n";

$db = createDB();
$db2 = createDB();
testUnbuffered($db, 'first connection');
testUnbuffered($db2, 'second connection');
unset($db);
unset($db2);
echo "\n";

?>
--CLEAN--
<?php
require __DIR__ . '/mysql_pdo_test.inc';
MySQLPDOTest::dropTestTable();
?>
--EXPECT--
Running query on first connection
Got 1 for first connection
Running query on second connection
SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction

Running query on first connection
Got 1 for first connection
Running query on second connection
SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction

Running query on first connection
Got 1 for first connection
Running query on second connection
SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction