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.
you want to use:
@Transactional(propagation = Propagation.REQUIRES_NEW)
See: Propagation
Also, your TransactionManager must be configured to allow nested transactions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With