简体   繁体   English

来自多个servlet的远程数据库连接池

[英]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: 我的问题:

  • To setup a remote MySQL database server, I need to modify some configuration files to allow external connections. 要设置远程MySQL数据库服务器,我需要修改一些配置文件以允许外部连接。 Which files are these and how can I modify them on the fly for new added servers whose IP address is not known until they are actually started up? 这些是哪些文件,如何为新添加的服务器动态修改它们,这些服务器的IP地址在实际启动之前是未知的?
  • For remote database access, should I be using connection pooling? 对于远程数据库访问,我应该使用连接池吗? Will the connection pooling be done individually for each servlet? 是否将为每个servlet单独完成连接池? How will the pool scale for newly added servers (more requests)? 池将如何扩展新添加的服务器(更多请求)?
  • Each request to an application server goes through multiple separate database calls (example: finds and updates). 对应用程序服务器的每个请求都要经过多个单独的数据库调用(例如:查找和更新)。 Should I be using the same connection across these operations or releasing it every time? 我应该在这些操作中使用相同的连接还是每次都释放它?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM