summaryrefslogtreecommitdiff
path: root/ext/pdo_mysql/tests/pdo_mysql_pconnect.phpt
blob: d59c93032846bf24018a5c5ff4b010d1492bbb75 (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
--TEST--
MySQL PDO->__construct(), PDO::ATTR_PERSISTENT
--SKIPIF--
<?php
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'skipif.inc');
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
MySQLPDOTest::skip();
?>
--FILE--
<?php
	require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');

	try {

		$dsn = MySQLPDOTest::getDSN();
		$user = PDO_MYSQL_TEST_USER;
		$pass = PDO_MYSQL_TEST_PASS;

		$db1 = new PDO($dsn, $user, $pass, array(PDO::ATTR_PERSISTENT => true));
		$db2 = new PDO($dsn, $user, $pass, array(PDO::ATTR_PERSISTENT => true));
		$db1->exec('SET @pdo_persistent_connection=1');
		$stmt = $db2->query('SELECT @pdo_persistent_connection as _pers');
		$tmp = $stmt->fetch(PDO::FETCH_ASSOC);
		if ($tmp['_pers'] !== '1')
			printf("[001] Both handles should use the same connection.");

		$stmt = $db1->query('SELECT CONNECTION_ID() as _con1');
		$tmp = $stmt->fetch(PDO::FETCH_ASSOC);
		$con1 = $tmp['_con1'];

		$stmt = $db2->query('SELECT CONNECTION_ID() as _con2');
		$tmp = $stmt->fetch(PDO::FETCH_ASSOC);
		$con2 = $tmp['_con2'];

		if ($con1 !== $con2)
			printf("[002] Both handles should report the same MySQL thread ID");

		$db1 = NULL; /* should be equal to closing to my understanding */
		$db1 = new PDO($dsn, $user, $pass, array(PDO::ATTR_PERSISTENT => true));
		$stmt = $db1->query('SELECT CONNECTION_ID() as _con1');
		$tmp = $stmt->fetch(PDO::FETCH_ASSOC);
		$con1 = $tmp['_con1'];

		if ($con1 !== $con2)
			printf("[003] Both handles should report the same MySQL thread ID");

		$affected = $db1->exec(sprintf('KILL %d', $con1));
		// Server needs some think-time sometimes
		sleep(1);
		if ('00000' == $db1->errorCode()) {
			// looks like KILL has worked ? Or not... TODO: why no warning with libmysql?!
			@$db1->exec("SET @pdo_persistent_connection=2");
			// but now I want to see some error...
			if ('HY000' != $db1->errorCode())
				printf("[004] Wrong error code %s\n", $db1->errorCode());

			$tmp = implode(' ', $db1->errorInfo());
			if (!strstr($tmp, '2006'))
				printf("[005] Wrong error info %s\n", $tmp);
		}

		$db1 = new PDO($dsn, $user, $pass, array(PDO::ATTR_PERSISTENT => false));
		$stmt = $db1->query('SELECT CONNECTION_ID() as _con1');
		$tmp = $stmt->fetch(PDO::FETCH_ASSOC);
		$con1 = $tmp['_con1'];

		@$db2 = new PDO($dsn, $user, $pass, array(PDO::ATTR_PERSISTENT => true));
		$stmt = $db2->query('SELECT CONNECTION_ID() as _con2');
		$tmp = $stmt->fetch(PDO::FETCH_ASSOC);
		$con2 = $tmp['_con2'];

		if ($con1 == $con2)
			printf("[006] Looks like the persistent and the non persistent connection are using the same link?!\n");

		// lets go crazy and create a few pconnections...
		$connections = array();
		for ($i = 0; $i <= 20; $i++) {
			$connections[$i] = new PDO($dsn, $user, $pass, array(PDO::ATTR_PERSISTENT => true));
		}
		do {
			$i = mt_rand(0, 20);
			if (isset($connections[$i]))
				unset($connections[$i]);
		} while (!empty($connections));


	} catch (PDOException $e) {
		printf("[001] %s, [%s] %s [%s] %s\n",
			$e->getMessage(),
			(is_object($db1)) ? $db1->errorCode() : 'n/a',
			(is_object($db1)) ? implode(' ', $db1->errorInfo()) : 'n/a',
			(is_object($db2)) ? $db2->errorCode() : 'n/a',
			(is_object($db2)) ? implode(' ', $db2->errorInfo()) : 'n/a');
	}

	print "done!";
?>
--EXPECTF--
done!