Microsoft Knowledge Base Email Alertz

(294149) - When ODBC connection pooling is disabled, using the following sequence in a loop does not result in a Function sequence error: SQLExecDirect SQLFetch SQLSetStmtOption (SQL_ATTR_AUTOCOMMIT,SQL_AUTOCOMMIT_OFF) SQLTransact(SQL_COMMIT) SQLSetStmtOption...

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 294149 - Last Review: December 5, 2003 - Revision: 4.1

BUG: Function Sequence Error Not Returned for Transactions When Connection Pooling Is Disabled

This article was previously published under Q294149

On This Page

SYMPTOMS

When ODBC connection pooling is disabled, using the following sequence in a loop does not result in a "Function sequence error":
SQLExecDirect
SQLFetch 
SQLSetStmtOption (SQL_ATTR_AUTOCOMMIT,SQL_AUTOCOMMIT_OFF) 
SQLTransact(SQL_COMMIT)
SQLSetStmtOption (SQL_ATTR_AUTOCOMMIT,SQL_AUTOCOMMIT_ON) 
SQLFetch
				
When connection pooling is enabled, a "Function sequence error" is returned on the second iteration.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

When an operation is committed or rolled back, all statement handles of any resultsets are freed unless the SQL_PRESERVE_CURSORS option is set to ON. For this reason, subsequent fetch operations should fail.

Steps to Reproduce Behavior

  1. From Query Analyzer, run the following code to create the table "sytest":
    if exists (select * from sysobjects where id = object_id(N'[dbo].[sytest]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
       drop table [dbo].[sytest]
       GO
    
    create table sytest(col1 int)
    insert into sytest Values(1)
    insert into sytest Values(2)
    insert into sytest Values(3)
    					
  2. Create a new Win32 console application and paste the following code in your project:
    #include <windows.h>
    #include <sql.h>
    #include <sqlext.h>
    #include <stdio.h>
    
    int StmtError(HSTMT);
    
    void main(void)
    {
        SQLHENV henv;
        SQLHDBC hdbc;
        SQLHSTMT hstmt;
        SQLRETURN nstatus;
        SQLCHAR szConnect[1024];
    
        // Enable connection pooling.
        nstatus = SQLSetEnvAttr(NULL,SQL_ATTR_CONNECTION_POOLING,(SQLPOINTER)SQL_CP_ONE_PER_DRIVER,SQL_IS_INTEGER);
        nstatus = SQLAllocHandle(SQL_HANDLE_ENV,NULL,&henv);
        nstatus = SQLSetEnvAttr(henv,SQL_ATTR_ODBC_VERSION,(SQLPOINTER) SQL_OV_ODBC3,0);
        nstatus = SQLAllocHandle(SQL_HANDLE_DBC,henv,&hdbc);
    
        for (int i=0;i<=10;i++)
        {
    	nstatus = SQLDriverConnect(hdbc,NULL,(unsigned char *)"DSN=toCaveman;UID=sa;PWD=Password1;", 
    		SQL_NTS, szConnect, 256, NULL, SQL_DRIVER_NOPROMPT);
    
    	nstatus = SQLAllocHandle(SQL_HANDLE_STMT,hdbc,&hstmt);
    	nstatus = SQLExecDirect(hstmt,(SQLCHAR*) "select col1 from sytest",SQL_NTS);
    	nstatus = SQLFetch(hstmt);
    	nstatus = SQLSetConnectAttr(hdbc,SQL_ATTR_AUTOCOMMIT,(void *)SQL_AUTOCOMMIT_OFF,0);
    	nstatus = SQLTransact(henv,hdbc,SQL_COMMIT);
    	nstatus = SQLSetConnectAttr(hdbc,SQL_ATTR_AUTOCOMMIT,(void *)SQL_AUTOCOMMIT_ON,0);
    	nstatus = SQLFetch(hstmt);
    	
    	if(nstatus == SQL_ERROR)
    	{
    	StmtError(hstmt);   
    	}
    	
    	nstatus = SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
    	nstatus = SQLDisconnect(hdbc);
        }
    
        nstatus = SQLFreeHandle(SQL_HANDLE_DBC,hdbc);
        nstatus = SQLFreeHandle(SQL_HANDLE_ENV,henv);
    
        printf("Done");
    } 
    
    StmtError(HSTMT hstmt)
        {
        // Variables for SQLDiagRec.
        RETCODE rc = 0;
        char mstate[6] = "\0";
        long native = 0;
        char mtext[300] = "\0";
        short mlength = 0;
        short i = 0;
    
        while ((rc = SQLGetDiagRec(SQL_HANDLE_STMT, hstmt, ++i, (unsigned char *)&mstate, 
       		&native, (unsigned char *)&mtext, 300, &mlength)) != SQL_NO_DATA)
            printf("\nODBC Error:\t%s\n",mtext);
    
        return(0);
        };
    					
  3. Change the connection string to use your database and server.
  4. Compile and then run the code. On the second iteration, you should receive the following error in the console window:
    [Microsoft][ODBC Driver Manager] Function sequence error
  5. Comment out the line of code where SQLSetEnvAttr is called to enable the connection pooling.
  6. Recompile and run the code. You should not see any errors.

APPLIES TO
  • Microsoft Data Access Components 2.0
  • Microsoft Data Access Components 2.1
  • Microsoft Data Access Components 2.5
  • Microsoft Data Access Components 2.6
Keywords: 
kbbug kbpending KB294149
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate