Conn res fix (#1091)

This commit is contained in:
David Puglielli 2020-02-06 07:42:20 -08:00 committed by GitHub
parent e90fd2c4a4
commit 5455b4dcb8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 50 deletions

View file

@ -22,24 +22,24 @@ function generateTables($server, $uid, $pwd, $dbName, $tableName1, $tableName2)
$stmt = $conn->query($sql);
// Insert data
$sql = "INSERT INTO $tableName1 VALUES ( ?, ? )";
$sql = "INSERT INTO $tableName1 VALUES (?, ?)";
for ($t = 100; $t < 116; $t++) {
$stmt = $conn->prepare($sql);
$ts = substr(sha1($t), 0, 5);
$params = array( $t,$ts );
$params = array($t, $ts);
$stmt->execute($params);
}
// Create table
$sql = "CREATE TABLE $tableName2 ( c1 INT, c2 VARCHAR(40) )";
$sql = "CREATE TABLE $tableName2 (c1 INT, c2 VARCHAR(40))";
$stmt = $conn->query($sql);
// Insert data
$sql = "INSERT INTO $tableName2 VALUES ( ?, ? )";
$sql = "INSERT INTO $tableName2 VALUES (?, ?)";
for ($t = 200; $t < 209; $t++) {
$stmt = $conn->prepare($sql);
$ts = substr(sha1($t), 0, 5);
$params = array( $t,$ts );
$params = array($t, $ts);
$stmt->execute($params);
}
@ -52,8 +52,12 @@ function generateTables($server, $uid, $pwd, $dbName, $tableName1, $tableName2)
// 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).
// 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)
{
sleep(1);
$stmt1 = $conn->query("SELECT @@SPID");
$obj = $stmt1->fetch(PDO::FETCH_NUM);
$spid = $obj[0];
@ -69,11 +73,11 @@ function dropTables($server, $uid, $pwd, $tableName1, $tableName2)
$conn = new PDO("sqlsrv:server = $server ; Database = $dbName ;", $uid, $pwd);
$query="IF OBJECT_ID('$tableName1', 'U') IS NOT NULL DROP TABLE $tableName1";
$stmt=$conn->query($query);
$query = "IF OBJECT_ID('$tableName1', 'U') IS NOT NULL DROP TABLE $tableName1";
$stmt = $conn->query($query);
$query="IF OBJECT_ID('$tableName2', 'U') IS NOT NULL DROP TABLE $tableName2";
$stmt=$conn->query($query);
$query = "IF OBJECT_ID('$tableName2', 'U') IS NOT NULL DROP TABLE $tableName2";
$stmt = $conn->query($query);
}
dropTables($server, $uid, $pwd, $tableName1, $tableName2);

View file

@ -12,79 +12,79 @@ $tableName2 = "test_connres2";
// 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 )
function GenerateTables($server, $uid, $pwd, $dbName, $tableName1, $tableName2)
{
$connectionInfo = array( "Database"=>$dbName, "uid"=>$uid, "pwd"=>$pwd );
$connectionInfo = array("Database"=>$dbName, "uid"=>$uid, "pwd"=>$pwd);
$conn = sqlsrv_connect( $server, $connectionInfo );
if ( $conn === false )
{
die ( print_r( sqlsrv_errors() ) );
$conn = sqlsrv_connect($server, $connectionInfo);
if ($conn === false) {
die (print_r(sqlsrv_errors()));
}
// Create table
$sql = "CREATE TABLE $tableName1 ( c1 INT, c2 VARCHAR(40) )";
$stmt = sqlsrv_query( $conn, $sql );
$sql = "CREATE TABLE $tableName1 (c1 INT, c2 VARCHAR(40))";
$stmt = sqlsrv_query($conn, $sql);
// Insert data
$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 );
$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);
}
// Create table
$sql = "CREATE TABLE $tableName2 ( c1 INT, c2 VARCHAR(40) )";
$stmt = sqlsrv_query( $conn, $sql );
$sql = "CREATE TABLE $tableName2 (c1 INT, c2 VARCHAR(40))";
$stmt = sqlsrv_query($conn, $sql);
// Insert data
$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 );
$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);
}
sqlsrv_close( $conn );
sqlsrv_close($conn);
}
// 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).
function BreakConnection( $conn, $conn_break )
// 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)
{
$stmt1 = sqlsrv_query( $conn, "SELECT @@SPID" );
if ( sqlsrv_fetch( $stmt1 ) )
{
$spid=sqlsrv_get_field( $stmt1, 0 );
sleep(1);
$stmt1 = sqlsrv_query($conn, "SELECT @@SPID");
if (sqlsrv_fetch($stmt1)) {
$spid=sqlsrv_get_field($stmt1, 0);
}
$stmt2 = sqlsrv_prepare( $conn_break, "KILL ".$spid );
sqlsrv_execute( $stmt2 );
$stmt2 = sqlsrv_prepare($conn_break, "KILL ".$spid);
sqlsrv_execute($stmt2);
sleep(1);
}
// Remove the tables generated by GenerateTables
function DropTables( $server, $uid, $pwd, $tableName1, $tableName2 )
function DropTables($server, $uid, $pwd, $tableName1, $tableName2)
{
global $dbName;
$connectionInfo = array( "Database"=>$dbName, "UID"=>$uid, "PWD"=>$pwd );
$conn = sqlsrv_connect( $server, $connectionInfo );
$connectionInfo = array("Database"=>$dbName, "UID"=>$uid, "PWD"=>$pwd);
$conn = sqlsrv_connect($server, $connectionInfo);
$query="IF OBJECT_ID('$tableName1', 'U') IS NOT NULL DROP TABLE $tableName1";
$stmt=sqlsrv_query( $conn, $query );
$query = "IF OBJECT_ID('$tableName1', 'U') IS NOT NULL DROP TABLE $tableName1";
$stmt = sqlsrv_query($conn, $query);
$query="IF OBJECT_ID('$tableName2', 'U') IS NOT NULL DROP TABLE $tableName2";
$stmt=sqlsrv_query( $conn, $query );
$query = "IF OBJECT_ID('$tableName2', 'U') IS NOT NULL DROP TABLE $tableName2";
$stmt = sqlsrv_query($conn, $query);
}
DropTables( $server, $uid, $pwd, $tableName1, $tableName2 );
GenerateTables( $server, $uid, $pwd, $dbName, $tableName1, $tableName2 );
DropTables($server, $uid, $pwd, $tableName1, $tableName2);
GenerateTables($server, $uid, $pwd, $dbName, $tableName1, $tableName2);
?>