1

ORA-0100: Maximum open cursors exceeded当我们无法更改游标数量时,最好的避免方法是什么?

有没有比以下更好的方法:

Connection connection = DriverManager.getConnection(/* Oracle Driver */);
Statement st = null;
  st = connection.createStatement();
for (/* a lot of iteration with counter */) {

  st.executeUpdate(insertSql);

  if ( counter % 500 == 0) {
    st.close();
    connection.commit();
    st = connection.createStatement();
  }
}

哪个方法调用使用游标:toexecuteUpdate还是 to createStatement
我认为这executeUpdate就是我制作这个计数器的原因。

对于我工作的 Oracle:

select * from v$version;

结果 :

BANNER
----------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
PL/SQL Release 11.2.0.1.0 - Production
CORE 11.2.0.1.0 Production
TNS for 32-bit Windows: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
4

2 回答 2

4

您只关闭每50个语句...

for (/* a lot of iteration with counter */) {
  st = connection.createStatement(); //you create a statement each iteration!

  //...

  // This whole thing not right here
  if ( counter % 50 == 0) {
    st.close(); // not right here -- will have 49 open statements by now
    connection.commit(); //this too
  }

}

您应该使用准备好的语句,并批量插入此数量的数据。

 PreparedStatement statement = connection.prepareStatement(insertTableSQL);
 for(<the objects you have>) {

   //set parameters into insert query
   statement.set*.*(1, <paramValue>);

   statement.addBatch(); //add it to the batch
 }

 statement.executeBatch();//execute whole batch
 connection.commit();
于 2013-10-09T16:10:58.447 回答
2

您只需要一个语句来处理所有操作,因此您可以在循环之外创建语句。

Connection connection = DriverManager.getConnection(/* Oracle Driver */);
Statement statement = connection.createStatement();
for (/* a lot of iteration with counter */) {
  // do some INSERT, SELECT, UPDATE
}
statement.close();
connection.close();

现在,您可以在循环内运行查询,例如:

statement.executeUpdate("query");
于 2013-10-09T16:15:51.997 回答