我使用OCCI和C++从Oracle获取数据。代码运行良好,但我注意到性能有所下降。这是因为在rset->next()迭代中,某些计算需要时间。此延迟的影响是oracle连接池有一个连接繁忙。如果并发请求需要相同的计算,可能池中的所有连接都会很忙。
Statement *stmt = conn->createStatement (sqlQuery);
ResultSet *rset = stmt->executeQuery ();
while (rset->next ())
{
//Slow computation takes time
compute()
}
stmt->closeResultSet (rset);
conn->terminateStatement (stmt);
env->terminateConnection (conn);因此,我的问题是:我可以复制Occi::ResultSet (使用共享指针)吗?为了在复制后关闭连接并在释放连接后进行计算?
go_to_oracle( ResultSet &result) {
Statement *stmt = conn->createStatement (sqlQuery);
ResultSet *rset = stmt->executeQuery ();
copy_rset_to_result;
stmt->closeResultSet (rset);
conn->terminateStatement (stmt);
env->terminateConnection (conn);
}
my_method() {
ResultSet *result = NULL
go_to_oracle(result);
//here connection is closed, but we have the data
compute(result) // do this without have connection occupied
}在GitHub上有可用的例子吗?
发布于 2017-10-16 09:20:24
无法关闭到数据库的连接并保存结果集(occi::ResultSet)供以后使用。原因之一是occi::ResultSet::next从数据库中检索数据。相反,可以使用数组fetch和用户分配的数据缓冲区来存储结果。
使用occi::ResultSet::setDataBuffer:的示例
oracle::occi::ResultSet* rs=nullptr;
//.....
// query
//.....
static const size_t max_numrows=5000;
char var_buf[max_numrows][7];
char sym_buf[max_numrows][9];
rs->setDataBuffer(1,var_buf,oracle::occi::OCCI_SQLT_STR,sizeof(var_buf[0]),(ub2*)0);
rs->setDataBuffer(2,sym_buf,oracle::occi::OCCI_SQLT_STR,sizeof(sym_buf[0]),(ub2*)0);
size_t fetch_count=0;
while(rs->next(max_numrows)==ResultSet::DATA_AVAILABLE)
{
/* This would probably be an error as you would like
the whole result to fit in the data buffer.*/
}
stmt->closeResultSet (rs);
conn->terminateStatement (stmt);
compute(var_buf,sym_buf);注意,数组fetch的作用类似于
Status next(
unsigned int numRows =1);每次呼叫取到numRows。
发布于 2017-10-13 11:22:51
执行查询不检索数据。使用rset->next ()从服务器读取数据。因此,如果您终止连接-您不能读取数据。
https://stackoverflow.com/questions/46685925
复制相似问题