我正在将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上有可用的示例吗?

最佳答案

无法关闭与数据库的连接并保存结果集(occi::ResultSet)供以后使用。原因之一是occi::ResultSet::next从数据库中检索数据。相反,您可以使用数组获取和用户分配的数据缓冲区来存储结果。

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);

请注意,数组提取的行为与预取类似
Status next(
   unsigned int numRows =1);

每次调用最多提取numRows个。

09-26 01:02