重试逻辑直到数据库出现

发布于 2024-09-11 09:58:22 字数 72 浏览 6 评论 0原文

通过我的java代码,我使用连接池连接到多个数据库。如果我的数据库出现故障,我需要处理重试逻辑来获取连接,直到它返回一个连接对象。

Through my java code i m connecting to multiple databases using connection pooling.if my database goes down i need handle the retry logic to get connection until its return a connection object.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

命比纸薄 2024-09-18 09:58:22

如果您的数据库连接抛出某种异常,那么您可以休眠一会儿,然后再次重试该操作。

在下面的示例中,worker 是一个执行一些工作的对象,例如连接到数据库等。它非常通用,因此您可以重试任何类型的操作,例如从文件中读取等。

请注意,捕获 Throwable< /code> 可能不一定是个好主意。

    boolean success = false;
    int i = 0;
    long delay = retryDelay;

    LOGGER.info("Starting operation");

    /*
     * Loop until you cannot retry anymore or the operation completed successfully
     * The catch block has a nested try catch to ensure that nothing goes wrong
     * while trying to sleep
     * 
     * In case of failure the last retry exception is propagated up to the calling
     * class.
     */
    while (i++ < retryMax && !success)
    {
        try
        {
            worker.work();
            success = true;
        }
        catch (Throwable t)
        {
            try
            {
                LOGGER.warn("Caught throwable", t);

                if (i == retryMax) 
                {
                    LOGGER.warn("Retry maximum reached, propagating error");
                    throw t;
                }

                if (retryPolicy == RetryPolicy.ESCALATING) 
                {
                    delay *= 2;
                }

                LOGGER.info("Sleeping for " + delay + " milliseconds");

                Thread.sleep(delay);
            }
            catch (Throwable tt)
            {
                /*
                 * Quick check to see if the maximum has been hit, so we don't log twice
                 * 
                 * t is the original error, and tt is the error we got while retrying
                 * tt would most likely be a InterruptedException or something
                 */
                if (i == retryMax) 
                {
                    throw t;
                }

                LOGGER.warn("Error while retrying, propagating original error up", tt);

                throw t;
            }
        }

    } // end retry loop

If your db connection throws some sort of an Exception then you can just sleep for a bit and retry the operation again.

In the example below worker is an object that does some work such as connecting to a db, etc. It's pretty generic so you can retry any sort of an operation such as reading from a file, etc.

Note that catching Throwable might not necessarily be a great idea.

    boolean success = false;
    int i = 0;
    long delay = retryDelay;

    LOGGER.info("Starting operation");

    /*
     * Loop until you cannot retry anymore or the operation completed successfully
     * The catch block has a nested try catch to ensure that nothing goes wrong
     * while trying to sleep
     * 
     * In case of failure the last retry exception is propagated up to the calling
     * class.
     */
    while (i++ < retryMax && !success)
    {
        try
        {
            worker.work();
            success = true;
        }
        catch (Throwable t)
        {
            try
            {
                LOGGER.warn("Caught throwable", t);

                if (i == retryMax) 
                {
                    LOGGER.warn("Retry maximum reached, propagating error");
                    throw t;
                }

                if (retryPolicy == RetryPolicy.ESCALATING) 
                {
                    delay *= 2;
                }

                LOGGER.info("Sleeping for " + delay + " milliseconds");

                Thread.sleep(delay);
            }
            catch (Throwable tt)
            {
                /*
                 * Quick check to see if the maximum has been hit, so we don't log twice
                 * 
                 * t is the original error, and tt is the error we got while retrying
                 * tt would most likely be a InterruptedException or something
                 */
                if (i == retryMax) 
                {
                    throw t;
                }

                LOGGER.warn("Error while retrying, propagating original error up", tt);

                throw t;
            }
        }

    } // end retry loop
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文