问题
Oracle JDBC supports two distinct models for update batching: Standard Batching and Oracle Specific Batching.
According to oracle 11g JDBC Developer Guide, in any single application, you can use one model or the other, but not both. Oracle JDBC driver will throw exceptions when you mix these.
In my standalone application, the above statement does not hold true. I want to know if I am missing something.
In my application I create a OracleDataSource and do the following
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();
The above code runs well and I could see both the update batches using different batching models getting executed well. Is there anything which I missed out or my interpretation of jdbc developer guide is incorrect?
Thanks in advance
回答1:
Yes, they write the truth :-) But this aply to one instance of PreparedStatement
I looked at decompile sources of 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;
}
}
So, they realy check mixing of batch modes for instance of OraclePreparedStatement
来源:https://stackoverflow.com/questions/4976770/oracle-update-batching-models-using-both-batching-models-in-same-application