[英]Remote database connection pooling from multiple servlets
This is a multipart question, so please bear with me. 这是一个多方面的问题,请耐心等待。
I'm developing a web based application with java servlets. 我正在使用Java Servlet开发基于Web的应用程序。 There will be multiple application servers to load balance the system.
将有多个应用程序服务器来负载均衡系统。 These servers access a central remote database (none of them are in a LAN) to carry out the requests.
这些服务器访问中央远程数据库(它们均不在LAN中)以执行请求。 One of the requirements of the system is to be able to dynamically add a new application server to the network and have it be able to connect to the database and start handling requests right away.
该系统的要求之一是能够动态地向网络添加新的应用程序服务器,并使它能够连接到数据库并立即开始处理请求。
My questions: 我的问题:
Thank you 谢谢
I don't know much about MySQL, so I won't answer the first part. 我对MySQL不太了解,所以我不会回答第一部分。
But yes, you should definitely use a connection pool. 但是,是的,您绝对应该使用连接池。 Connection setup time will be even worse for a distant database than a nearby one, so reusing connections is even more important.
远程数据库的连接建立时间将比附近数据库更差,因此重用连接更为重要。
Pools can be configured however you like, but are typically per-server, so they will be shared by all the servlets in a given server. 可以根据需要配置池,但是池通常是按服务器配置的,因此它们将由给定服务器中的所有servlet共享。 If you configure a maximum size to the pool, then the total number of connections at the database will be proportional to the number of servers.
如果您为池配置了最大大小,则数据库中的连接总数将与服务器数成正比。 This is something to be careful of - i've seen databases go down a few times because of inappropriately large pool sizes in large populations of clients.
这是要小心的事情-我已经看到数据库崩溃了几次,原因是在大量客户中池大小过大。
Yes, you should reuse a single connection for the duration of each request (probably). 是的,您应该在每个请求的持续时间内重用单个连接(可能)。 For starters, you will have to do this if you want to use a single transaction across the whole request, which you almost certainly do.
对于初学者,如果要在整个请求中使用单个事务,则必须这样做,几乎可以肯定。 Aside from that, getting and releasing connections is not free, so reusing one amortizes the cost across several operations.
除此之外,获取和释放连接不是免费的,因此重用连接会分摊多个操作的成本。
One caveat: holding on to a connection for the life of a request may increase the necessary pool size over grabbing and releasing for each operation. 一个警告:在请求的整个生命周期中保持连接可能会增加每个操作所需的池大小,而不是抓住和释放。 Usually, that's a good tradeoff.
通常,这是一个很好的权衡。 But if you are severely constrained by the number of connections, it might not be.
但是,如果您受到连接数量的严重限制,则可能不是这样。 It depends to some extent on how long your requests last, and how much database work they do, with shorter, busier requests making better use of a reused connection.
在某种程度上,它取决于您的请求持续多长时间以及它们完成多少数据库工作,而更短,更忙的请求可以更好地利用重用的连接。 If you're serving huge media streams which can be generated without reference to the database, it's possible your application doesn't fit this pattern.
如果您要提供庞大的媒体流,而这些媒体流可以在不参考数据库的情况下生成,则您的应用程序可能不适合这种模式。
A final point: if your database is a long way away, you will benefit more than usual from caching. 最后一点:如果您的数据库还有很长的路要走,那么从缓存中获得的好处将比平时更多。 Look hard at whether there is data you can usefully cache, or even store in a local database, at each server, to avoid trips to the central database.
仔细检查是否有数据可以有效地缓存,甚至可以存储在每台服务器的本地数据库中,以避免访问中央数据库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.