php-sqlsrv/test/functional/sqlsrv/break.php

91 lines
2.9 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
2017-06-27 22:17:31 +02:00
$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.
2020-02-06 16:42:20 +01:00
function GenerateTables($server, $uid, $pwd, $dbName, $tableName1, $tableName2)
2017-06-02 00:11:20 +02:00
{
2020-02-06 16:42:20 +01:00
$connectionInfo = array("Database"=>$dbName, "uid"=>$uid, "pwd"=>$pwd);
2017-06-02 00:11:20 +02:00
2020-02-06 16:42:20 +01:00
$conn = sqlsrv_connect($server, $connectionInfo);
if ($conn === false) {
die (print_r(sqlsrv_errors()));
2017-06-02 00:11:20 +02:00
}
// Create table
2020-02-06 16:42:20 +01:00
$sql = "CREATE TABLE $tableName1 (c1 INT, c2 VARCHAR(40))";
$stmt = sqlsrv_query($conn, $sql);
2017-06-02 00:11:20 +02:00
// Insert data
2020-02-06 16:42:20 +01:00
$sql = "INSERT INTO $tableName1 VALUES (?, ?)";
for ($t = 100; $t < 116; $t++) {
$ts = substr(sha1($t), 0, 5);
$params = array($t, $ts);
$stmt = sqlsrv_prepare($conn, $sql, $params);
sqlsrv_execute($stmt);
2017-06-02 00:11:20 +02:00
}
// Create table
2020-02-06 16:42:20 +01:00
$sql = "CREATE TABLE $tableName2 (c1 INT, c2 VARCHAR(40))";
$stmt = sqlsrv_query($conn, $sql);
2017-06-02 00:11:20 +02:00
// Insert data
2020-02-06 16:42:20 +01:00
$sql = "INSERT INTO $tableName2 VALUES (?, ?)";
for ($t = 200; $t < 209; $t++) {
$ts = substr(sha1($t), 0, 5);
$params = array($t, $ts);
$stmt = sqlsrv_prepare($conn, $sql, $params);
sqlsrv_execute($stmt);
2017-06-02 00:11:20 +02:00
}
2020-02-06 16:42:20 +01:00
sqlsrv_close($conn);
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 = sqlsrv_query($conn, "SELECT @@SPID");
if (sqlsrv_fetch($stmt1)) {
$spid=sqlsrv_get_field($stmt1, 0);
2017-06-02 00:11:20 +02:00
}
2020-02-06 16:42:20 +01:00
$stmt2 = sqlsrv_prepare($conn_break, "KILL ".$spid);
sqlsrv_execute($stmt2);
2017-06-02 00:11:20 +02:00
sleep(1);
}
// Remove the tables generated by GenerateTables
2020-02-06 16:42:20 +01:00
function DropTables($server, $uid, $pwd, $tableName1, $tableName2)
2017-06-02 00:11:20 +02:00
{
global $dbName;
2020-02-06 16:42:20 +01:00
$connectionInfo = array("Database"=>$dbName, "UID"=>$uid, "PWD"=>$pwd);
$conn = sqlsrv_connect($server, $connectionInfo);
2017-06-02 00:11:20 +02:00
2020-02-06 16:42:20 +01:00
$query = "IF OBJECT_ID('$tableName1', 'U') IS NOT NULL DROP TABLE $tableName1";
$stmt = sqlsrv_query($conn, $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 = sqlsrv_query($conn, $query);
2017-06-02 00:11:20 +02:00
}
2020-02-06 16:42:20 +01:00
DropTables($server, $uid, $pwd, $tableName1, $tableName2);
GenerateTables($server, $uid, $pwd, $dbName, $tableName1, $tableName2);
2017-06-02 00:11:20 +02:00
?>