Thứ Sáu, 13 tháng 11, 2015

Spring Data JPA: rollback if specified exception is throw

Link: http://stackoverflow.com/q/14729227

@Transactional(rollbackFor = { ServiceException.class }, noRollbackFor={IllegalArgumentException.class})
    public void updateUser(User user) throws Exception {
        try {
            userRepository.save(user);
           
            if (user.getName().isEmpty()) {
                throw new IllegalArgumentException("Name must be has characters");
            }
           
            if (user.getEmail().equals("admin@gmail.com")) {
                throw new ServiceException("Update Admin");
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw e;
        }
    }

Using rollbackFor and noRollbackFor, for rollback or commit.

Continue ...

- Comments

0 nhận xét:

Đăng nhận xét