ID:               44300
 Comment by:       frosty dot z at freesbee dot fr
 Reported By:      alfa77 at gmail dot com
 Status:           Open
 Bug Type:         MSSQL related
 Operating System: Windows 2000
 PHP Version:      5.2.5
 New Comment:

Hi, same problem detected here (connection "rarely" successful with
mssql_connect, with a MSSQL server under quite heavy load).

Happens only with PHP on Windows, not on Linux (FreeDTS).

But for some reason I needed to connect from PHP/Windows, so I have
used the "ADO workaround", as previously suggested by alfa77.

At first, I didn't understand very well that workaround, so here are
some details :

Do not use the ADOdb engine 'mssql' because it will still use
mssql_connect(). Instead, use 'ado_mssql' which uses COM objects ; that
makes all the difference.

Here is a basic database functions lib :

function db_open($db_host, $db_login, $db_pass, $db_name)
{
  $db = NewADOConnection('ado_mssql');
                
  $dsn="PROVIDER=MSDASQL;DRIVER={SQL Server};"
  .
"SERVER=".$db_host.";DATABASE=".$db_name.";UID=".$db_login.";PWD=".$db_pass.";";
                
  $db->Connect($dsn);
                
  return $db;
}

function db_query($db, $query)
{
  return $db->Execute($query);
}
   
function db_fetch_assoc($res)
{
  $obj = $res->FetchNextObj();
  return get_object_vars($obj);
}
   
function db_close($db)
{
  $db->Close();
}


Previous Comments:
------------------------------------------------------------------------

[2008-09-26 11:53:35] yusefhassan at gmail dot com

Have you try editing php.ini mssql.max_procs?
mssql.max_procs = -1

------------------------------------------------------------------------

[2008-03-26 08:31:18] william at nettsite dot co dot za

I am running three Windows XP SP2 boxes as developer workstations, all
PHP 5.2.5, installed into c:\php to avoid the space in "Program Files",
and the MS Sql connection works perfectly on one of the machines, but
not the other two. All three boxes are on the same LAN, can connect to
the database using MS SQL Studio, and all have IIS 5.1. I have copied
the PHP installation from the working PC to the others, no difference.
This is whether the connection is to a remote or local SQL server.

------------------------------------------------------------------------

[2008-03-10 05:41:10] alfa77 at gmail dot com

I think that this problem occurs due to usage of old DB-Lib for
connecting to MSSQL. There are another bug – that you can’t fetch
varchar more than 256-character length (you have to convert it to
TEXT).
It’s a pity, but the best way to work with MSSQL now is using ADO (no
connection/long varchar/Unicode problems).

------------------------------------------------------------------------

[2008-03-08 07:38:07] alfa77 at gmail dot com

Well, I found two solutions.
1) I use ADO to connect to MSSQL. ADO is more slower (up to 2 times!),
than mssql_* functions, but there are no connection problems
2) I set connection timeout in code:
function getmt()
{
   list($usec, $sec)=explode(" ",microtime());
   return ((float)$usec+(float)$sec);
}
$time_st=getmt();
$db=new CSql;
while(!$db->connect_db($Host, $Name, $Login, $Passw))
   if(round(getmt()-$time_st, 0)>60) break;
if(!$db->dbc) die("Connection failed!"); 

It is too hard to use odbc_* functions, because there are a lot of bugs
with TEXT field type – I must put it on the last position in query and
use CONVERT(varbinary, other way I get ODBC errors.

I hope that mssql_* and odbc_* problems will be solved in future PHP
releases.

------------------------------------------------------------------------

[2008-03-01 03:30:06] alfa77 at gmail dot com

Description:
------------
Hello!
The number of my site users has recently increased and I have faced the
following bug: 
mssql_connect function sometimes fails to connect to MSSQL. It works
fine for few hours, 
then suddenly fails, and the work is resumed in a few minutes.

Here is the part of my DB class (CSql)
function connect_db($server, $user, $password)
{
        $this->dbc=mssql_connect($server, $user, $password);
        if(!$this->dbc) return false;
        return true;
}

The warnings in Apache log are
[Thu Feb 21 20:50:58 2008] [error] [client 82.200.***.***] PHP Warning:
mssql_connect() 
[<a href='function.mssql-connect'>function.mssql-connect</a>]: Unable
to connect to server: 
****** in ******.php on line 22

When I reload the page on site, connection sometimes succeeds,
sometimes – fails.
Web-server configuration: Windows 2000 Server (SP4) + Apache 2 + PHP
5.2.5. Peak usage 
of CPUs is no more than 70%, RAM usage – no more than 50%. There is a
program on this server 
that works with MSSQL through ADO, and it never has connection
problems. There are no 
connection problems from LAN too.
SQL Server 2000 (SP4) is working on dedicated server (Windows 2000
Server (SP4)) 
which is connected to the web server by gigabit LAN. I have tested the
network adapters, 
they work fine. CPUs usage is no more than 60%, RAM usage – no more
than 70%.

I have already:
1) replaced ntwdblib.dll in PHP directory with a new version. Also, I
have installed MDAC 2.81
2) moved SQL Server to a  new, fast server.
3) tried to use mssql_connect or  mssql_pconnect.
4) tried to use server name or server IP and port as the first
parameter for mssql_connect
5) tried to connect through TCP/IP or Named Pipes.
6) When I cycled connection attempts, it succeeded in connection almost
every time (usually 
from the second or third attempt)
$db=new CSql;
$db_failed=1;
while(!$db->connect_db(DBHost, DBLogin, DBPassw))
{ 
       if($db_failed>=5) break;
        sleep(2);
        $db_failed++;
}
if(!$db->dbc) die("DB connection failed!");
7) I checked the network connection by using sockets or ODBC when
mssql_connect had failed. It worked fine!
if(!fsockopen("192.168.0.3", 1433, $errno, $errstr, 10))
WrLog("C:\\sqlerr.txt","Err1 (($errstr ($errno)))");
if(!odbc_connect("sqlsrv",DBLogin, DBPassw))
WrLog("C:\\sqlerr.txt","Err2");
8) There are no connection limits in SQL Server or PHP.ini
[MSSQL]
mssql.max_persistent = -1
mssql.max_links = -1

I examined Apache logs few months ago, when MSSQL worked on the same
server with Apache. 
There were no messages about connection problems. 



------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=44300&edit=1

Reply via email to