创建数据库连接池 [英] Creating a database connection pool

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

问题描述

需要有关创建到数据库的连接池(不考虑数据库)的信息,以及它们的效率如何?

Need information on creating a connection pool to the database (irrespective of the database) , and how efficient they are? What are the conditions where they can enhance performance.

如何显式创建?

推荐答案

您的问题有点含糊:

您要连接池实现如果是这样,这是一个很好的起点: http:// java。 sun.com/developer/onlineTraining/Programming/JDCBook/conpool.html 对于生产环境而言,这是非常不鼓励的。更好地使用现有的,经过彻底测试的连接池API,如 DBCP C3P0

Do you want to homegrow a connection pool implementation? If so, this is a nice starting point: http://java.sun.com/developer/onlineTraining/Programming/JDCBook/conpool.html But this is highly discouraged for production environments. Better use an existing and thoroughly tested connection pooling API, like DBCP or C3P0.

或者你想知道如何使用连接池?如果是这样,答案取决于您使用的连接池API。

Or do you want to know how to use a connection pool? If so, the answer depends on the connection pooling API you're using. It's fortunately usually available at the website of the API in question.

或者,您是否想知道何时/为什么要使用连接池?如果是这样,如果您有一个长期的应用程序(例如Web应用程序),并且您需要频繁地连接数据库,它肯定会增强连接性能。正常的JDBC实践是:获取关闭连接语句 ResultSet 最短可能范围(即在非常相同的方法块内)。因为连接相当昂贵,可能需要200毫秒的时间甚至更多,使用连接池要快得多。它根据需要提供连接,并关心实际关闭连接。这并不意味着你可能改变你写JDBC的方式,你仍然需要获取并关闭它们在可能的范围。你唯一需要改变的是你获取连接的方式。例如。更改

Or do you want to know when/why to use a connection pool? If so, it will surely enhance connecting performance if you have a long-living application (e.g. a webapplication) and you need to connect the database more than often. The normal JDBC practice is namely: acquire and close the Connection, Statement and ResultSet in the shortest possible scope (i.e. inside the very same method block). Because connecting is fairly expensive and can take up to 200ms of time or even more, using a connection pool is much faster. It gives connections on demand and takes care about actually closing the connection. That does however not mean that you may change the way you write JDBC, you still need to acquire and close them in the shorest possible scope. The only thing you need to change is the way you acquire the connection. E.g. change from

connection = driverManager.getConnection();

connection = connectionPool.getConnection();

只要您的JDBC代码写得很好,就不需要做任何更改。

No more changes are needed as long as your JDBC code is well-written.

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

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