-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: OpenSessionInViewFilter when using entityMan.getDelegate?
PostPosted: Sat Mar 24, 2007 1:25 pm 
Newbie

Joined: Sat Mar 24, 2007 1:10 pm
Posts: 5
hi all

I'm using the JPA-Extension of Hibernate. But since criteria queries do not exist yet in JPA, I'm using entityManager.getDelegate() in my DAOs to get a Hibernate-Session when needed.

This runs fine in my DAO-Tests, but as soon as I load data in the web-view, I get a org.hibernate.SessionException: Session is closed!

I have already added Spring's OpenEntityManagerInViewFilter in web.xml. Do I need to add the OpenSessionInViewFilter too? And if so, how can I tell the filter where to search my SessionFactory (I have an entityManagerFactory)?

Can anybody help me? Thanks
Stefan


Hibernate version:
3.2.1.ga

Full stack trace of any exception that occurs:
Caused by: org.hibernate.SessionException: Session is closed!
at org.hibernate.impl.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:49)
at org.hibernate.impl.SessionImpl.createCriteria(SessionImpl.java:1508)
at ch.berndeutsch.kamus.dao.jpa.GenericDaoJpa.findByCriteria(GenericDaoJpa.java:96)
at ch.berndeutsch.kamus.dao.jpa.GenericDaoJpa.findAll(GenericDaoJpa.java:59)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:280)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:187)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:154)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:78)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:176)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:210)
at $Proxy21.findAll(Unknown Source)
at ch.berndeutsch.kamus.service.spring.FrontendFacadeSpring.getEntries(FrontendFacadeSpring.java:58)
at ch.berndeutsch.kamus.web.beans.SearchBean.doSearch(SearchBean.java:49)
at ch.berndeutsch.kamus.web.beans.SearchBean.searchStringChangeListener(SearchBean.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.sun.el.parser.AstValue.invoke(AstValue.java:157)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:283)
at com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)
at javax.faces.event.MethodExpressionValueChangeListener.processValueChange(MethodExpressionValueChangeListener.java:80)


Name and version of the database you are using:
hsqldb 1.8.0.7


Top
 Profile  
 
 Post subject: update on this issue
PostPosted: Sun Mar 25, 2007 9:39 am 
Newbie

Joined: Sat Mar 24, 2007 1:10 pm
Posts: 5
hi all

I've found that it isn't a problem in the view-layer. I wrote a test for the service layer and get the same exception.

It is very strange: in the test of the dao-layer the method runs fine, but as soon as it is called by the service-layer I get the org.hibernate.SessionException: Session is closed! But basically I do the same thing in the dao-test as in the service-layer: List<Langauge> list = dao.findAll();

by the way, I don't think this is a lazy-problem because the class has no collections and I never access a field-value in the test. I just check the resulting list for null and it's size.

any ideas?
stefan

Here's a log-dump of a failed service-test:
DEBUG [main] (GenericDaoJpa.java:62) - findAll, class-name: Language
DEBUG [main] (SharedEntityManagerCreator.java:167) - Creating new EntityManager for shared EntityManager invocation
DEBUG [main] (SessionImpl.java:220) - opened session at timestamp: 11748285418
DEBUG [main] (QueryTranslatorImpl.java:246) - parse() - HQL: from ch.berndeutsch.kamus.model.Language
DEBUG [main] (QueryTranslatorImpl.java:266) - --- HQL AST ---
\-[QUERY] 'query'
\-[SELECT_FROM] 'SELECT_FROM'
\-[FROM] 'from'
\-[RANGE] 'RANGE'
\-[DOT] '.'
+-[DOT] '.'
| +-[DOT] '.'
| | +-[DOT] '.'
| | | +-[IDENT] 'ch'
| | | \-[IDENT] 'berndeutsch'
| | \-[IDENT] 'kamus'
| \-[IDENT] 'model'
\-[IDENT] 'Language'

