import org.slf4j.MDC;
MDC.put(ID1, id1);
MDC.put(ID2, id2);
MDC.put(ID3, id3);
try {
return joinPoint.proceed();
} finally {
MDC.remove(ID1);
MDC.remove(ID2);
MDC.remove(ID3);
}
- Assuming there is a code as above, Exceptions can also occur for remove, put()
- If an exception occurs in the deletion process for id1, you cannot delete irrelevant id2,id3. To prevent this problem, do I have to take an additional process of taking and processing an exception for that part of the above code? Like the attached code The code below is only processed for ID1, but you need a try-catch statement every time you add it
MDC.put(ID1, id1);
MDC.put(ID2, id2);
MDC.put(ID3, id3);
try {
return joinPoint.proceed();
} finally {
try{
MDC.remove(ID1);
} finally {
MDC.remove(ID2);
MDC.remove(ID3);
}
}
- There's no compiler exception
If there's anything lacking in the question, please point it out. I'll reflect it and upload it again
I assume that you are actually asking about the correct way to do the removals.
If we assume that any of the
putorremovecalls can throw an exception, AND that not tidying up the MDC is unacceptable, then the correct way to write this would be:You can simplify this by using
putCloseableinstead ofputas follows:The
putCloseablevariant returns an adapter that implementsCloseablewhere theclose()method performs the requiredremove.On the other hand, if we treat the MDC javadocs as defining the behavior, then the only case where
removeis documented to throw an exception is when the key isnull. You should be able to guarantee thatID1,ID2andID3are notnull. If you can, the above code is unnecessary; i.e. your original version with a singlefinallyshould be fine.