php-sqlsrv/test/functional/pdo_sqlsrv/break_pdo.php

85 lines
2.8 KiB
PHP
Raw Normal View History

2017-06-02 00:11:20 +02:00
<?php
require_once("MsSetup.inc");
2017-06-27 22:17:31 +02:00
// Using the test database for two tables specifically constructed
2017-06-02 00:11:20 +02:00
// for the connection resiliency tests
$dbName = $databaseName;
2017-06-02 00:11:20 +02:00
$tableName1 = "test_connres1";
$tableName2 = "test_connres2";
// Generate tables for use with the connection resiliency tests.
// Using generated tables will eventually allow us to put the
// connection resiliency tests on Github, since the integrated testing
// from AppVeyor does not have AdventureWorks.
function generateTables($server, $uid, $pwd, $dbName, $tableName1, $tableName2)
2017-06-02 00:11:20 +02:00
{
2017-11-03 22:35:16 +01:00
try {
$conn = new PDO("sqlsrv:server = $server ; Database = $dbName ;", $uid, $pwd);
// Create table
$sql = "CREATE TABLE $tableName1 (c1 INT, c2 VARCHAR(40))";
$stmt = $conn->query($sql);
// Insert data
2020-02-06 16:42:20 +01:00
$sql = "INSERT INTO $tableName1 VALUES (?, ?)";
2017-11-03 22:35:16 +01:00
for ($t = 100; $t < 116; $t++) {
$stmt = $conn->prepare($sql);
$ts = substr(sha1($t), 0, 5);
2020-02-06 16:42:20 +01:00
$params = array($t, $ts);
2017-11-03 22:35:16 +01:00
$stmt->execute($params);
}
// Create table
2020-02-06 16:42:20 +01:00
$sql = "CREATE TABLE $tableName2 (c1 INT, c2 VARCHAR(40))";
2017-11-03 22:35:16 +01:00
$stmt = $conn->query($sql);
// Insert data
2020-02-06 16:42:20 +01:00
$sql = "INSERT INTO $tableName2 VALUES (?, ?)";
2017-11-03 22:35:16 +01:00
for ($t = 200; $t < 209; $t++) {
$stmt = $conn->prepare($sql);
$ts = substr(sha1($t), 0, 5);
2020-02-06 16:42:20 +01:00
$params = array($t, $ts);
2017-11-03 22:35:16 +01:00
$stmt->execute($params);
}
unset($conn);
} catch (PDOException $e) {
var_dump($e->errorInfo);
2017-06-02 00:11:20 +02:00
}
}
// Break connection by getting the session ID and killing it.
// Note that breaking a connection and testing reconnection requires a
// TCP/IP protocol connection (as opposed to a Shared Memory protocol).
2020-02-06 16:42:20 +01:00
// Wait one second before and after breaking to ensure the break occurs
// in the correct order, otherwise there may be timing issues in Linux
// that can cause tests to fail intermittently and unpredictably.
function breakConnection($conn, $conn_break)
2017-06-02 00:11:20 +02:00
{
2020-02-06 16:42:20 +01:00
sleep(1);
$stmt1 = $conn->query("SELECT @@SPID");
$obj = $stmt1->fetch(PDO::FETCH_NUM);
2017-06-02 00:11:20 +02:00
$spid = $obj[0];
$stmt2 = $conn_break->query("KILL ".$spid);
2017-06-02 00:11:20 +02:00
sleep(1);
}
// Remove any databases previously created by GenerateDatabase
function dropTables($server, $uid, $pwd, $tableName1, $tableName2)
2017-06-02 00:11:20 +02:00
{
global $dbName;
$conn = new PDO("sqlsrv:server = $server ; Database = $dbName ;", $uid, $pwd);
2020-02-06 16:42:20 +01:00
$query = "IF OBJECT_ID('$tableName1', 'U') IS NOT NULL DROP TABLE $tableName1";
$stmt = $conn->query($query);
2017-06-02 00:11:20 +02:00
2020-02-06 16:42:20 +01:00
$query = "IF OBJECT_ID('$tableName2', 'U') IS NOT NULL DROP TABLE $tableName2";
$stmt = $conn->query($query);
2017-06-02 00:11:20 +02:00
}
dropTables($server, $uid, $pwd, $tableName1, $tableName2);
generateTables($server, $uid, $pwd, $dbName, $tableName1, $tableName2);