Oracle OCI OCIStmtFetch2挂起游标C ++

时间:2014-09-16 10:46:40

标签: c++ oracle oci

当我尝试迭代游标时,我的代码挂起。 execute语句返回大约800k结果(当我在sqlplus中运行时)。当我做类似的事情调用一个更简单的存储过程,结果为20时,它会立即返回。我已经把程序挂了超过12个小时仍然没有返回。我正在使用Oracle11g oci.lib,但是当我使用9i版本的lib测试时,它会在5秒内返回。使用9i Oci对我来说不是一个选择(出于我无法控制的原因)。我怀疑OCIStmtFetch2()调用是先预先缓存所有结果。是否可以停止此操作并使光标可用?

存储过程声明:

CREATE OR REPLACE PROCEDURE FXT_TEST_CALL(CRESULTS OUT SYS_REFCURSOR) IS

以下是代码:(我已经删除了一些样板文件)

OCIStmt* pOciStatement;
OCIStmt* cursor;
OCIEnv* g_pOciEnvironment = NULL;
OCIServer* g_pOciServer = NULL;
OCISession* g_pOciSession = NULL;
OCISvcCtx* g_pOciServiceContext = NULL;
char* sqlCharArray = "BEGIN fxt.fxt_test_call(:refCursor ); END;";

answer = OCIInitialize(OCI_THREADED, NULL, NULL, NULL, NULL);
answer = OCIEnvInit(&g_pOciEnvironment, OCI_DEFAULT, 0, NULL);
answer = OCIHandleAlloc(g_pOciEnvironment, (void **)&pOciError, OCI_HTYPE_ERROR, 0, NULL);
answer = OCIHandleAlloc(g_pOciEnvironment, (void **)&g_pOciSession, OCI_HTYPE_SESSION, 0, NULL);
answer = OCIHandleAlloc(g_pOciEnvironment, (void **)&g_pOciServer, OCI_HTYPE_SERVER, 0, NULL);
answer = OCIHandleAlloc(g_pOciEnvironment, (void **)&g_pOciServiceContext, OCI_HTYPE_SVCCTX, 0, NULL);
answer = OCIServerAttach(g_pOciServer, pOciError, (unsigned char *)pConnectChar, strlen(pConnectChar),OCI_DEFAULT);
answer = OCIAttrSet(g_pOciSession, OCI_HTYPE_SESSION, (unsigned char *)pUsernameChar, strlen(pUsernameChar),OCI_ATTR_USERNAME, pOciError);
answer = OCIAttrSet(g_pOciSession, OCI_HTYPE_SESSION, (unsigned char *)pPasswordChar, strlen(pPasswordChar),OCI_ATTR_PASSWORD, pOciError);
answer = OCIAttrSet(g_pOciServiceContext, OCI_HTYPE_SVCCTX, g_pOciServer, 0, OCI_ATTR_SERVER, pOciError);
answer = OCIAttrSet(g_pOciServiceContext, OCI_HTYPE_SVCCTX, g_pOciSession, 0, OCI_ATTR_SESSION, pOciError);
answer = OCISessionBegin(g_pOciServiceContext, pOciError, g_pOciSession, OCI_CRED_RDBMS, OCI_DEFAULT);
answer = OCIHandleAlloc(g_pOciEnvironment, (void **)(&pOciStatement), OCI_HTYPE_STMT, 0, NULL);
answer = OCIStmtPrepare(pOciStatement, pOciError, (unsigned char *)sqlCharArray, strlen(sqlCharArray),OCI_NTV_SYNTAX, OCI_DEFAULT);
answer = OCIHandleAlloc(g_pOciEnvironment, (void **)(&cursor), OCI_HTYPE_STMT, 0, NULL);

answer = OCIBindByPos(pOciStatement,&pBind, pOciError, 1, &cursor, 0,SQLT_RSET, pIndicator2, 0,NULL, 0,0,OCI_DEFAULT);

OCINumber numTest;

answer = OCIStmtExecute(g_pOciServiceContext, pOciStatement, pOciError, 1, 0, NULL, NULL, OCI_COMMIT_ON_SUCCESS);
answer = OCIDefineByPos(cursor,&pOciDefine2, pOciError,1,&numTest,sizeof(OCINumber), SQLT_VNU, 0, 0, 0,OCI_DEFAULT);

// Program hangs here
while ((answer = OCIStmtFetch2(cursor,pOciError, 1,OCI_FETCH_NEXT,0,OCI_DEFAULT)) == 0)
{
    OCIAttrGet(cursor, OCI_HTYPE_STMT, (void*)&fetched, NULL, OCI_ATTR_ROWS_FETCHED, pOciError);
    long long newNum =  0;
    OCINumberToInt(pOciError, &numTest, sizeof(long long), OCI_NUMBER_SIGNED , &newNum);
}

1 个答案:

答案 0 :(得分:0)

由于这已经闲逛了一段时间没有回答我会回答任何好奇的人,我是如何解决这个问题的。它在MFC调试版本中运行缓慢,因此我在发布模式下运行并且它有效(暂停2分钟)。

我仍然没有看到它会产生什么不同,也许这些库在调试/发布版本中的工作方式不同。

无论如何,至少目前它不再是一个问题。