Issue
@Transactional(noRollbackFor = {SQLIntegrityConstraintViolationException.class, ConstraintViolationException.class, DataIntegrityViolationException.class})
public void createLead(List<Info> infos)
{
for (Info info : infos)
{
try
{
repo.save(info.toDao());
}
// should catch Duplicate entry (SQLIntegrityConstraintViolationException, ConstraintViolationException, DataIntegrityViolationException)
catch (Throwable ignore)
{
}
}
}
I want to insert all List<Info>
to the database and rollback if any errors (except for duplicate entry
occurs), but even with a try/catch and the noRollbackFor configuration, the transaction is being rolledback, why?
Solution
I would assume the exceptions you included in noRollbackFor
are being thrown at commit time (when the transaction interceptor tries to commit current session) and naturally since the commit is not possible, the transaction is rolled back.
That means Spring respect your request not to rollback on those exceptions but since it's happening in commit phase it simply can do anything else other than rollback transaction and rethrow the exception. There is nothing you can do about it - this is how the mechanism of @Transactional
work.
Answered By - Piotr Podraza
Answer Checked By - Mary Flores (JavaFixing Volunteer)