Search code examples
javaspringspring-transactionstransactional

Spring @Transactional behaviour in case of multiple update queries


I have a method that should perform multiple updates and want to be sure that if one update fails then no updates are commited to the database, the code looks like:

    private void updateStatuses(List<Status> statuses, String docId) {
        statuses.forEach(status-> {
            Long nextVersionNumber = Long.parseLong(status.getRecordVersionNbr()) + 1;
            getJdbcTemplate().update(
                    UPDATE_FUNDS_TRANSFER_DOC_ID_SQL,
                    nextVersionNumber,
                    docId,
                    status.getStatusId());
        });
    }

I want to annotate this method with org.springframework.transaction.annotation. @Transactional, would this work for me? Should I specify some extra-parameters?


Solution

  • It will work like charms! :)

    I hope you have already configured the Transaction Manager in your Spring Configuration.

    If you want to specify the Propagation and Rollback then you can do it as follows:

    @Transactional(propagation=Propagation.REQUIRED, rollbackFor=DAOException.class)