休眠:什么是连接池,为什么默认的不适合生产? [英] Hibernate: What is the connection pool and why is the default one unsuitable for production?

查看:107
本文介绍了休眠:什么是连接池,为什么默认的不适合生产?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Hibernate非常不熟悉,刚刚开始研究一个将它与MySQL数据库结合使用的Web应用程序。我注意到社区文档教程陈述:


内置的Hibernate连接池决不适合生产使用。它缺乏在任何像样的连接池上发现的几个功能。

有人可以详细说明吗?究竟错过了什么,人们对默认有什么问题?在Google上,我发现了一个网站这里,但它并没有真正解释问题,只是你应该用什么来代替。

解决方案


什么是连接池,为什么默认的不适合生产?有人可以详细说明这一点吗?

连接池是一种打开/准备/关闭连接的技术。连接池机制是一个软件(组件),您可以将其管理连接的功能委托给它。您的应用程序只会请求连接,使用它并将其传回池中。该组件负责打开N个连接,并在应用程序请求时让它们准备就绪。如果连接失效,池化机制将关闭它并重新打开一个连接。这代表了更好的连接用法,因为您不需要等待在代码实际执行期间建立连接,也不必担心过时的连接。



Hibernate并没有真正提供任何真正的连接池机制。它提供了一个非常基本的内部连接管理器。原因很简单:几乎(如果不是全部的话)应用程序服务器(如JBoss AS)和Servlet容器(如Tomcat)默认提供连接池机制。因此,您的应用程序不必担心有关它的详细信息。它只是要求AS进行连接。



在我看来,只有两种情况需要担心连接池:


  1. 您正在处理独立应用程序(不在容器中运行)。

  2. 您在连接池方面非常专业,并且没有任何现有的适合您的需求。 / li>

但是根据我的经验,大多数使用外部连接池的人会这样做,因为缺乏关于连接池的知识, 缺乏关于他们的容器的知识。

I'm very unfamiliar with Hibernate and have just started working on a web app that uses it with a MySQL database. I notice that the community documentation tutorial states:

The built-in Hibernate connection pool is in no way intended for production use. It lacks several features found on any decent connection pool.

Can someone elaborate on this? What exactly is it missing and what are problems people have with the 'default' one? On googling I found a website here but it doesn't really explain the problems, just what you should be using instead.

解决方案

What is the connection pool and why is the default one unsuitable for production? Can someone elaborate on this?

Connection pooling is a technique to open/prepare/close connections. A connection pooling mechanism is a piece of software (component), to which you delegate the function of managing connections. Your application would just ask for a connection, use it, and deliver it back to the pool. The component is responsible for opening N connections and leave them ready for when your application asks. If a connection is stale, the pooling mechanism would then close it and reopen a new one. This represents a better usage of connections, as you don't need to wait for the connection to be established during the actual execution of your code and you don't have to worry about stale connections.

Hibernate doesn't really ship any real connection pooling mechanism. It provides an internal connection manager, which is very rudimentary. The reason is simple: almost (if not all) Application Servers (like JBoss AS) and Servlet Containers (like Tomcat) provides a connection pooling mechanism by default. Thus, your application don't have to worry about the details about it. It just asks the AS for a connection.

In my opinion, there are only two cases where you need to worry about connection pooling:

  1. You are dealing with a standalone application (which doesn't run inside a container)
  2. You are really expert in connection pooling and none of the existing suits your needs.

But in my experience, most people that uses an "external" connection pooling do so for lack of knowledge about connection pooling and lack of knowledge about their container.

这篇关于休眠:什么是连接池,为什么默认的不适合生产?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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