当我们无法更改游标数时,避免 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();
}
}
哪个方法调用使用游标:executeUpdate
还是 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
最佳答案
您仅在每 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();
关于java - 如何管理数百个插入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19276903/