From 5455b4dcb8a621fada62289de122db6091da028e Mon Sep 17 00:00:00 2001 From: David Puglielli Date: Thu, 6 Feb 2020 07:42:20 -0800 Subject: [PATCH] Conn res fix (#1091) --- test/functional/pdo_sqlsrv/break_pdo.php | 22 ++++--- test/functional/sqlsrv/break.php | 82 ++++++++++++------------ 2 files changed, 54 insertions(+), 50 deletions(-) diff --git a/test/functional/pdo_sqlsrv/break_pdo.php b/test/functional/pdo_sqlsrv/break_pdo.php index c5287a33..0bddb36b 100644 --- a/test/functional/pdo_sqlsrv/break_pdo.php +++ b/test/functional/pdo_sqlsrv/break_pdo.php @@ -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); diff --git a/test/functional/sqlsrv/break.php b/test/functional/sqlsrv/break.php index 97abcccf..b7bdc43b 100644 --- a/test/functional/sqlsrv/break.php +++ b/test/functional/sqlsrv/break.php @@ -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); ?>