连接池:是否合适
我正在构建一个基于 Java 的 Web 应用程序(主要是使用 Tomcat 部署的 JSP)。用户数量永远不会超过~30人。它是一个工作日志,因此用户将不断更新/访问数据库(SQL Server)。 Web 应用程序中有许多方法需要连接到数据库。
每次需要一个连接时,我都会打开一个新连接(我也会适当地关闭它),但这似乎需要打开/关闭大量连接。连接池适用于这种情况吗?我试图理解池的作用,但我很困惑;每个用户都有一个连接池吗?
如果我偏离了轨道(我怀疑我是这样),那么这个问题有更好的解决方案吗?难道还有问题吗?
谢谢!
I'm building a Java based web app (primarily JSPs deployed with Tomcat). The number of users will never be more than ~30 people. It is a work log, so the users will constantly be updating/accessing the database (SQL Server). There are many methods in the web app that require a connection to the database.
I open a new connection each time one is required (I also close it appropriately), but this seems like a lot of opening/closing of connections. Does connection pooling apply to this situation? I'm trying to understand the role of the pool, but I'm confused; would there be a connection pool for each user?
If I'm way off track (which I have a suspicion I am), then is there a better solution to this problem? Is there even a problem?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,我将池的大小设置为 30 个连接并让它管理它们。您将通过这种方式分摊所有请求打开连接的成本。
许多用户可以访问一个池来获取连接;每个请求一个连接。
Yes, I'd size the pool for 30 connections and let it manage them. You'll amortize the cost of opening connections over all requests that way.
There's one pool that many users would access to get connections; one connection per request.
连接池适用于应用程序(而不是针对每个用户)。连接池的概念是尽可能地重用打开的连接,并在绝对需要时打开新的连接。就 CPU 周期和内存而言,打开数据库连接是一项昂贵的操作。这就是为什么需要连接池。对于 30 个用户,我建议使用连接池。
您可以将池大小设置为 15 到 30 个连接之间的任意位置。
看看 http://commons.apache.org/dbcp/
The connection pool is for the application (not per user). The concept of connection pool is to reuse open connections as much as possible and open a new one when it is absolutely needed. Opening a connection to a database is an expensive operation in terms of both cpu cycles and memory. Thats why a connection pool is needed. For 30 users, i would recommend using a connection pool.
You can size the pool anywhere between 15 to 30 connections in the pool.
take a look at http://commons.apache.org/dbcp/
您当然可以池化与数据库的连接。通常,您会为每个数据库使用一个池(尽管可能有原因需要使用更多池)。
你问是否有问题是对的。连接池将减少必须协商的新连接数量,因此它将减少服务请求所需的时间,并减少服务器上的负载。它还会减少使用的套接字数量,这(对于较大的应用程序)可能是系统性能的一个因素。
但是:您是否有想要解决的性能问题?响应时间可以接受吗?负载是否可以接受?平衡您在性能方面获得的收益与开发成本。存在预构建的连接池,因此集成连接池可能很容易。但它不是免费的,优化通常应该针对特定目标来完成,而不是“因为我应该”。
You certainly could pool connections to the database. Generally you'd use one pool per DB (though there could be reasons that you'd have more).
You're right to ask whether there's even a problem. Connection pooling is going to reduce the number of new connections that have to be negotiated, so it will reduce the time it takes to service a request and also reduce load on the servers. Also it will reduce the number of sockets used, which (for larger applications) can be a factor in system performance.
However: do you have a performance problem that you're trying to solve? Are response times acceptable? Is load acceptable? Balance what you'd gain in perf versus development cost. Pre-built connection pools exist, so it's likely easy to integrate one. But it's not free and optimization should generally be done with specific goals, not "because I should".
这里的重点不是用户数量,而是需要打开连接的请求数量。
如果你有,
你仍然会从连接池中受益,因为
c.close()
并没有真正关闭连接,而只是将其放回池中。The point here is less the number of users, but the number for requests that require opening a connection.
If you have
You will still benefit from the connection pool, as the
c.close()
is not really closing the connection, but just putting it back into the pool.