From:             [EMAIL PROTECTED]
Operating system: Windows NT4(SP6)
PHP version:      4.2.3
PHP Bug Type:     MSSQL related
Bug description:  mssql_connect fails under stress

I have found out that mssql_connect fails under stress. I'm using PHP
4.2.3/ISAPI on IIS4/NT4(SP6). MSSQL 7.0 Server is a dedicated dual
processor server with NT4(SP6). I've installed MDAC 2.7 RTM on the web
server and performance setting is set to foreground applications (read
bug: 9852).

I've despertately searched solution to this problem, since the site I'm
running is on production state already. I experienced this bug with PHP
4.2.2 at first then moved on to 4.2.3 without any change to the problem.
The error occurs on a line:

$dbconn = mssql_connect($dbalias,$dbuser,$dbpw);

and application log entry for the error is:

The description for Event ID ( 2000 ) in Source ( c-client ) could not be
found. It contains the following insertion string(s): , PHP Warning:  MS
SQL:  Unable to connect to server:  *dbname* in *path* on line x.

I'm using TCP/IP protocol, however I've tried named pipes and
multiprotocol without help. I've tried to work around this problem and I
found out that usually this error occurs when several users are trying to
connect to the database simultaneously. I managed to reduce the appearance
of the error with the code:

function usleepWindows($usec)
{
$start = gettimeofday();

do
{
$stop = gettimeofday();
$timePassed = 1000000 * ($stop['sec'] - $start['sec'])
+ $stop['usec'] - $start['usec'];
}
while ($timePassed < $usec);
}

$tries=11;
$totaldelay=0;
// Set db connection
$dbconn = mssql_connect($dbalias,$dbuser,$dbpw);
while(!$dbconn){
        if ($tries<=0){
                echo "Database failed to respond.";
                $fp = fopen("c:\\logs\\conn_failed.log","a");
                fputs($fp, gmdate("M d Y H:i:s") . ": db connection failed. Total 
delay:
$totaldelay. From: $REMOTE_HOST.\r\n");
                fclose($fp);
                exit;
        }
        $delay=mt_rand(80000, 150000);
        usleepWindows($delay);
        $conn = mssql_connect($dbalias,$dbuser,$dbpw);
        $tries--;
        $totaldelay += $delay;
}

The function usleepWindows was found from
http://www.php.net/manual/en/function.usleep.php.

I haven't tried the CGI yet and wouldn't want to since it will surely
suffocate the web server under load. ISAPI is far more superior and my
other experiences of it are very positive.

I hope to find a solid answer to this problem as it very critical to my
site.

-- 
Edit bug report at http://bugs.php.net/?id=19541&edit=1
-- 
Try a CVS snapshot:  http://bugs.php.net/fix.php?id=19541&r=trysnapshot
Fixed in CVS:        http://bugs.php.net/fix.php?id=19541&r=fixedcvs
Fixed in release:    http://bugs.php.net/fix.php?id=19541&r=alreadyfixed
Need backtrace:      http://bugs.php.net/fix.php?id=19541&r=needtrace
Try newer version:   http://bugs.php.net/fix.php?id=19541&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=19541&r=support
Expected behavior:   http://bugs.php.net/fix.php?id=19541&r=notwrong
Not enough info:     http://bugs.php.net/fix.php?id=19541&r=notenoughinfo
Submitted twice:     http://bugs.php.net/fix.php?id=19541&r=submittedtwice
register_globals:    http://bugs.php.net/fix.php?id=19541&r=globals

Reply via email to