I have a API , That looks like below
@Transaction
void method (){
try{
service1.insertOne();
service2.insertTwo();
}
catch(Exception ex) {
// log exception
}
}
In Service classes, I am checking for certain validation and I am throwing an exception which is a subclass of RuntimeException
. When i throw this exception, javax.persistence.RollbackException: Transaction marked as rollbackOnly
. While it is preventing the data in the first service from not being inserted , since the second service validation has failed, I am not quite quite sure if this is the right way to handle this scenario.
In case if the Exception is not a sub-class of Exception
, even when the validation for service2 fails , data in service1 gets inserted, but i do see the custom exception being thrown. So i am not sure where i am going wrong. Any help is appreciated.