Hello,
we have a Service task, with a simplified implementation like this:
public class DelegateCallInterface implements JavaDelegate {
private IRequestProcessor requestProcessor;
@Override
public void execute(DelegateExecution execution) throws Exception {
requestProcessor.process();
}}
When the delegate was successfully executed the transaction commits on camunda side and on our db side (using the same transaction manager). Now let’s say, that an unexpected error ocurred. The transaction will be rollbacked and marked as a failed job.
The implementation of the requestProcessor issimplified as follows:
public class SpecialRequestProcessor implements IRequestProcessor {
private OwnLogService logService;
@Override
public void process(){
// some code
Object o = doSomething(); // this i would like to have inside a seperate transaction,
// now i am expecting, that the new transaction is commited, because generateLog was handled inside seperate transaction
if(o == null){
// here i throw the exception in order to get a failed job
throw new RuntimeException("Some exception, which will cause a failed job");
}
// rest of the code
}
// this is the method i want to execute in each case (also when exception occur. A log has to be saved!!! This method is not part of the interface)
@Transactional(Transactional.TxType.REQUIRES_NEW)
public Object doSomething(){
try{
Object object = ...
// some code where exception can occur
logService.save(< someEntity without>)
return object;
}catch(Exception ex){
logService.save(< someEntity with error message>)
return null;
}
Assuming something went wrong, null is returned by doSomething and the RuntimeException is thrown. Camunda is rolling back the transaction. But also the log is not saved, although i have explicity declared method “doSomething()” to be inside a seperate transaction. The goal is, to save a log in each case. When everything went well, a succesful log-entity is saved. If an exception occur inside doSomething()
, a failing.log-entity has to be saved AND a failed job should be generated by throwing a Runtime Exception after the transaction
How can i realize, that a log is allways saved?
I would be grateful for suggestions. Thanks a lot