使用Oracle API短时间内持续连接Oracle server,会出现“ORA-12516: TNS:listener could not find available handler with matching protocol stack”错误,同时会造成内存泄漏。
测试代码如下:
#include <iostream>
#include <deque>
#include <cstdlib>
#include <string>
#include <stdio.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
#include <occi.h>
using namespace oracle::occi;
using namespace std;
int main()
{
oracle::occi::Statement *stmt1;
oracle::occi::Environment *env;
oracle::occi::Connection *conn1;
ResultSet *rs;
char tmp[512];
while (1)
{
try{
env = oracle::occi::Environment::createEnvironment(oracle::occi::Environment::DEFAULT);
conn1 = env->createConnection("system", "123456", "172.168.0.120:1521/XE");
stmt1 = conn1->createStatement("select count(*) from emp");
rs=stmt1->executeQuery();
while (rs->next())
{
int num = rs->getInt(1);
sprintf((char *)tmp, "%d", num);
cout << tmp << endl;
}
stmt1->closeResultSet(rs);
conn1->terminateStatement(stmt1);
env->terminateConnection(conn1);
oracle::occi::Environment::terminateEnvironment(env);
}catch(oracle::occi::SQLException &ex)
{
cout<<ex.getMessage()<<endl;
}
usleep(10*1000);
}
}
以上测试代码编译命令:
g++ -I/usr/include/oracle/11.2/client64 -L/usr/lib/oracle/11.2/client64/lib/ -locci -lclntsh -o test test.cpp
运行上述测试代码,出现的错误:
运行过程中,内存使用情况:
上图显示,虚拟内存和物理内存都出现了显著增长。
以上测试代码,如果将usleep(10000)时间改长一点,比如改成usleep(1000000),则不会有问题。