简体   繁体   English

EntityManager和休眠会话

[英]EntityManager and hibernate Session

i am using hibernate JPA in my SpringMVC application. 我在SpringMVC应用程序中使用休眠JPA。 i am initializing it with this piece of code 我正在用这段代码对其进行初始化

@PersistenceContext
    protected EntityManager entityManager;

Now this entityManager is going to live for complete life of my application. 现在,这个entityManager可以继续使用我的应用程序。 hibernate's implementation of EntityManager is wraping a hibernate session. hibernate的EntityManager实现包装了一个hibernate会话。 you can unwrap it if you wish to. 您可以根据需要解开包装。 When we are working with pure hibernate without JPA. 当我们使用没有JPA的纯休眠模式时。 we open the session do our work and close the session. 我们打开会议以完成工作,然后关闭会议。 If EntityManager is using hibernate's session behind the scenes when it is opened and when it is closed. 如果EntityManager在打开和关闭时在后台使用了hibernate的会话。 if it keeps on a single session for whole life of application will not it be creating problems as entities are not automatically evicted? 如果在整个应用程序生命周期中只进行一次会话,是否不会因为实体没有自动驱逐而造成问题? it may lead to out of memory error? 它可能导致内存不足错误?

I will try to answer every question (based on my experience and expectations), but I am pretty sure it really depends on whether you work with JTA transactions or with UserTransactions (aka resource-local transactions): 我将尝试回答每个问题(基于我的经验和期望),但是我敢肯定,这实际上取决于您是使用JTA事务还是使用UserTransactions(又称为资源本地事务):

If EntityManager is using hibernate's session behind the scenes when it is opened and when it is closed? 如果EntityManager在打开和关闭时在后台使用了hibernate的会话?

For every instantion of an EntityManager, a new hibernate session is opened. 对于EntityManager的每个实例,都会打开一个新的休眠会话。 If you work with JTA transactions, the entityManager is closed when the transaction is closed (when your first service bean method returns). 如果您使用JTA事务,则在事务关闭时(第一个服务bean方法返回时),entityManager也将关闭。

If it keeps on a single session for whole life of application will not it be creating problems as entities are not automatically evicted? 如果它在整个应用程序生命周期中都保持单一会话状态,那么会不会因为没有自动驱逐实体而造成问题? it may lead to out of memory error? 它可能导致内存不足错误?

It does not keep a single hibernate session. 它不保留单个休眠会话。 If you configured so, it will has a pool of reusable connection, but for every new created bean it will generate a new entity manager with a new Hibernate session behind it. 如果进行了配置,它将有一个可重用的连接池,但是对于每个新创建的bean,它将生成一个新的实体管理器,并在其后面有一个新的Hibernate会话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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