Oracle 更新批处理模型 - 在同一应用程序中使用两种批处理模型

发布于 2024-10-16 20:14:59 字数 1760 浏览 1 评论 0原文

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

上面的代码运行良好,我可以看到使用不同批处理模型的两个更新批次都执行得很好。有什么我错过的或者我对 jdbc 开发人员指南的解释不正确吗?

提前致谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七七 2024-10-23 20:14:59

是的,他们写的是事实:-)
但这适用于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实例的批处理模式的混合

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文