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