FIX: Setting of connection attribute fails when you use connection pooling for the SQL Server ODBC connection (822841)
The information in this article applies to:
- Microsoft Data Access Components 2.7 SP1
- Microsoft Data Access Components 2.8
SYMPTOMSWhen 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.
WORKAROUNDTo work around this problem, follow these steps:
- Disable the connection pooling option for the ODBC
connection.
Note Disabling connection pooling may affect the performance of your
application. - After you commit the transaction, do not turn on the auto
commit option.
- Set the transaction isolation level before you open the
ODBC connection.
- Use a DSN-less connection instead of obtaining the ODBC
connection with a DSN.
RESOLUTIONA supported hotfix is now available from Microsoft, but it is only intended to correct the problem that this article describes. Apply it only to systems that are experiencing this specific problem. To resolve this problem, contact Microsoft Product Support Services to obtain the hotfix. For a complete list of Microsoft Product Support Services telephone numbers and information about support costs, visit the following Microsoft Web site: Note In special cases, charges that are ordinarily incurred for support calls may be canceled if a Microsoft Support Professional determines that a specific update will resolve your problem. The usual support costs will apply to additional support questions and issues that do not qualify for the specific update in question. 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
STATUSMicrosoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section. This problem was corrected in Microsoft Data Access Component
2.7 Service Pack 1 Refresh and Microsoft Data Access Components 2.8.
MORE INFORMATIONYou see the problem that is mentioned in the "Symptoms"
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.
//
#include "stdafx.h"
#include "windows.h"
#include "sqlext.h"
#include "sql.h"
#include "stdlib.h"
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], "DSN") == 0)
lbUseDSN = TRUE;
if(argc > 2)
{
if(strcmp(argv[2], "TRAN") == 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*)"LocalCPR",
SQL_NTS,
(SQLCHAR*)"sa",
SQL_NTS,
(SQLCHAR*)"password1",
SQL_NTS);
if(!SQL_SUCCEEDED(iReturn))
{
GetSQLError();
return 8;
}
}
else
{
if(!SQL_SUCCEEDED(SQLDriverConnect(ghConnection,
NULL,
(SQLCHAR*)"DSN=LocalCPR;UID=sa;PWD=password1;",
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*)"SELECT * From Table1", 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("Output Value : %s\n",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);
}
Modification Type: | Minor | Last Reviewed: | 10/25/2005 |
---|
Keywords: | kbHotfixServer kbQFE kberrmsg kbDriver kbTransaction kbfix kbbug KB822841 kbAudDeveloper |
---|
|