Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested Spring Transaction never rollback

I am working on a service layer that logs to a database as it performs tasks. I want to ensure that this log doesn't roll back whenever an error occurs, as I should always keep a record of failed attempts. Below is sample code to explain what it is that I want.

@Transactional(rollbackFor=Exception.class)
public void performTask()
{
    //Perform task 1
    log("task1Complete");

    //Perform task 2
    log("task2Complete");
}

@Transactional()
public void log(String message)
{
    //commit message to DB
    //This should never rollback
}

I assume the way to do it is to start a new transaction but I'm not sure how.

like image 895
Michael Allen Avatar asked Apr 01 '26 17:04

Michael Allen


1 Answers

you want to use:

@Transactional(propagation = Propagation.REQUIRES_NEW)

See: Propagation

Also, your TransactionManager must be configured to allow nested transactions.

like image 159
smp7d Avatar answered Apr 04 '26 07:04

smp7d



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!