Does the object manager create a database connection?

In my project, I forgot to close the object manager for each operation. After some time, I got an exception due to excessive connections to the mysql server. Does this mean that every organization manager establishes a connection? What happens when we forget to close the connection? I used only one factory entity manager.

+7
source share
2 answers

Assuming you are using an application-driven object manager, you are responsible for initializing and closing the object manager. On the other hand, if you rely on a container to inject an entity manager into your beans session (or any managed classes), then the container is responsible for keeping the object manager closed.

Typically, the object manager is not responsible for creating database connections. Instead, it uses the connection pool, which is defined in persistence.xml . This is true for both JTA entity managers and local resource managers; JTA entity managers rely on the JTA data source provided by the application server environment, while local resource administrators create and manage their pools.

If you do not close manager entities, and if you continue to create new instances from them, then you can exhaust the connections in the JTA data source (for JTA entity managers) or reach the server-specific limit on the connection client (for both JTA and managers local resources). Each database instance will 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 disconnect the additional connections. If you open entity manager instances that request additional connections from the pool (for JTA entity managers) or create new pools (for local resource managers), then it is very likely that the pool itself might be exhausted or too many connections would be open.

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

In addition, it would be wise to look at the use of a well-configured and adequate connection pool size for each instance of the entity manager if you are using resource-local entity managers for any reason. If you are using the JTA entity manager, consider using entity managers in containers and a JTA data source that is supported by a well-configured and adequate connection pool size.

+11
source

In any case, if you open the EntityManager openly (get it from EMFactory), you must close it. Whether the connection is required (which seems to be your case) or not depends on the settings that are associated with the JPA provider. In our project, EM opens a connection for each request and immediately returns it (closes). But there are other options: save the connection or the connection that follows the transaction life cycle.

An example is the settings for OpenJPA . I would say that if EM opens a connection for a request / transaction, you will not run into a problem. Your EM seems to be accepting the connection and holding it.

In any case, you should close EM if you created it. Best practice is to do this with the same method, if possible, so it’s easy to review the correctness and do it at the end of the block.

+3
source

All Articles