0

@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;
    }
}
4

2 に答える 2

1

コメントで述べたように、スレッドローカルを使用している理由はわかりません...しかし、そうであれば、無限ループを引き起こしている可能性があるのは、この側面の再帰的な使用です。デバッガーで実行するか、プロファイルを作成して、ネストされた方法で同じアスペクトにヒットしているかどうかを確認します。

于 2013-05-29T20:24:07.457 に答える
0

正直なところ、あなたのコードを見ると、これをまったく行わないほうがよいと思いますが、接続プールで接続テストを構成するだけです (接続プールを使用していると仮定します): http://pic.dhe.ibm. com/infocenter/wasinfo/v6r1/index.jsp?topic=/com.ibm.websphere.nd.multiplatform.doc/info/ae/ae/tdat_pretestconn.html

于 2013-05-29T20:02:18.803 に答える