1--TEST--
2MySQL PDO->__construct(), PDO::ATTR_PERSISTENT
3--SKIPIF--
4<?php
5require_once(__DIR__ . DIRECTORY_SEPARATOR . 'skipif.inc');
6require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
7MySQLPDOTest::skip();
8?>
9--FILE--
10<?php
11    require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
12
13    try {
14
15        $dsn = MySQLPDOTest::getDSN();
16        $user = PDO_MYSQL_TEST_USER;
17        $pass = PDO_MYSQL_TEST_PASS;
18
19        $db1 = new PDO($dsn, $user, $pass, array(PDO::ATTR_PERSISTENT => true));
20        $db2 = new PDO($dsn, $user, $pass, array(PDO::ATTR_PERSISTENT => true));
21        $db1->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
22        $db2->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
23        $db1->exec('SET @pdo_persistent_connection=1');
24        $stmt = $db2->query('SELECT @pdo_persistent_connection as _pers');
25        $tmp = $stmt->fetch(PDO::FETCH_ASSOC);
26        if ($tmp['_pers'] !== '1')
27            printf("[001] Both handles should use the same connection.");
28
29        $stmt = $db1->query('SELECT CONNECTION_ID() as _con1');
30        $tmp = $stmt->fetch(PDO::FETCH_ASSOC);
31        $con1 = $tmp['_con1'];
32
33        $stmt = $db2->query('SELECT CONNECTION_ID() as _con2');
34        $tmp = $stmt->fetch(PDO::FETCH_ASSOC);
35        $con2 = $tmp['_con2'];
36
37        if ($con1 !== $con2)
38            printf("[002] Both handles should report the same MySQL thread ID");
39
40        $db1 = NULL; /* should be equal to closing to my understanding */
41        $db1 = new PDO($dsn, $user, $pass, array(PDO::ATTR_PERSISTENT => true));
42        $db1->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
43        $stmt = $db1->query('SELECT CONNECTION_ID() as _con1');
44        $tmp = $stmt->fetch(PDO::FETCH_ASSOC);
45        $con1 = $tmp['_con1'];
46
47        if ($con1 !== $con2)
48            printf("[003] Both handles should report the same MySQL thread ID");
49
50        $affected = $db1->exec(sprintf('KILL %d', $con1));
51        // Server needs some think-time sometimes
52        sleep(1);
53        if ('00000' == $db1->errorCode()) {
54            // looks like KILL has worked ? Or not... TODO: why no warning with libmysql?!
55            @$db1->exec("SET @pdo_persistent_connection=2");
56            // but now I want to see some error...
57            if ('HY000' != $db1->errorCode())
58                printf("[004] Wrong error code %s\n", $db1->errorCode());
59
60            $tmp = implode(' ', $db1->errorInfo());
61            if (!strstr($tmp, '2006'))
62                printf("[005] Wrong error info %s\n", $tmp);
63        }
64
65        $db1 = new PDO($dsn, $user, $pass, array(PDO::ATTR_PERSISTENT => false));
66        $db1->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
67        $stmt = $db1->query('SELECT CONNECTION_ID() as _con1');
68        $tmp = $stmt->fetch(PDO::FETCH_ASSOC);
69        $con1 = $tmp['_con1'];
70
71        @$db2 = new PDO($dsn, $user, $pass, array(PDO::ATTR_PERSISTENT => true));
72        $db2->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
73        $stmt = $db2->query('SELECT CONNECTION_ID() as _con2');
74        $tmp = $stmt->fetch(PDO::FETCH_ASSOC);
75        $con2 = $tmp['_con2'];
76
77        if ($con1 == $con2)
78            printf("[006] Looks like the persistent and the non persistent connection are using the same link?!\n");
79
80        // lets go crazy and create a few pconnections...
81        $connections = array();
82        for ($i = 0; $i <= 20; $i++) {
83            $connections[$i] = new PDO($dsn, $user, $pass, array(PDO::ATTR_PERSISTENT => true));
84        }
85        do {
86            $i = mt_rand(0, 20);
87            if (isset($connections[$i]))
88                unset($connections[$i]);
89        } while (!empty($connections));
90
91
92    } catch (PDOException $e) {
93        printf("[001] %s, [%s] %s [%s] %s\n",
94            $e->getMessage(),
95            (is_object($db1)) ? $db1->errorCode() : 'n/a',
96            (is_object($db1)) ? implode(' ', $db1->errorInfo()) : 'n/a',
97            (is_object($db2)) ? $db2->errorCode() : 'n/a',
98            (is_object($db2)) ? implode(' ', $db2->errorInfo()) : 'n/a');
99    }
100
101    print "done!";
102?>
103--EXPECT--
104done!
105