什么时候应该关闭EntityManagerFactory? [英] When should we close the EntityManagerFactory?

查看:321
本文介绍了什么时候应该关闭EntityManagerFactory?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对ORM很新颖。我刚刚开始阅读关于使用Hibernate的Java持久性API的书籍和文档。

我只是想知道,关闭EntityManagerFactory与jdbc数据库连接关闭类似吗?



我们应该在每隔坚持/更新/删除或不?如果我们不关闭它,数据库连接会保持打开状态吗?

解决方案


我只是想知道,关闭 EntityManagerFactory 与jdbc数据库连接关闭类似?

这不是完全正确,但关闭 EntityManagerFactory 会更接近销毁整个连接池。如果你想考虑JDBC连接,你应该考虑 EntityManager


每次持续/更新/删除后关闭它?


创建 EntityManagerFactory 是一项非常昂贵的操作,应该为应用程序的生命周期(您在应用程序结束时您关闭)。所以,不,你不应该为每个持久/更新/删除操作关闭它。
$ b $ < EntityManagerFactory 为所有人创建一次,您通常会得到 EntityManager per request,在请求结束时关闭( EntityManager 每个请求是多用户客户端/服务器应用程序最常见的模式)。


如果我们不关闭它,数据库连接是否会保持打开状态?


正如所暗示的,它是 code> EntityManager ,它实际上与一个数据库连接关联,并关闭 EntityManager 将实际释放JDBC连接(通常将其返回给池)。


I am pretty new on the ORM's. I just start to read books and documents about Java Persistence API with Hibernate.

I just wondered, closing EntityManagerFactory is similar with jdbc database connection closing?

Should we close it after every persist/update/delete or not? If we don't close it, will the database connection stay opened?

解决方案

I just wondered, closing EntityManagerFactory is similar with jdbc database connection closing?

This is not exactly true but closing an EntityManagerFactory would be closer to destroying a whole connection pool. If you want to think JDBC connection, you should think EntityManager.

Should we close it after every persist/update/delete or not?

Creating an EntityManagerFactory is a pretty expensive operation and should be done once for the lifetime of the application (you close it at the end of the application). So, no, you should not close it for each persist/update/delete operation.

The EntityManagerFactory is created once for all and you usually get an EntityManager per request, which is closed at the end of the request (EntityManager per request is the most common pattern for a multi-user client/server application).

If we don't close it, will the database connection stay opened?

As hinted, it's the EntityManager that is actually associated to a database connection and closing the EntityManager will actually release the JDBC connection (most often, return it to a pool).

这篇关于什么时候应该关闭EntityManagerFactory?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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