Microsoft KB Archive/822841

= FIX: Setting of connection attribute fails when you use connection pooling for the SQL Server ODBC connection =

Article ID: 822841

Article Last Modified on 11/15/2007

-

APPLIES TO


 * Microsoft Data Access Components 2.7 Service Pack 1
 * Microsoft Data Access Components 2.8

-





SYMPTOMS
When you try to set a connection attribute of an ODBC connection where connection pooling is enabled by using ODBC Driver API with Microsoft SQL Server ODBC Driver, you receive the following error message:

IM006[Microsoft][ODBC Driver Manager] Driver's SQLSetConnectAttr failed

Note The ODBC Driver documentation mentions that the message is an information message. However, you receive the message as an error message.

This problem may occur when all the following conditions are true:
 * You make the connection with a data source name (DSN) by using the SQLConnect ODBC Driver API.
 * One of the default properties of the DSN (such as the Use ANSI nulls, paddings and warnings option) is disabled.
 * Connection pooling is enabled for the environment handle.
 * The Transaction Isolation Level attribute of the connection is set after you open the connection by using SQLConnect.
 * A database transaction is performed.



WORKAROUND
To work around this problem, follow these steps:
 * 1) Disable the connection pooling option for the ODBC connection.

Note Disabling connection pooling may affect the performance of your application.
 * 1) After you commit the transaction, do not turn on the auto commit option.
 * 2) Set the transaction isolation level before you open the ODBC connection.
 * 3) Use a DSN-less connection instead of obtaining the ODBC connection with a DSN.



RESOLUTION
A supported hotfix is now available from Microsoft. However, this hotfix is intended to correct only the problem that this article describes. Apply this hotfix only to systems that are experiencing this specific problem.

To resolve this problem, submit a request to Microsoft Online Customer Services to obtain the hotfix. To submit an online request to obtain the hotfix, visit the following Microsoft Web site:

http://go.microsoft.com/?linkid=6294451

Note If additional issues occur or any troubleshooting is required, you might have to create a separate service request. The usual support costs will apply to additional support questions and issues that do not qualify for this specific hotfix. To create a separate service request, visit the following Microsoft Web site:

http://support.microsoft.com/contactus/?ws=support

The English version of this fix has the file attributes (or later) that are listed in the following table. The dates and times for these files are listed in coordinated universal time (UTC). When you view the file information, it is converted to local time. To find the difference between UTC and local time, use the Time Zone tab in the Date and Time tool in Control Panel.  MDAC 2.7 SP1

Date          Time  Version    Size       File name 13-Oct-2002 19:24                     90,112  Dahotfix.exe 03-Jul-2003 04:09  2000.81.9031.51   372,736  Sqlsrv32.dll

MDAC 2.8

Date          Time  Version    Size       File name 31-Mar-2004 16:44  2000.85.1040.0     24,576  Odbcbcp.dll 31-Mar-2004 16:43  2000.85.1040.0    401,408  Sqlsrv32.dll

Note For a list of all the hotfixes available for MDAC 2.8, click the following article number to view the article in the Microsoft Knowledge Base:

839801 FIX: Hotfixes are available for MDAC 2.8



STATUS
Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the &quot;Applies to&quot; section. This problem was corrected in Microsoft Data Access Component 2.7 Service Pack 1 Refresh and Microsoft Data Access Components 2.8.



MORE INFORMATION
You see the problem that is mentioned in the &quot;Symptoms&quot; section of this article only when you have the Microsoft Data Access Component (MDAC) 2.7 Service Pack 1 installed on your computer.

Steps to reproduce the behavior
Use the following code to reproduce the problem: // ODBCTestCase.cpp : Defines the entry point for the console application. //


 * 1) include &quot;stdafx.h&quot;
 * 2) include &quot;windows.h&quot;
 * 3) include &quot;sqlext.h&quot;
 * 4) include &quot;sql.h&quot;
 * 5) include &quot;stdlib.h&quot;

