Do I need connection.commit () after executeBatch ()?

I need to check my colleague's code, and I came across this piece of code:

private void pdate(JdbcTemplate jdbcTemplate, List<Long> saisineIdsToUpdate,Connection connection) throws SQLException {
    String sqlUpdate = "UPDATE SAISINES SAI WHERE SAI.IDSAISINE = ?"; //request simplified

    PreparedStatement psUpdate = connection.prepareStatement(sqlUpdate);

    for (Long saisineId : saisineIdsToUpdate) {
        psUpdate.setLong(1, saisineId );
        psUpdate.addBatch();

    }
    psUpdate.executeBatch();
    psUpdate.close();

The code works, the updates are correct, but I can’t find the trace. connection.commit(); I wonder how this can work without committing - can someone explain why?

+4
source share
2 answers

As explained here , JDBC drivers usually use autocommit, you can enable database tracing using specific DBMS driver parameters, such as showSQLor generateDDLin JPA.

JDBC , Connection setAutoCommit(). false setAutoCommit (), . true, .

+3

auto-commit false,

connection.setAutoCommit(false);
// your code goes here
connection.commit();

auto-commit, true,

+1

All Articles