在entitymanager上使用unwrap方法获得本地hibernate会话后,我是否必须关闭两者? [英] After using the unwrap method on entitymanager to get the native hibernate session do I have to close both?

查看:4079
本文介绍了在entitymanager上使用unwrap方法获得本地hibernate会话后,我是否必须关闭两者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



pre $ this.entityManager = AppFactory.instance()。getEntityManagerFactory()。createEntityManager ();
this.hibernateSession = entityManager.unwrap(Session.class);
try {
//使用entityManager和hibernateSession
}做一些查询finally {
this.entityManager.close();
}

但是我好像有一处连接泄漏。我想知道是否应该关闭entityManager和hibernateSession。有没有其他人处理过这种情况?

解决方案

您不必关闭Session和EntityManger,在hibernate中实际上是hibernate Session。调用unwarp会传递给你底层Session。所以关闭其中一个是好的。

关于连接泄漏,看看我的答案以下问题,也许这是同一个问题。

I have code that looks like this.

 this.entityManager = AppFactory.instance().getEntityManagerFactory().createEntityManager();
 this.hibernateSession = entityManager.unwrap(Session.class);
 try{
 //do some queries using both entityManager and hibernateSession
 }finally{
 this.entityManager.close();
 }

But I seem to have a connection leak somewhere. I'm wondering if I am supposed to close both entityManager and hibernateSession. Has anybody else worked with this type of situation?

解决方案

You do not have to close both Session and EntityManger, under the hood EntityManger in hibernate is actually hibernate Session. Calling unwarp will pass you the underlying Session. So closing one of them is fine.
Regarding the connection leak, look at my answer to the following question, maybe it’s the same issue.

这篇关于在entitymanager上使用unwrap方法获得本地hibernate会话后,我是否必须关闭两者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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