Answer the question
In order to leave comments, you need to log in
Throwable or Exception?
The other day I came up with a problem and, as it happens, I tried to solve it. There is a two part procedure. If there is an error between the first and second parts, you need to roll back the first one at all costs. If an error occurred during the second part, then you need to roll back the second part, and then the first and nothing else. As a result, the try/catch monster was born.
public void doAndForget() {
try {
service.doFirstPart();
try {
service.doSecondPart();
} catch (Throwable t) {
Log.warn(t);
try {
service.doRollbackSecondPart();
try {
service.doRollbackFirstPart();
} catch(Throwable t1) {
Log.error(t1);
}
} catch (Throwable t1) {
Log.error(t1);
}
}
} catch (Throwable t) {
Log.warn(t);
try {
service.doRollbackFirstPart();
} catch (Throwable t1) {
Log.error(t1);
}
}
}
Answer the question
In order to leave comments, you need to log in
Why not simplify and remove a bunch of unnecessary try?
public void doAndForget() {
boolean firstOk = false;
boolean secondOk = false;
try{
firstOk = service.doFirst();
if (firstOk){ //Если есть какая то жесткая зависимость исхода выполнения
secondOk = service.doSecond();
}
} catch(Чего угодно){
Log.warn("Status 1 task: "+ firstOk+"; 2nd task "+secondOk;
} finally {
//Проверить флаги и вызвать откаты в нужной последовательности
}
}
I am not a java developer - so I apologize in advance, but this language seems to have a finally block, which was also invented for organizing rollbacks. What for to you to intercept errors to me at all it is not clear.
And judging by the wording of the try/finally task , there should be 2 blocks, not 5 like yours.
And as for the questions themselves - do not forget that there are situations in which you still lose confidence that you will be able to do a rollback - just like OutOfMemoryError - and your actions can lead to unexpected results. The application must be controlled.
You can take a little functional approach and do, for example, like this
public interface Transaction {
void commit();
void rollback();
}
public static void tryTransaction(Transaction tr) {
try {
tr.commit();
} catch (Exception e) {
//log.error....
tr.rollback();
throw new CommitException(...);
}
}
tryTransaction(new Transaction() {
@Override
public void commit() {
//step1
//...
//step2
tryTransaction(new Transaction() {
...
});
}
@Override
public void rollback() {
}
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question