void GetSQLError; long InitializeEnvironment; long Connect(BOOL lbUseDSN); long Disconnect; int ExecuteProcedure;

SQLHENV    ghEnvironment = NULL; SQLHDBC    ghConnection  = NULL; HSTMT      ghStatement   = NULL; /********************************************** * main **********************************************/ int main(int argc, char* argv[]) {   BOOL lbTransaction = TRUE; BOOL lbUseDSN     = FALSE; long lValue =0;

if(argc > 1) {       if(strcmp(argv[1], &quot;DSN&quot;) == 0) lbUseDSN = TRUE; if(argc > 2) {           if(strcmp(argv[2], &quot;TRAN&quot;) == 0) lbTransaction = TRUE; }   }    if(InitializeEnvironment == 0) {       for(long llSub = 0; llSub < 2; llSub++) {           if(Connect(lbUseDSN) == 0) {

if(lbTransaction) SQLSetConnectOption(ghConnection, SQL_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF);

//SQLGetConnectAttr(ghConnection,SQL_ATTR_AUTOCOMMIT,&lValue,0,NULL);

ExecuteProcedure;

if(lbTransaction) {                   SQLTransact(ghEnvironment, ghConnection, SQL_COMMIT); //If you do not call the following, the problem does not occur: SQLSetConnectOption(ghConnection, SQL_AUTOCOMMIT, SQL_AUTOCOMMIT_ON);

//If you call the following the problem does not occur: //SQLSetConnectOption(ghConnection, SQL_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF); //SQLGetConnectAttr(ghConnection,SQL_ATTR_AUTOCOMMIT,&lValue,0,NULL); }

Disconnect; }       }        SQLFreeHandle(SQL_HANDLE_ENV, ghEnvironment); }   return 0; }

/********************************************** * InitializeEnvironment **********************************************/ long InitializeEnvironment {   if (!SQL_SUCCEEDED(SQLSetEnvAttr(NULL,                                     SQL_ATTR_CONNECTION_POOLING,                                     (SQLPOINTER)SQL_CP_ONE_PER_DRIVER,                                     SQL_IS_INTEGER))) {       GetSQLError; return 8; }

if(!SQL_SUCCEEDED(SQLAllocHandle(SQL_HANDLE_ENV, NULL, &ghEnvironment))) {       GetSQLError; return 8; }   if(!SQL_SUCCEEDED(SQLSetEnvAttr(ghEnvironment, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC2, SQL_IS_INTEGER))) {       GetSQLError; return 8; }   if (!SQL_SUCCEEDED(SQLSetEnvAttr(ghEnvironment,                                      SQL_ATTR_CP_MATCH,                                     (SQLPOINTER) SQL_CP_STRICT_MATCH,                                      //(SQLPOINTER) SQL_CP_RELAXED_MATCH ,                                      SQL_IS_INTEGER))) {       GetSQLError; return 8; }

return 0; } /********************************************** * Connect **********************************************/ long Connect(BOOL lbUseDSN) {   SQLCHAR     lszOutConnectString[1024]; SQLSMALLINT llReturnLength; SQLAllocHandle(SQL_HANDLE_DBC, ghEnvironment, &ghConnection);

// If you set the isolation before opening the connection, no error reported. // Customer cannot set this attribute before opening connection because the object // is running under COM+, and under COM+ isolation levels automatically are set to serializable // if(!SQL_SUCCEEDED(::SQLSetConnectAttr(ghConnection, SQL_ATTR_TXN_ISOLATION, (SQLPOINTER)SQL_TXN_READ_COMMITTED, SQL_IS_INTEGER))) // { //   GetSQLError; //   return 8; // }

if(lbUseDSN) {       int iReturn = ::SQLConnect(ghConnection,                                       (SQLCHAR*)&quot;LocalCPR&quot;,                                       SQL_NTS,                                       (SQLCHAR*)&quot;sa&quot;,                                       SQL_NTS,                                       (SQLCHAR*)&quot;password1&quot;,                                       SQL_NTS); if(!SQL_SUCCEEDED(iReturn)) {           GetSQLError; return 8; }   }    else {       if(!SQL_SUCCEEDED(SQLDriverConnect(ghConnection,                                            NULL,                                           (SQLCHAR*)&quot;DSN=LocalCPR;UID=sa;PWD=password1;&quot;,                                           SQL_NTS,                                           lszOutConnectString,                                           1024,                                           &llReturnLength,                                           SQL_DRIVER_NOPROMPT))) {           GetSQLError; return 8; }   }

SQLAllocStmt(ghConnection, &ghStatement);

//If you set the isolation after you open the connection, you see the problem. if(!SQL_SUCCEEDED(::SQLSetConnectAttr(ghConnection, SQL_ATTR_TXN_ISOLATION, (SQLPOINTER)SQL_TXN_READ_COMMITTED, SQL_IS_INTEGER))) {    GetSQLError; return 8; }   return 0; } /********************************************** * Disconnect **********************************************/ long Disconnect {   if(ghStatement) {       if(!SQL_SUCCEEDED(SQLFreeHandle(SQL_HANDLE_STMT, ghStatement))) {           GetSQLError; return 8; }       ghStatement = NULL; }   if(ghConnection) {       ::SQLDisconnect(ghConnection);

if(!SQL_SUCCEEDED(SQLFreeHandle(SQL_HANDLE_DBC, ghConnection))) {           GetSQLError; return 8; }       ghConnection = NULL; }   return 0; } /********************************************** * ExecuteProcedure **********************************************/ int ExecuteProcedure {   SQLINTEGER  mlIndicator = 0; SQLRETURN  lnSqlRetCd = SQL_SUCCESS; ::SQLFreeStmt(ghStatement, SQL_CLOSE); ::SQLFreeStmt(ghStatement, SQL_UNBIND);

/*****************************************************************/   /* Execute Procedure /*****************************************************************/   RETCODE llDbRetCd = SQLExecDirect(ghStatement, (SQLCHAR*)&quot;SELECT * From Table1&quot;, SQL_NTS); if((llDbRetCd != SQL_SUCCESS) && (llDbRetCd != SQL_SUCCESS_WITH_INFO)) {       GetSQLError; return 8; }

/*****************************************************************/   /* Bind return Value /*****************************************************************/   char lszReturnBuf[300]; SDWORD lSts;

llDbRetCd = SQLBindCol(ghStatement, 1, SQL_C_TCHAR, &lszReturnBuf, 300, &lSts);

if ((llDbRetCd != SQL_SUCCESS) && (llDbRetCd != SQL_SUCCESS_WITH_INFO)) {       GetSQLError; return 8; }

/*****************************************************************/   /* Fetch Result /*****************************************************************/   llDbRetCd = SQLFetch(ghStatement); if ((llDbRetCd != SQL_SUCCESS) && (llDbRetCd != SQL_SUCCESS_WITH_INFO)) {       GetSQLError; return 8; }

printf(&quot;Output Value : %s\n&quot;,lszReturnBuf); return 0; }

/********************************************** * GetSQLError **********************************************/ void GetSQLError { long   llDbErrCd = 0; short  llRetMsgLen = 0; char   lszSqlErrMsg[255]; char   lszSqlMsg[255];

SQLError(ghEnvironment,           ghConnection,            ghStatement,            (SQLCHAR*) lszSqlErrMsg,            &llDbErrCd,            (SQLCHAR*) lszSqlMsg,            255,            &llRetMsgLen); printf(lszSqlErrMsg); printf(lszSqlMsg); }

Keywords: kbhotfixserver kbqfe kberrmsg kbdriver kbtransaction kbfix kbbug KB822841

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.