Are addBatch () and executeBatch () thread safe?

Is it possible to call executeBacth from a stream while another calls addBatch () in the same statement (or PreparedStatement)?

Update: Does anyone have any experience with this? because I get the wrong results. Not all updates added to the package are performed.

+5
source share
5 answers

I would take a step back and deeply revise the design. Why would you like to use the same Statement(and therefore implicitly also Connection) between two threads?

JDBC , Connection, Statement ResultSet . . :

public void update(List<Item> items) throws SQLException {
    Connection connection = null;
    PreparedStatement statement = null;
    try {
        connection = database.getConnection();
        statement = connection.prepareStatement(sql);
        for (Item item : items) {
            statement.setObject(1, item.getSomething());
            statement.addBatch();
        }
        statement.executeBatch();
    } finally {
        if (statement != null) try { statement.close(); } catch (SQLException ignore) {}
        if (connection != null) try { connection.close(); } catch (SQLException ignore) {}
    }
}

, . C3P0. , , ! , . .

, : , . , , Java, .

+17

. JDBC, JDBC :

API JDBC 3.0, A.1.6


BalusC, ResultSet PreparedStatements . ( , JDBC, ). , JDBC Oracle, , , , , . , , , , , , . , .

+6

PreparedStatement . , .

ex: - DelegatingPreparedStatement , , OraclePreparedStatement ' , .

+3

, . , , ​​ . , , , , , .

addBatch executeBatch, . , , Oracle ( ), . , , - , .

+3

This can be very specific to JDBC. I would rather never rely on concurrent access to the batch version and delegate the complexity of concurrency to the database server. why not have only more independent connections?

+1
source

All Articles