jdbc中管理丢失连接的标准方法是什么? [英] What the standard way in jdbc to manage lost connection?

查看:112
本文介绍了jdbc中管理丢失连接的标准方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有时可能会失去与MySQL数据库的连接. 我认为,好的解决方案是安排一些计时器,以便在一段时间后尝试重新检查. 可以做得更好吗?可能是尝试连接到数据库的单独线程?还是存在标准做法? 谢谢.

My application sometimes can lost connection to MySQL database. I think that good solution will be schedule some timer to try to recconect after some time. How better it can be done? May be separate thread that try to connecto to db? Or exist the stardard practices ? Thanks.

推荐答案

JDBC是开始构建Java数据库应用程序的好方法,但是管理对象映射和连接/事务可以非常迅速地导致大量样板并重写已经被许多程序员多次编写的逻辑.

JDBC is a great way to start building a java database application, but managing object mappings and connections / transactions can very rapidly lead to alot of boiler plate and rewriting of logic which has already been written many times by many programmers.

除非您有高吞吐量的应用程序,否则应该正常地断开/关闭连接,在这种情况下,您可能会保持几个连接处于活动状态(这称为连接池).

It is expected that you should lose/close a connection normally, unless you have a high throughput application, in which case you might keep several connections alive (this is known as connection pooling).

基本上有3种高级"方法可以维持有效的联系和交易:

1)最简单的解决方案是检查何时重新使用连接以确保其有效,或者每次都重新打开它.

1) The simplest solution is to check when you are reusing a connection to make sure it is valid, or reopen it every time.

2)一个更复杂的解决方案是使用连接池机制,例如apache http://commons .apache.org/dbcp/ dbcp库.

2) A more sophisticated solution is to use a connection pooling mechanism, such as the apache http://commons.apache.org/dbcp/ dbcp library.

3)最后,我认为,最可维护的解决方案是使用ibatis/hibernate 这样的JDBC框架,该框架将为您提供用于管理对象关系映射/事务/的简单声明式接口.数据库状态----同时还为您透明地维护连接逻辑.

3) Finally, in my opinion, the most maintainable solution is to use a JDBC framework like ibatis/hibernate which will , providing you a simple , declarative interface for managing object relational mapping / transactions / database state ---- while also transparently maintaining connection logic for you.

ALSO:如果对象关系映射不是您的事,那么您可以使用apache之类的DBUtils之类的框架来管理查询和连接,而不会影响繁重的数据映射.

ALSO : If object relational mapping is not your thing, then you can use a framework such as DBUtils by apache to manage querying and connections, without the heavy weight data mapping stuff getting in the way.

这篇关于jdbc中管理丢失连接的标准方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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