DEBUG [main] (ErrorCounter.java:68) - throwQueryException() : no errors
DEBUG [main] (HqlSqlBaseWalker.java:111) - select << begin [level=1, statement=select]
DEBUG [main] (FromElement.java:109) - FromClause{level=1} : ch.berndeutsch.kamus.model.Language (no alias) -> language0_
DEBUG [main] (HqlSqlBaseWalker.java:117) - select : finishing up [level=1, statement=select]
DEBUG [main] (HqlSqlWalker.java:508) - processQuery() : ( SELECT ( FromClause{level=1} languages language0_ ) )
DEBUG [main] (HqlSqlWalker.java:715) - Derived SELECT clause created.
DEBUG [main] (JoinProcessor.java:148) - Using FROM fragment [languages language0_]
DEBUG [main] (HqlSqlBaseWalker.java:123) - select >> end [level=1, statement=select]
DEBUG [main] (QueryTranslatorImpl.java:232) - --- SQL AST ---
\-[SELECT] QueryNode: 'SELECT' querySpaces (languages)
+-[SELECT_CLAUSE] SelectClause: '{derived select clause}'
| +-[SELECT_EXPR] SelectExpressionImpl: 'language0_.id as id2_' {FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=null,role=null,tableName=languages,tableAlias=language0_,origin=null,colums={,className=ch.berndeutsch.kamus.model.Language}}}
| \-[SQL_TOKEN] SqlFragment: 'language0_.isoCode as isoCode2_, language0_.displayName as displayN3_2_, language0_.version as version2_'
\-[FROM] FromClause: 'from' FromClause{level=1, fromElementCounter=1, fromElements=1, fromElementByClassAlias=[], fromElementByTableAlias=[language0_], fromElementsByPath=[], collectionJoinFromElementsByPath=[], impliedElements=[]}
\-[FROM_FRAGMENT] FromElement: 'languages language0_' FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=null,role=null,tableName=languages,tableAlias=language0_,origin=null,colums={,className=ch.berndeutsch.kamus.model.Language}}

DEBUG [main] (ErrorCounter.java:68) - throwQueryException() : no errors
DEBUG [main] (QueryTranslatorImpl.java:216) - HQL: from ch.berndeutsch.kamus.model.Language
DEBUG [main] (QueryTranslatorImpl.java:217) - SQL: select language0_.id as id2_, language0_.isoCode as isoCode2_, language0_.displayName as displayN3_2_, language0_.version as version2_ from languages language0_
DEBUG [main] (ErrorCounter.java:68) - throwQueryException() : no errors
DEBUG [main] (AbstractEntityManagerImpl.java:417) - mark transaction for rollback


Top
 Profile  
 
 Post subject: SOLVED with transaction
PostPosted: Mon Mar 26, 2007 3:57 pm 
Newbie

Joined: Sat Mar 24, 2007 1:10 pm
Posts: 5
hi all

It's once again myself. I've found the problem: my dao-tests are running automatically in a transaction that is rolled back after the test (the magic is done by spring).
As soon as I run the service method in a transaction too (read-only in my case), it's running fine.

can anybody explain me why the loading of objects fails when there is no transaction? From time to time I think I will never understand o/r-mapping :-)

thanks
stefan


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 26, 2007 4:11 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I guess if there is no tx, spring automagically close the session right away. pure guess though

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Generic CRUD Interface "Java Persistence with Hibernate
PostPosted: Sat Mar 31, 2007 8:05 pm 
Newbie

Joined: Sat Mar 24, 2007 1:10 pm
Posts: 5
hi emmanuel

I'm using the generic CRUD interface, introduced on page 711 in "Java Persistence with Hibernate".

Is it possible that this causes the "must-be-transactional"? I really don't see this deep into ORM, but my service-methods are calling the concrete DAO, that extends/calls the generic DAO that does the database query.

Is it possible that spring closes the session right after the generic DAO has done it's work and therefore, the concrete DAO can't get the data from the generic DAO or can't pass the data to the serivce layer?

regards
stefan


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 01, 2007 2:45 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
You should not use any Spring filters or templates if you use Java Persistence (or you should ask in a Spring forum how their proprietary stuff works).

If the EntityManager is the primary interface you use, there are two ways to implement "Open Session (persistence context) in view":

- Use an EJB 3.0 container and scope the persistence context to a stateful session bean that is available when your view is rendering

- Use JBoss Seam, which can manage persistence contexts for you even with plain JavaBeans as application components

With EJB 3.0:

Code:
@Stateless
public class GenericDAO {

    @PersistenceContext
    EntityManager em;

}


With EJB 3.0, you can give the EM an extended life by binding it to a SFSB:

Code:
@Stateful
public class MyAction {

    @PersistenceContext(EXTENDED)
    EntityManager em;
   
    @EJB
    MyDAO myDAO;

    public void doSomething() {
        myDAO.loadStuff();
    }

}


The same persistence context is now available when the view after doSomething() is rendered. It is also propagated into the myDAO.loadStuff() call and injected into that DAO.

With Seam and JavaBeans it looks like this:

Code:
@Name("mySeamComponent")
public class MyAction {

    @In
    MyDAO myDAO;

    public void doSomething() {
        myDAO.loadStuff();
    }

}


Code:
public class GenericDAO {

    @In
    EntityManager em;

}


Code:
Name("myDAO")
public class MyDAO extends GenericDAO {

}


In both environments you can easily call em.getDelegate() to get a Session interface when needed.

I recommend you first drop the chapter 16 in Java Persistence with Hibernate and read chapters 9, 10, and 11, which give you the background to understand all of this.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.