Reputation: 1228
I am trying to insert and update on a table, by reading messages from active mq triggered via separate threads each time. At times insert query fails because of ConstraintVoilationException. I want to do a retry as the code will consider the retry attempt as an update and ConstraintVoilationException won't appear.
However, RetryPolicy is not working on this exception. How can I customize it?
Failsafe artifact version: net.jodah (2.3.3)
Here is the code sample, where 'T' is the className of my entity.
@Transactional
processTxn(T t){
RetryPolicy<T> rp = RetryPolicy<T>.handle(ConstraintVoilationException.class).withMaxRetries(3);
Failsafe.with(rp).run(() -> insertOrUpdateMethod(t));
}
@Transactional
insertOrUpdateMethod (T t){
//Fetch instance of t from DB, if it exists - fire update on a few columns
//else insert a new row
}
I have also tried this code with DataIntegrityVoilationException and SQLIntegrityConstraintVoilationException but the code doesn't retry after failure.
Upvotes: 0
Views: 1696
Reputation: 1
I am not really sure but I think the
@Transactional
processTxn(T t){
is the problem. A ConstraintViolationException has to be followed by a rollback of the current transaction. When You remove this @Transactional and use only the ID of your entity or a detached instance in the method processTxn then a new Transaction is opened in insertOrUpdateMethod and in the case of ConstraintViolationException in the next try a new transaction is used.
In this case you have to be careful that the transaction in insertOrUpdateMethod is a new transaction and not a reused transaction from a caller of processTxn.
Cheers Peter Schütt
Upvotes: 0