从JNDI查找获得的EntityManager已经关闭

时间:2022-09-06 20:09:49

For (Glassfish v2.1), two RuntimeExceptions from two separate requests from a session bean:

对于(Glassfish v2.1),来自会话bean的两个单独请求中的两个RuntimeExceptions:

  • "org.hibernate.SessionException: Session is closed!"
  • “org.hibernate.SessionException:会话已关闭!”

org.hibernate.SessionException: Session is closed! at org.hibernate.impl.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:72) at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1138) at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102) at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:67) [wrapped] javax.persistence.PersistenceException: org.hibernate.SessionException: Session is closed! at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:614) at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:76)

org.hibernate.SessionException:会话已关闭! org.hibernate.impl.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:72)org.hibernate.impl.SiionImpl.list(SessionImpl.java:1138)atg.hibernate.impl.QueryImpl.list(QueryImpl.java:102) )org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:67)[wrapped] javax.persistence.PersistenceException:org.hibernate.SessionException:会话关闭! org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:614)org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:76)

  • "java.lang.IllegalStateException: EntityManager is closed"
  • “java.lang.IllegalStateException:EntityManager已关闭”

java.lang.IllegalStateException: EntityManager is closed at org.hibernate.ejb.EntityManagerImpl.close(EntityManagerImpl.java:97) at com.sun.enterprise.util.QueryWrapper.clearDelegates(QueryWrapper.java:460) at com.sun.enterprise.util.QueryWrapper.getResultList(QueryWrapper.java:198)

java.lang.IllegalStateException:EntityManager在com.sun上的com.sun.enterprise.util.QueryWrapper.clearDelegates(QueryWrapper.java:460)的org.hibernate.ejb.EntityManagerImpl.close(EntityManagerImpl.java:97)处关闭。 enterprise.util.QueryWrapper.getResultList(QueryWrapper.java:198)

Both of these EntityManagers were obtained via JNDI lookup (java:comp:/env/TargetSitePersistenceContext) Using JTA (transaction-type attribute is not defined in persistence.xml). & SQL Server 2008 w/ sqljdbc4.jar

这两个EntityManage都是通过JNDI查找获得的(java:comp:/ env / TargetSitePersistenceContext)使用JTA(事务类型属性未在persistence.xml中定义)。 &SQL Server 2008 w / sqljdbc4.jar

The code just does the ff:

代码只是ff:

query = entityManager.createQuery();
query.getResultList();

And that's it. If I'm not mistaken, I believe the app container will handle open/commit/rollback/close, so we shouldn't have any entityManager.close().

就是这样。如果我没有弄错,我相信app容器将处理open / commit / rollback / close,所以我们不应该有任何entityManager.close()。

What might have caused those two runtime exceptions?

什么可能导致这两个运行时异常?

When does GF actually open/close an EntityManager?

GF何时实际打开/关闭EntityManager?

Is there any difference between:

之间有什么区别:

  • an EntityManager obtained via JNDI lookup
  • 通过JNDI查找获得的EntityManager

  • via @PersistenceContext Injection? (So far not issues with this style)
  • 通过@PersistenceContext注入? (到目前为止还没有这种风格的问题)

2 个解决方案

#1


0  

All things being equal, a @PersistenceContext injection and a JNDI lookup should return the same EntityManager. So it might be a bug of GlassFish and you might want to reach for them. But make sure to give all the context like type of session bean used for injection, transaction or not etc etc.

在所有条件相同的情况下,@ PersistenceContext注入和JNDI查找应该返回相同的EntityManager。所以它可能是GlassFish的一个错误,你可能想要达到它们。但是请确保提供所有上下文,例如用于注入,事务或非等的会话bean类型等。

#2


0  

Mark your bean with annotation @TransactionAttribute

使用注释@TransactionAttribute标记您的bean

@Stateless
@TransactionAttribute(TransactionAttributeType.MANDATORY)
public class Repo implements IRepo
{  

container managed transaction is regulated by this parameter

容器管理的事务由此参数调节

#1


0  

All things being equal, a @PersistenceContext injection and a JNDI lookup should return the same EntityManager. So it might be a bug of GlassFish and you might want to reach for them. But make sure to give all the context like type of session bean used for injection, transaction or not etc etc.

在所有条件相同的情况下,@ PersistenceContext注入和JNDI查找应该返回相同的EntityManager。所以它可能是GlassFish的一个错误,你可能想要达到它们。但是请确保提供所有上下文,例如用于注入,事务或非等的会话bean类型等。

#2


0  

Mark your bean with annotation @TransactionAttribute

使用注释@TransactionAttribute标记您的bean

@Stateless
@TransactionAttribute(TransactionAttributeType.MANDATORY)
public class Repo implements IRepo
{  

container managed transaction is regulated by this parameter

容器管理的事务由此参数调节