---
title: "重现ORA-021500(17099)错误，并提供解决办法"
author: "ah_fu"
date: "2008-04-19T13:02:00Z"
tags: ["C", "Linux", "Oracle", "SQL", "多线程"]
slug: "ora-021500-17099"
draft: false
lastmod: "2008-04-19T13:02:00Z"
id: 281552
url: "https://ah-fu.iteye.com/blog/281552"
---

两天前写的一篇文章提到公司的应用程序常常出现ORA-21500错误（http://blog.csdn.net/ah\_\_fu/archive/2008/04/16/2296641.aspx），在网上找了很久，终于有以下资料提供了些头绪：

http://www.dbatools.net/doc/bug10204.html     Bugs fixed in the 10.2.0.4 Patch Set  
http://blogs.sun.com/mandalika/tags/oracle     Solaris/SPARC: Oracle 11gR1 client for Siebel 8.0

    从BLOG http://blogs.sun.com/mandalika/tags/oracle 的最后几句话：

Although I'm not sure what exactly is the underlying issue for the core dump, my suspicion is that there is some memory corruption in Oracle client's code; and the Siebel Object Manager crash is due to the Oracle bug **5682121 - Multithreaded OCI clients do not mutex properly for LOB operations**. The fix to this particular bug would be available in Oracle 10.2.0.4 release; and is already available as part of Oracle 11.1.0.6.0. In case if you notice the symptoms of failure as described in this blog post, **upgrade the Oracle client in the application-tier to Oracle 11gR1** and see if it brings stability to the Siebel environment.

    得知，这个专家认为这个错误是ORACLE客户端的BUG导致的，必须要将客户端升级到ORACLE 10G才能解决。（P.S 公司使用的ORACLE版本是9.0.4）

   文档http://www.dbatools.net/doc/bug10204.html 中搜索ORA-21500也发现一行这样的信息：  
       BUG ID; 5682121  
       Bug: Multithreaded OCI clients do not mutex properly for LOB operations (ORA-21500 \[17099])  
           我们的代码也的确是在多线程环境下使用PRO\*C导致了ORA-21500（P.S PRO\*C实际上只是预编译器，仍然需要将PC的代码转换成对OCI的调用，ORACLE应该只用一种API接口，那就是OCI）

   因此，到处寻找ORACLE 10G Client for Linux的光盘，找了半天也没找到。没办法，只好自己想办法让这个BUG重现，便于确定是否是自己的代码的问题。  
    出问题的代码在于自己封装了一个数据库的连接池，其原理是预先分配N个连接，然后每个线程需要连接的时候从连接池中获取一个空闲连接来使用，使用完后立即将连接释放回连接池。这样，每个线程不必一直占用一个连接，也不必每次都直接连接数据库。连接池的代码很简单，看了几遍也没发现哪儿有问题。于是编写了以下的代码来模拟连接池的原理，尝试让ORA-21500重现。

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//ORA21500.pc  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)#ifndef ORA\_PROC  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    #include &lt;stdio.h&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    #include &lt;string.h&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    #include &lt;stdlib.h&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    #include &lt;pthread.h&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    // 必须先定义这个宏，避免在每个PC文件中生成全局的sqlca变量  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    #define SQLCA\_NONE  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    #include &lt;sqlca.h&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)#endif  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)#define MAX\_CONN 4  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)#define CONN\_STR "webuser/webuser@bsmp"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*//// 去除PROC函数内的“变量未使用”的警告的宏  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)#define NO\_WARNING {sqlstm.sqlvsn = sqlstm.sqlvsn;}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*//// 分配一个全局的连接池  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)EXEC SQL BEGIN DECLARE SECTION;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)sql\_context ctx\[MAX\_CONN];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)EXEC SQL END DECLARE SECTION;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*//// 线程函数  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)void* read\_data(void* param)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    EXEC SQL BEGIN DECLARE SECTION;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    struct sqlca sqlca;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    int* index = (int\*)param;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    int nCount = 0;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    int user\_id;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    EXEC SQL END DECLARE SECTION;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    printf("%s %d: index=%d thread=%d ", \_\_FUNCTION\_\_, \_\_LINE\_\_, \*index, (int)pthread\_self());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    EXEC SQL CONTEXT USE :ctx\[\*index];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    EXEC SQL DECLARE CurUser CURSOR FOR   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        SELECT user\_id FROM webuser.user\_info WHERE rownum&lt;=100000;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    EXEC SQL OPEN CurUser;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    do  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        EXEC SQL FETCH CurUser INTO :user\_id;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        nCount++;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    } while(sqlca.sqlcode==0);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    EXEC SQL CLOSE CurUser;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    printf("%s %d: user count=%d index=%d thread=%d ", \_\_FUNCTION\_\_, \_\_LINE\_\_,   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        nCount, \*index, (int)pthread\_self());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    return NULL;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    NO\_WARNING;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*//// 创建连接池和线程  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)void test()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    EXEC SQL BEGIN DECLARE SECTION;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    struct sqlca sqlca;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    int i;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    char ConnStr\[100];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    int index\[MAX\_CONN];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    EXEC SQL END DECLARE SECTION;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    pthread\_t threads\[MAX\_CONN];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    strcpy(ConnStr, CONN\_STR);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    memset(threads, 0, sizeof(threads));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    /\*\*//// 创建连接池  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    for (i=0; i&lt;MAX\_CONN; i++)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        EXEC SQL CONTEXT ALLOCATE :ctx\[i];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        EXEC SQL CONTEXT USE :ctx\[i];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        EXEC SQL CONNECT :ConnStr;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        if (sqlca.sqlcode != 0)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            printf("%s %d: 连接数据库错误：code=%d, errm=%s ", \_\_FUNCTION\_\_, \_\_LINE\_\_,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            return;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    //创建线程  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    for (i=0; i&lt;MAX\_CONN; i++)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        index\[i] = i;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        if (0!=pthread\_create(threads+i, NULL, read\_data, index+i))  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            printf("%s %d: create thread failed! ", \_\_FUNCTION\_\_, \_\_LINE\_\_);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        else  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            printf("%s %d: thread\[%d]=%d ", \_\_FUNCTION\_\_, \_\_LINE\_\_, i, (int)threads\[i]);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    //连接线程  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    for (i=0; i&lt;MAX\_CONN; i++)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        pthread\_join(threads\[i], NULL);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    printf("%s %d: end ", \_\_FUNCTION\_\_, \_\_LINE\_\_);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    return;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    NO\_WARNING;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)int main()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    test();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    return 1;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*//*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)proc userid=webuser/webuser@bsmp parse=partial code=cpp sqlcheck=full threads=yes char\_map=string def\_sqlcode=false oraca=false objects=false errors=true lines=true auto\_connect=no ltype=none hold\_cursor=yes release\_cursor=no iname=ORA21500.pc oname=ORA21500.cpp cpool=yes cmax=5 cmin=3 cincr=2 ctimeout=2 cnowait=7   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)g++ -o ORA21500.o -c ORA21500.cpp -g -Wall -Werror -I"${ORACLE\_HOME}/precomp/public"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)g++ -o ORA21500.exe ORA21500.o  "${ORACLE\_HOME}/lib/libsql9.a" "${ORACLE\_HOME}/lib/libclntsh.so" -lpthread  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

    代码的最后几行是编译命令。编译后执行这个程序，几乎每次都发生core dump, 但是出现ORA-21500信息的次数较少。以上的代码实际上也就是我封装的连接池的原理所在。如此简单的代码也能崩溃掉，看来这真的是ORACLE客户端的BUG无疑了。

    正当打算将客户端安装成ORACLE 10g的时候，突然想到修改一下PRO\*C的编译参数试试。  
    于是将proc编译参数中的连接池信息去掉：cpool=yes cmax=5 cmin=3 cincr=2 ctimeout=2 cnowait=7  
    再编译执行以上代码，结果让我大跌眼睛（还好我不戴眼睛）————居然运行完全正确，没有发生core dump!  
    恩，从上面的现象看来，ORA-21500内部错误这个BUG与ORACLE的连接池机制有关。关闭连接池暂时可以屏蔽掉这个错误，但是不能肯定在其他环境下不会出现。

   OK，综上所述：  
1、在PRO\*C的编译选项中，如果使用了连接池，多线程下会出现ORA-21500错误；去掉连接池选项即可解决；  
2、直接调用OCI代码，在多线程和连接池同时使用的情况下，也可能会出现这个错误；  
3、不管怎么样，ORA-21500\[17099]始终是一个BUG，最好还是换成ORACLE新版的产品。