具有自动重新连接的JDBC连接 [英] JDBC connection with auto reconnect

查看:132
本文介绍了具有自动重新连接的JDBC连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JDBC连接到数据库服务器. 该连接是通过无线网络进行的,有时可能会令人迷惑. 在失去连接的那一刻,我需要关闭并重新启动应用程序.

I am using JDBC to connect to a database server. The connection is over a wireless network and can be dodgy at times. At the moment when the connection is lost I need to close and restart the application.

有人有一些代码示例,可以在其中编写某种包装程序来自动重新连接并重新运行上一个查询吗?这样可以节省很多麻烦.

Does anyone have some examples of code where I could write some sort of wrapper to automatically reconnect and rerun the last query? This would save a lot of hassles.

我只是不确定应该/应该如何实施. 也许已经有可用的东西了?

I am just not sure how it should/could be implemented. Maybe there is already something available?

推荐答案

让连接池为您处理此问题,其中许多人可以验证连接. DBCP 也是如此,它具有一个testOnBorrow参数,该参数在使用每个连接之前强制进行完整性检查.此参数的默认值为true,只需要将validationQuery设置为非null字符串即可生效.因此,设置validationQuery即可开始!查看文档.

Let a connection pool handle this for you, many of them can validate a connection. So does DBCP which has a testOnBorrow parameter that forces a sanity check on every connection before it's used. The default value of this parameter is true, it just needs validationQuery to be set to a non-null string to have any effect. So set the validationQuery and there you go! Check out the documentation.

这篇关于具有自动重新连接的JDBC连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