Search code examples
javasqlspringjdbctemplateautocommit

Disabling auto-commit in NamedParameterJdbcTemplate.batchUpdate


I am doing a batch update into my DB table using NamedParameterJdbcTemplate.batchUpdate, but I would like to disable auto-commit and perform the commit manually.

I can set auto-commit mode off from the connection object, but not sure how to do the same using NamedParameterJdbcTemplate object.


Solution

  • I have done my implementation using TransactionTemplate

    It has an execute method and I do the business logic inside a callback in this function.

    transTemplate.execute( new TransactionCallbackWithoutResult()
            {       
                    @Override
                    protected void doInTransactionWithoutResult( TransactionStatus status)
                    {
                        status.setRollbackOnly();
                        //business logic
                    }
    
            });