Currently we are facing a spring transaction related issue in our application.
As you can see that in deleteRecord()
we are doing a DB operation. But in the next line
a business exception is thrown.
Expected Behavior(As per my knowledge) : The DB operation should be rolled back as exception is thrown from the next line
Actual Behavior : Its not getting rolled back. Data is getting inserted to the table
Question :
Why transaction is not getting rolled back ? I dont think its because of the catch block
because deleteRecord()
will be executed in new transaction. Please correct me if I am wrong
Code:
class A {
void myMethod() {
for(int i=0 ; i<count ; i++) {
try {
deleteRecord();
} catch(Exception e) {
log.error("Exception caught");
}
}
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
deleteRecord() throws Exception{
line 1 : deleting record
line 2 : Throwing business exception
}
}