重新使用与数据库的连接 [英] Reusing the connection to the database

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

问题描述

到现在为止,每当我查询数据库时,我打开一个新的连接到数据库。如何实现属性,一旦我打开连接,我可以重复使用?

Until now, whenever I query the database I open a fresh connection to the database. How do I implement the property that once I open the connection I can reuse that?

完成后,请告诉我是否可以泄漏资源。

With this done, please tell me if I could leak the resources.

推荐答案

基本上你需要JDBC连接池,通常实现 DataSource 界面。请查看 dbcp c3p0 。有可能是容器/服务器已经提供了连接池的实现。

Basically you need JDBC connection pool, typically implementing DataSource interface. Have a look at dbcp and c3p0. Chances are you container/server already provides an implementation of connection pooling.

当您每次使用连接池实际上从池中取一个(或如果池是空的,则打开)。当关闭连接时,它实际上返回到池中。泄漏只有在你忘记后者时才会发生。 (或忘记关闭 ResultSet 语句 ...)

When you use a connection pool every time you open a connection you are actually taking one from the pool (or opening if pool is empty). When closing the connection, it is actually returned to the pool. The leak can only occur if you forget the latter. (or forget closing ResultSet, Statement...)

这篇关于重新使用与数据库的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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