Try to connect and execute any statement with ConnectionPooling=1 is failed when previous connection used 'exec as user' without corresponding 'revert'.
Error message: HY000[0]: [Microsoft][ODBC Driver 11 for SQL Server]Unspecified error occurred on SQL Server. Connection may have been terminated by the server.
How to fix it? Of course, I'll use 'revert' at the end of user session, but in case of fault connection in pool will be broken for future use.
PHP 7.2, MS SQL 2005, PHP driver 7.2 ts 64bit.
Example code is attached.
<?php
defined( 'ROOT_FOLDER' ) or define( "ROOT_FOLDER", str_replace("\\","/",dirname(__FILE__)));
require_once( ROOT_FOLDER.'/Core/config.php' ); // User name and password file
$handle = sqlsrv_connect( Config::server, array( "UID"=>Config::user, "PWD"=>Config::password, "Database"=>Config::database, "ConnectionPooling" => 1, 'ReturnDatesAsStrings'=> true, 'MultipleActiveResultSets' => true,
'CharacterSet' => SQLSRV_ENC_CHAR, 'LoginTimeout' => 120, 'TraceOn' => false ) );
if ($handle === false) die( "Connect error: ".GetLastError()."<br>" );
ExecSQL( $handle, 'exec as user=\'nkolosov\';' );
//ExecSQL( $handle, 'revert;' ); // With this - error will gone...
sqlsrv_close( $handle );
echo 'first ok<br>';
$handle = sqlsrv_connect( Config::server, array( "UID"=>Config::user, "PWD"=>Config::password, "Database"=>Config::database, "ConnectionPooling" => 1, 'ReturnDatesAsStrings'=> true, 'MultipleActiveResultSets' => true,
'CharacterSet' => SQLSRV_ENC_CHAR, 'LoginTimeout' => 120, 'TraceOn' => false ) );
if ($handle === false) die( "Connect error: ".GetLastError()."<br>" );
echo 'second connect ok<br>';
ExecSQL( $handle, 'set ansi_padding on;' ); // <-- here is an error
sqlsrv_close( $handle );
function ExecSQL( $handle, $stmt ) {
$res = sqlsrv_query( $handle, $stmt );
if ($res === false) echo "ExecSQL ($stmt): ".GetLastError()."<br>";
$rows = sqlsrv_rows_affected ( $res );
sqlsrv_free_stmt( $res );
}
function GetLastError() {
if(($errors = sqlsrv_errors()) == null) return "";
$err = "";
foreach( $errors as $error) {
$mess = $error[ 'message'];
$err = $err.($err ? "::: " : "").$error[ 'SQLSTATE']."[".$error[ 'code']."]: ".$mess;
}
return "SQL error $err";
}
?>