实体管理器是否创建与数据库的连接? [英] Does an entity manager create a connection to the database?

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

问题描述

在我的项目中,我忘记为每个操作关闭实体管理器。一段时间后,由于与mysql服务器的连接过多,出现异常。这是否意味着每个实体管理器都建立了连接?当我们忘记关闭连接时会发生什么?我只使用了一个实体管理器工厂。

In my project, I forgot to close the entity manager for each operation. After some time, I got exception due to excessive connections to mysql server. Does this mean that each entity manager establish the connection? What will happen when we forget to close the connection? I have used only one entity manager factory.

推荐答案

假设您使用的是由应用程序管理的实体管理器,那么您负责用于初始化和关闭实体管理器。另一方面,如果您依靠容器将实体管理器注入会话Bean(或任何托管类)中,则容器负责确保实体管理器已关闭。

Assuming that you are using an application-managed entity manager, then you are responsible for initializing and closing the entity manager. One the other hand, if you are relying on the container to inject the entity manager into your session beans (or any managed classes), then the container is responsible for ensuring that the entity manager is closed.

通常,实体管理器不负责创建与数据库的连接。相反,它将使用在 persistence.xml 中定义的连接池。 JTA实体管理器和资源本地实体管理器均是如此。 JTA实体管理器依赖于应用程序服务器环境提供的JTA数据源,而资源本地实体管理器创建和管理自己的池。

Usually, the entity manager is not responsible for creating connections to the database. It would instead use a connection pool, that is defined in persistence.xml. This is true of both JTA entity managers and resource-local entity managers; JTA entity managers rely on a JTA datasource provided by the application server environment, while resource-local entity managers create and manage their own pools.

如果不关闭实体管理器,如果您继续创建它们的新实例,则有可能耗尽JTA数据源中的连接(对于JTA实体管理器)或达到服务器定义的客户端连接限制(对于JTA和资源本地实体)经理)。每个数据库实例将配置为接受不超过一定数量的连接。如果所有客户端建立的连接数超过此限制,则服务器将简单地删除其他连接。如果您打开实体管理器实例以请求从池中获取其他连接(对于JTA实体管理器),或者创建新的池(对于资源本地实体管理器),则很有可能池本身已被耗尽,或者也是如此许多连接将被打开。

If you do not close entity managers, and if you continue to create new instances of them, then it is possible for you to exhaust connections in the JTA datasource (for JTA entity managers) or hit a server-defined limit on client connections (for both JTA and resource-local entity managers). Each database instance would be configured to accept no more than a certain number of connections. If the number of connections established by all clients, exceeds this limit, the server will simply drop addition connections. If you open entity manager instances that request for additional connections from a pool (for JTA entity managers), or create new pools (for resource-local entity managers), then it is very much likely that the pool itself might be exhausted, or too many connections would have been opened.

由于您无法直接关闭连接,甚至无法从应用程序中调整连接池的大小,因此很明显,您必须关闭实体管理器当您不再需要它们时;这样会自动释放为实体管理器建立的连接。

Since you cannot close the connections directly, or even resize the connection pools from your application, it is quite obvious that you must close entity manager instances when you no longer need them; this will automatically release the connections that were established for the entity manager.

此外,明智的做法是为每个应用程序使用经过调整的适当大小的连接池实体管理器实例,以防万一您使用资源本地实体管理器。如果您使用的是JTA实体管理器,请考虑使用容器注入的实体管理器和一个JTA数据源,该数据源由经过适当调整和足够大小的连接池支持。

Also, it would be wise to look at using a well-tuned and adequately sized connection pool for every entity manager instance, in case you are using resource-local entity managers for a reason. If you are using a JTA entity manager, consider using container-injected entity managers and a JTA datasource that is backed by a well-tuned and adequately sized connection pool.

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

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