@Aspect を使用して、データベースの古い接続の問題に対する再試行ロジック (max_retries = 5) を実装しています。古い接続の問題に対する無制限の再試行を避けるために、再試行の最大回数は 5 (定数) です。
しかし、私が抱えている問題は、この @Aspect Java クラスで ThreadLocal がインクリメントされず、これがコード内で endlees ループを引き起こしていることです。これはもちろん、再試行の最大回数の後に再試行すべきではありませんが、その回数に達することはなく、抜け出すことはありませんwhile ループ。
@Aspect と ThreadLcal オブジェクトでこの問題が発生した場合はお知らせください。
コードを共有させていただきます。
private static ThreadLocal<Integer> retryCounter= new ThreadLocal<Integer>() {};
private static final String STALE_CONNECTION_EXCEPTION = "com.ibm.websphere.ce.cm.StaleConnectionException";
@Around("service")
public Object retryConnection(ProceedingJoinPoint pjp) throws Throwable {
if (staleConnectionException == null) {
return pjp.proceed();
}
Throwable exception = null;
retryCounter.set(new Integer(0));
while ( retryCounter.get() < MAX_TRIES) {
try {
return pjp.proceed();
}
catch (AppDataException he) {
exception = retry(he.getCause());
}
catch (NestedRuntimeException e) {
exception = retry(e);
}
}
if (exception != null) {
Logs.error("Stale connection exception occurred, no more retries left", this.getClass(), null);
logException(pjp, exception);
throw new AppDataException(exception);
}
return null;
}
private Throwable retry(Throwable e) throws Throwable {
if (e instanceof NestedRuntimeException && ((NestedRuntimeException)e).contains(staleConnectionException)) {
retryCounter.set(retryCounter.get()+1);
LogUtils.log("Stale connection exception occurred, retrying " + retryCounter.get() + " of " + MAX_TRIES, this.getClass());
return e;
}
else {
throw e;
}
}