SQL Native Client ODBC application won't disconnect after SQLDisconnect and won't merge?

Background:
I am working with a program coded in C ++ that uses ODBC for SQL Native Client to establish connections to interact with a SQL Server 2000 database.

Problem:
My connections are abstracted into an object that opens the connection when the object is created and closes the connection when the object is destroyed. I see objects being destroyed: their destructor is run and inside these destructors is called SQLDisconnect( ConnHandle )

, followed by SQLFreeHandle( SQL_HANDLE_DBC, ConnHandle );

however looking at the number of connections using sp_Who2

or performance monitor in SQL shows the number of connections increasing without relent despite destroying those connections.

This did not prove to be problematic until a chain of functions was executed that lasted long enough to create several thousand of these objects and, as such, several thousand connections.

Question:
Has anyone seen something like this before? What could be causing this? My initial Google searches weren't very helpful!

EDIT:
I have confirmed that it SQLDisconnect

returns without errors.

Connection pool disabled. In fact, when I try to enable it with SQLSetEnvAttr

, my application crashes on the second call SQLDriverConnect

.

+1


source to share


3 answers


Make sure you are not using connection pooling. If enabled, it will cache open connections for some (configurable) time.

If you are not using connection pooling then you should check the return value of SQLDisconnect (). You may have a transaction or rollback that will prevent SQL Disconnect () from releasing your connection.



You have more details on how to check for SQLDisconnect errors on MSDN .

+2


source


I believe I have seen the same problem in an application that uses MFC and ODBC and not the SQL API directly. Sometimes my application hangs on shutdown, stack trace:

sqlncli! CCriticalSectionNT :: Enter
sqlncli! SQLFreeStmt
sqlncli! SQLFreeConnect
sqlncli! SQLFreeHandle
odbc32! UnloadDriver
odbc32! FreeDbc
odbc32! DestroyIDbc
odbc32! FreeIdbc
odbc32! SQLFreeConnect
mfc42! CDatabase :: Close
mfc42! CDatabase :: Free
mfc42! CDatabase :: ~ CDatabase


Try as I could, I don't see anything that could cause such a frizz. I would be grateful if anyone can suggest a solution. It looks like others have seen similar problems on the internet, but I haven't found any solution to date.

+1


source


    sqlncli! CCriticalSectionNT :: Enter
    sqlncli! SQLFreeStmt
    sqlncli! SQLFreeConnect
    sqlncli! SQLFreeHandle
    odbc32! UnloadDriver
    odbc32! FreeDbc
    odbc32! DestroyIDbc
    odbc32! FreeIdbc
    odbc32! SQLFreeConnect
    mfc42! CDatabase :: Close
    mfc42! CDatabase :: Free
    mfc42! CDatabase :: ~ CDatabase

From your stacktrace which has no bottom, can we assume that the CDLD database is global? Perhaps in a dll?

We've detected your exact symptoms if you try to disconnect from SQL Server from a global variable destructor.

Using MDAC ODBC drivers works successfully. Moving the code out of the destructor works successfully.

It seems to have something to do with the internal sql client not wanting to be called from within DllMain.

+1


source







All Articles