Excessive socket usage using MS-SQL server via ADO COMobject

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View


I've an database import script written in PHP which used ADO via COM.
It makes thousands of queries to a MS-SQL server.  The problem is that
the process runs out of sockets.
The MSSQL server is used in the following way:

    // In the beginning of the script, the connection is set up
    $dbc=new COM("ADODB.Connection");
Library=DBMSSOCN;Data Source=FOO;Database=BAR;User ID=FOO;Password=BAR";
    // Typical query
    $rs=$dbc->execute("SELECT ... FROM ...");
    // process the result set here

    // Close and free the resultset object:
The script executes many queries in rapid succession.  When I inspect
the output of 'netstat -an' during execution, I see thousands of open


Sometimes the script even dies with a COM exception error, that it can't
make a connection to the MSSQL server anymore.  This happens when +/-
4000 sockets are in the TIME_WAIT state.  After two minutes these
sockets are automatically closed by Windows and MSSQL connections are
possible again.

Is this a known problem?  I couldn't find it in Google.

is there a way to make ADO (or PHP) reuse the existing socket, instead
of opening a new one for each Execute?


Re: Excessive socket usage using MS-SQL server via ADO COMobject

I am not sure how helpful this will be (probably not very) but if you
are using the ADOdb Library for PHP try $dbc->PConnect() instead of
$dbc->Open(). This should open a persistent connection instead of a new
connection each time.

Re: Excessive socket usage using MS-SQL server via ADO COMobject

Quoted text here. Click to load it

I am not using the ADOdb library, but thanks for the tip anyway: I will
look into the source code of ADOdb to see how PConnect() is implemented.
Maybe that will give some insight.

What do they mean by 'persistent connection'?  That the connection is
kept alive between script invocations?  Or that multiple queries done
from the same script invocation reuse the existing MSSQL socket?


Site Timeline