I am currently using the spring @transactional
annotation to make a series of database changes. Upon any exception that is not thrown by me (e.g. my UPDATA clause violates the column constraints in the table), the transaction is successfully rolled back. However, no error message is printed in the console.
I would like to handle such cases manually. For example, I may want to record such events in a txt file. I am wondering if there is a way for me to catch such exceptions(or more accurately, have a listener for such events).
Relevant Code
@Transactional
public class DefaultFooService implements FooService {
public Foo getFoo(String fooName) {
INSERT A ROW
UPDATE A ROW
}
}
In the code above, assume that INSERT A ROW
succeeds but UPDATE A ROW
failed due to SQLException
(for example due to column constraints violation). Currently, no error message is printed in the console with logging set to INFO. When I use DEBUG, I could see that SQLException
is detected and the tx is rolled back.
How could I be notified when the transaction failed and is rolled back?
======
This question is somewhat different from dynamically register transaction listener with spring? in that I want to be notified if and only if my transaction has failed and I want to know why it has failed.
To illustrate the problem differently. In the following code block, no exception will be thrown (printed in stdout) currently. How could I catch/handle the RuntimeException?
@Transactional
public class DefaultFooService implements FooService {
public Foo getFoo(String fooName) {
throw new RuntimeException();
}
}