-->
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.  [ 10 posts ] 
Author Message
 Post subject: sessions using EntityManager
PostPosted: Thu Aug 14, 2008 5:34 am 
Newbie

Joined: Thu Aug 14, 2008 5:15 am
Posts: 5
Hi all,

well, I am building a web application using struts 2, Spring and Hibernate JTA. I'm having a little problem because when the application executes a query, the console writes: org.hibernate.SessionException: Session is closed!

I haven't got the control of the session, because hibernate is integrated with Spring, and this is completely automatic. (I dont open or close the session, It is managed by the EntityManager object.

I have reading in forums and I solved it but I am not sure if it's really correct. The solution was, If I put @Transactional on top of the method that execute the query then it works fine and it doesn`t throw any exception... my question is: is it a really solution? is correct to make every single methods @transactionals...? anyone knows any other way to fix it?

thanks guys!
(and sorry for my english, it's not good)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 14, 2008 5:45 am 
Regular
Regular

Joined: Wed Apr 09, 2008 10:28 am
Posts: 52
which method do you referring? some methods has to be with @Transactional to ensure the operations are done within a transaction. The reason why you don't get a exception i believe is because the session is being closed after the transaction is commited.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 14, 2008 6:08 am 
Newbie

Joined: Thu Aug 14, 2008 5:15 am
Posts: 5
the method is this:

@Transactional
@SuppressWarnings("unchecked")
public List<User> findAll() {
Query query = getEntityManager().createQuery("select p FROM User p");
return query.getResultList();

}

If I remove "@Transaction" it does not work.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 14, 2008 6:51 am 
Regular
Regular

Joined: Wed Apr 09, 2008 10:28 am
Posts: 52
what's the exception you getting? Session is closed?


Top
 Profile  
 
 Post subject: the exception!
PostPosted: Thu Aug 14, 2008 8:44 am 
Newbie

Joined: Thu Aug 14, 2008 5:15 am
Posts: 5
The exception is this one:


1) testFindByMLUser(com.trixpert.service.DeviceReposi toryImplTest)javax.persistence.PersistenceExceptio n: org.hibernate.SessionException: Session is closed!
at org.hibernate.ejb.AbstractEntityManagerImpl.throwP ersistenceException(AbstractEntityManagerImpl.java :641)
at org.hibernate.ejb.QueryImpl.getResultList(QueryImp l.java:73)
at com.trixpert.service.DeviceRepositoryImpl.findByML User(DeviceRepositoryImpl.java:43)
at com.trixpert.service.DeviceRepositoryImplTest.test FindByMLUser(DeviceRepositoryImplTest.java:61)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
at org.springframework.test.ConditionalTestCase.runBa re(ConditionalTestCase.java:69)
at org.springframework.test.annotation.AbstractAnnota tionAwareTransactionalTests.access$001(AbstractAnn otationAwareTransactionalTests.java:44)
at org.springframework.test.annotation.AbstractAnnota tionAwareTransactionalTests$1.run(AbstractAnnotati onAwareTransactionalTests.java:112)
at org.springframework.test.annotation.AbstractAnnota tionAwareTransactionalTests.runTest(AbstractAnnota tionAwareTransactionalTests.java:177)
at org.springframework.test.annotation.AbstractAnnota tionAwareTransactionalTests.runTestTimed(AbstractA nnotationAwareTransactionalTests.java:150)
at org.springframework.test.annotation.AbstractAnnota tionAwareTransactionalTests.runBare(AbstractAnnota tionAwareTransactionalTests.java:108)
at org.springframework.test.jpa.AbstractJpaTests.runB are(AbstractJpaTests.java:160)
at org.springframework.test.jpa.AbstractJpaTests.runB are(AbstractJpaTests.java:239)
Caused by: org.hibernate.SessionException: Session is closed!
at org.hibernate.impl.AbstractSessionImpl.errorIfClos ed(AbstractSessionImpl.java:49)
at org.hibernate.impl.SessionImpl.list(SessionImpl.ja va:1110)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:7 9)
at org.hibernate.ejb.QueryImpl.getResultList(QueryImp l.java:64)
... 25 more


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 14, 2008 8:48 am 
Regular
Regular

Joined: Wed Apr 09, 2008 10:28 am
Posts: 52
Can you post your web.xml please? I think you can solve this by using the OpenSessionInViewFilter.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 14, 2008 9:34 am 
Newbie

Joined: Thu Aug 14, 2008 5:15 am
Posts: 5
of course, I copy it;
if you need I send you anything else, please, tell me, I am trying to solve it too.

Thank you.


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="person" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>manila</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 14, 2008 9:43 am 
Regular
Regular

Joined: Wed Apr 09, 2008 10:28 am
Posts: 52
I fixed this problem by using this before the struts 2 filter.

Code:
<filter>
   <filter-name>OpenEntityManagerInViewFilter</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
   
<filter-mapping>
   <filter-name>OpenEntityManagerInViewFilter</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>


but as i understand it right your using JTA i don't know if there is a such thing
for it too. I'm using JPA EntityManager.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 14, 2008 9:50 am 
Newbie

Joined: Thu Aug 14, 2008 5:15 am
Posts: 5
Great!!

It is working now!


Thank you so much

(I said JTA... I meant JPA LOL)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 14, 2008 9:53 am 
Regular
Regular

Joined: Wed Apr 09, 2008 10:28 am
Posts: 52
yeah i thought too that you misspelled it :). Glad to hear it is working.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 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.