PreparedStatement , CallableStatement and Performance Considerations

前端 未结 2 1109
眼角桃花
眼角桃花 2020-12-31 08:51

I have a oracle stored proc that needs to be called from my Java program. I had used CallableStatement to pass parameters to the stored proc. I am

相关标签:
2条回答
  • 2020-12-31 09:20

    From your comment, you have prepareCall inside your loop. An advantage of prepared statements (and callable statements) is that you can prepare it once, and then swap out the values passed in the parameters; there is overhead each time the call is prepared, so if you could bring that outside of your loop, you may find that run time decreases. You may find that turning off AutoCommit also helps, as there is overhead with each commit.

    conn.setAutoCommit(false);
    CallableStatement stmt = conn.prepareCall(sql);
    while(true) {
        stmt.setInt(1, value);
        stmt.execute();
    }
    conn.commit();
    conn.setAutoCommit(true);
    

    (conn.setAutoCommit(true) does commit, but I find it clearer to be explicit).

    0 讨论(0)
  • 2020-12-31 09:27

    Shouldn't you consider using batch?

    conn.setAutoCommit(false);
    CallableStatement stmt = conn.prepareCall(sql);
    while(true) {
        stmt.setInt(1, value);
        stmt.addBatch();
    }
    stmt.executeBatch()
    conn.commit();
    
    0 讨论(0)
提交回复
热议问题