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