Oracle JDBC支持两种不同的更新批处理模型:标准批处理和Oracle特定批处理。
根据oracle 11g JDBC开发人员指南,在任何单个应用程序中,您可以使用一个模型或另一个模型,但不能同时使用两者。混合这些时,Oracle JDBC驱动程序将抛出异常。
在我的独立应用程序中,上述声明并不成立。我想知道我是否遗漏了什么。
在我的应用程序中,我创建了一个OracleDataSource并执行以下操作
connection = datasource.getConnection();
preparedStatement = connection.prepareStatement("update CAR set CAR_NAME=?, OBJECT_VERSION=? where CAR_ID=?");
for(Car car : cars) {
preparedStatement.setString(1, car.getName());
preparedStatement.setInt(2, car.getVersion() + 1);
preparedStatement.setLong(3, car.getId());
preparedStatement.addBatch();
}
System.out.println("Update Batch : " + Arrays.toString(preparedStatement.executeBatch()));
for(Car car : cars) {
car.setName("v car " + car.getId());
}
//Oracle Update Batching
connection.setAutoCommit(false);
PreparedStatement preparedStatement =
connection.prepareStatement("update CAR set CAR_NAME=?, OBJECT_VERSION=? where CAR_ID=?");
//Change batch size for this statement to 3
((OraclePreparedStatement)preparedStatement).setExecuteBatch (10);
for(Car car : cars) {
preparedStatement.setString(1, car.getName());
preparedStatement.setInt(2, car.getVersion() + 1);
preparedStatement.setLong(3, car.getId());
System.out.println("Execute Update Count " + preparedStatement.executeUpdate());
}
System.out.println("Update Count : " + ((OraclePreparedStatement)preparedStatement).sendBatch()); // JDBC sends the queued request
connection.commit();
preparedStatement.close();
System.out.println("Update Batch : " + Arrays.toString(preparedStatement.executeBatch()));
for(Car car : cars) {
car.setName("v car " + car.getId());
}
//Oracle Update Batching
connection.setAutoCommit(false);
PreparedStatement preparedStatement =
connection.prepareStatement("update CAR set CAR_NAME=?, OBJECT_VERSION=? where CAR_ID=?");
//Change batch size for this statement to 3
((OraclePreparedStatement)preparedStatement).setExecuteBatch (10);
for(Car car : cars) {
preparedStatement.setString(1, car.getName());
preparedStatement.setInt(2, car.getVersion() + 1);
preparedStatement.setLong(3, car.getId());
System.out.println("Execute Update Count " + preparedStatement.executeUpdate());
}
System.out.println("Update Count : " + ((OraclePreparedStatement)preparedStatement).sendBatch()); // JDBC sends the queued request
connection.commit();
preparedStatement.close();
上面的代码运行良好,我可以看到使用不同批处理模型的更新批次执行得很好。有什么我错过了或我对jdbc开发者指南的解释不正确吗?
提前致谢
答案 0 :(得分:2)
是的,他们写下真相:-) 但这适用于一个 PreparedStatement实例
我查看了OraclePreparedStatement的反编译源:
public void addBatch() throws SQLException {
synchronized(connection){
setJdbcBatchStyle();
processCompletedBindRow(currentRank + 2, currentRank > 0 && sqlKind.isPlsqlOrCall());
currentRank++;
}
}
final void setJdbcBatchStyle() throws SQLException {
if(m_batchStyle == 1){
SQLException sqlexception = DatabaseError.createSqlException(getConnectionDuringExceptionHandling(), 90, "operation cannot be mixed with Oracle-style batching");
sqlexception.fillInStackTrace();
throw sqlexception;
} else{
m_batchStyle = 2;
return;
}
}
因此,他们真的会检查批处理模式的混合,例如OraclePreparedStatement