You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Martin Ravell <st...@rave-tech.com.au> on 2005/10/06 13:01:36 UTC

Premature Session Close

I’m running Struts, Tiles, Hibernate 2 and Spring in my project and have
been attempting for some time to get the OpenSessionInView pattern to work
without much luck.

I have the filter setup in my web.xml like so :


  <filter>
    <filter-name>hibernateFilter</filter-name>
    <filter-class>
org.springframework.orm.hibernate.support.OpenSessionInViewFilter
    </filter-class>
  </filter>
    
  <filter-mapping>
    <filter-name>hibernateFilter</filter-name>
    <url-pattern>*.do</url-pattern>
  </filter-mapping>


applicationContext.xml for various things is as follows:


  <bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean"> 
	<property name="jndiName">
      <value>java:/JournalDS</value> 
	</property>
  </bean>

  <!-- Hibernate SessionFactory -->
  <bean id="sessionFactory"
class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
    <property name="dataSource">
	    <ref local="dataSource"/>
    </property>
    <property name="mappingResources">
      <list>
        <value>com/rave/journal/model/Activity.hbm.xml</value>
          .
          .
          .
          .
          .
      </list>
    </property>
    <property name="hibernateProperties">
    <props>
      <prop key="hibernate.dialect">
        net.sf.hibernate.dialect.Oracle9Dialect
      </prop>
      <prop key="hibernate.show_sql">
        false
      </prop>
    </props>
    </property>
  </bean>


  <!-- Transaction manager for a single Hibernate SessionFactory -->
  <bean id="transactionManager"
class="org.springframework.orm.hibernate.HibernateTransactionManager">
    <property name="sessionFactory">
      <ref local="sessionFactory"/>
    </property>
  </bean>



Looking at the log output it seems to be closing the Hibernate session after
the first POJO is populated in my Action. The next piece of code tries to
access a collection and bam, down it goes :

          .
          .
          . 
[net.sf.hibernate.impl.SessionImpl] executing flush
[net.sf.hibernate.impl.SessionImpl] post flush
[org.springframework.transaction.support.TransactionSynchronizationManager]
Removed value [org.springframework.orm.hibernate.SessionHolder@181db77] for
key [net.sf.hibernate.impl.SessionFactoryImpl@9ea579] from thread
[Ajp13Processor[8009][4]]
[org.springframework.orm.hibernate.SessionFactoryUtils] Closing Hibernate
session
[net.sf.hibernate.impl.SessionImpl] closing session
[net.sf.hibernate.impl.SessionImpl] disconnecting session
[net.sf.hibernate.impl.SessionImpl] transaction completion
[net.sf.hibernate.LazyInitializationException] Failed to lazily initialize a
collection - no session or session was closed

 
All my mappings have lazy set to true. If I switch them all over to false
the application does run.  If I look at all the SQL generated with lazy set
false though it is quite scary how much access is done. 

I've attached a bigger excerpt from the log if that will help track down the
issue.

Any ideas what would be causing the Hibernate session to be closed? If I
understand things correctly the OpenSessionInViewFilter should be the thing
closing the session (after everything has run in the Request. From the log
it looks as if org.springframework.orm.hibernate.SessionFactoryUtils is
doing it.

I'd really appreciate some help here. This has been driving me nuts for
days.




Regards
Marty
 
Martin Ravell
FMS Global




---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Premature Session Close

Posted by Martin Ravell <st...@rave-tech.com.au>.
Unfortunately doing an eager fetch would (if I understand the concept
correctly) be the exact opposite of what I am trying to achieve here. I
really need the load to be lazy.

Am I correct in thinking that an eager fetch is the opposite of a lazy one?
i.e. all of the associated collections are loaded with the owning object?

The second suggestion of doing an initialize is basically the same right? It
is saying to Hibernate 'go and fill this collection for this object'. i.e.
not being lazy.

What I am trying to do is use the OpenSessionInView pattern.

This means that the control of opening and closing Hibernate sessions is
delegated to a filter (or some other mechanism) which opens the session at
the start of the request, allows any objects whether they are in the DAO
layer, Action layer, even JSP layer to access hibernate objects with Lazy
loading. Therefore it should only load collections when they are first
accessed. The filter should then execute it's shutdown method at the end of
the request which will close the Hibernate session.



Regards
Marty

-----Original Message-----
From: Martin Gainty [mailto:mgainty@hotmail.com] 
Sent: Friday, 7 October 2005 12:47 AM
To: struts@rave-tech.com.au
Cc: Struts Users Mailing List
Subject: Re: Premature Session Close

2 solutions I found at hibernate users group
http://forum.hibernate.org/viewtopic.php?t=938083&sid=3e74c7e9a3aa83e9cde5ce
4862c920fb

.assuming you will be retrieving a userID from your User class..
do an 'eager fetch' instead of lazy fetch e.g.

user = (User) session.createCriteria(User.class)
    .setFetchMode("groups", FetchMode.EAGER)
    .add(Expression.eq("userID", new Integer(userID)))
    .uniqueResult();

OR
In the case where you have nothing to return
add the item (userID) to the User class AND actively initialise hibernate to

apprise it of the new item's addition
user = (User) session.createCriteria(User.class).add(Expression.eq("userID",

new Integer(userID))).uniqueResult();
Hibernate.initialize(user.getGroups());

HTH,
Martin-

----- Original Message ----- 
From: "Martin Ravell" <st...@rave-tech.com.au>
To: "'Struts Users Mailing List'" <us...@struts.apache.org>; "'Martin 
Gainty'" <mg...@hotmail.com>
Sent: Thursday, October 06, 2005 10:14 AM
Subject: RE: Premature Session Close


> Hi Martin,
>
> Thanks for the post. Yep this is one of the main sites I have referenced 
> in
> trying to get this stuff up and running.
>
> BTW. I mentioned a log file in the first post and then forgot to attach 
> it.
> Here it is on this email.
>
> I wonder if the
> org.springframework.transaction.support.TransactionSynchronizationManager 
> is
> somehow involved? Its either that or
> org.springframework.orm.hibernate.SessionFactoryUtils.
>
> Trouble is I do not understand the mechanisms where these classes get 
> called
> from.
>
>
>
> Regards
> Marty
>
>>
>>-----Original Message-----
>>From: Martin Gainty [mailto:mgainty@hotmail.com]
>>Sent: Thursday, 6 October 2005 11:57 PM
>>To: Struts Users Mailing List
>>Subject: Re: Premature Session Close
>>
>>Martin/Werner
>>
>>Have you had a chance to look at Karl Blaums Lacy Initialization and the
> DAO
>>Pattern with Hibernate and Spring example
>>http://www.jroller.com/comments/kbaum/Weblog/orm_lazy_initialization_with_
d
> ao
>>
>>Martin-
>>
>>----- Original Message ----- 
>>From: "Werner Punz" <we...@gmx.at>
>>To: <us...@struts.apache.org>
>>Sent: Thursday, October 06, 2005 8:48 AM
>>Subject: Re: Premature Session Close
>>
>>
>>>I ran into a situation like that once, the application was using iframes
>>> internally which triggered other sessions...
>>>
>>> Werner
>>>
>>>
>>> Martin Ravell wrote:
>>>> I'm running Struts, Tiles, Hibernate 2 and Spring in my project and 
>>>> have
>>>> been attempting for some time to get the OpenSessionInView pattern to
>>>> work
>>>> without much luck.
>>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>>> For additional commands, e-mail: user-help@struts.apache.org
>>>
>>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>>For additional commands, e-mail: user-help@struts.apache.org
>


----------------------------------------------------------------------------
----


> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Premature Session Close

Posted by Martin Gainty <mg...@hotmail.com>.
2 solutions I found at hibernate users group
http://forum.hibernate.org/viewtopic.php?t=938083&sid=3e74c7e9a3aa83e9cde5ce4862c920fb

.assuming you will be retrieving a userID from your User class..
do an 'eager fetch' instead of lazy fetch e.g.

user = (User) session.createCriteria(User.class)
    .setFetchMode("groups", FetchMode.EAGER)
    .add(Expression.eq("userID", new Integer(userID)))
    .uniqueResult();

OR
In the case where you have nothing to return
add the item (userID) to the User class AND actively initialise hibernate to 
apprise it of the new item's addition
user = (User) session.createCriteria(User.class).add(Expression.eq("userID", 
new Integer(userID))).uniqueResult();
Hibernate.initialize(user.getGroups());

HTH,
Martin-

----- Original Message ----- 
From: "Martin Ravell" <st...@rave-tech.com.au>
To: "'Struts Users Mailing List'" <us...@struts.apache.org>; "'Martin 
Gainty'" <mg...@hotmail.com>
Sent: Thursday, October 06, 2005 10:14 AM
Subject: RE: Premature Session Close


> Hi Martin,
>
> Thanks for the post. Yep this is one of the main sites I have referenced 
> in
> trying to get this stuff up and running.
>
> BTW. I mentioned a log file in the first post and then forgot to attach 
> it.
> Here it is on this email.
>
> I wonder if the
> org.springframework.transaction.support.TransactionSynchronizationManager 
> is
> somehow involved? Its either that or
> org.springframework.orm.hibernate.SessionFactoryUtils.
>
> Trouble is I do not understand the mechanisms where these classes get 
> called
> from.
>
>
>
> Regards
> Marty
>
>>
>>-----Original Message-----
>>From: Martin Gainty [mailto:mgainty@hotmail.com]
>>Sent: Thursday, 6 October 2005 11:57 PM
>>To: Struts Users Mailing List
>>Subject: Re: Premature Session Close
>>
>>Martin/Werner
>>
>>Have you had a chance to look at Karl Blaums Lacy Initialization and the
> DAO
>>Pattern with Hibernate and Spring example
>>http://www.jroller.com/comments/kbaum/Weblog/orm_lazy_initialization_with_d
> ao
>>
>>Martin-
>>
>>----- Original Message ----- 
>>From: "Werner Punz" <we...@gmx.at>
>>To: <us...@struts.apache.org>
>>Sent: Thursday, October 06, 2005 8:48 AM
>>Subject: Re: Premature Session Close
>>
>>
>>>I ran into a situation like that once, the application was using iframes
>>> internally which triggered other sessions...
>>>
>>> Werner
>>>
>>>
>>> Martin Ravell wrote:
>>>> I'm running Struts, Tiles, Hibernate 2 and Spring in my project and 
>>>> have
>>>> been attempting for some time to get the OpenSessionInView pattern to
>>>> work
>>>> without much luck.
>>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>>> For additional commands, e-mail: user-help@struts.apache.org
>>>
>>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>>For additional commands, e-mail: user-help@struts.apache.org
>


--------------------------------------------------------------------------------


> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Premature Session Close

Posted by Martin Ravell <st...@rave-tech.com.au>.
Hi Martin,

Thanks for the post. Yep this is one of the main sites I have referenced in
trying to get this stuff up and running.

BTW. I mentioned a log file in the first post and then forgot to attach it.
Here it is on this email.

I wonder if the
org.springframework.transaction.support.TransactionSynchronizationManager is
somehow involved? Its either that or
org.springframework.orm.hibernate.SessionFactoryUtils.

Trouble is I do not understand the mechanisms where these classes get called
from.



Regards
Marty

>
>-----Original Message-----
>From: Martin Gainty [mailto:mgainty@hotmail.com] 
>Sent: Thursday, 6 October 2005 11:57 PM
>To: Struts Users Mailing List
>Subject: Re: Premature Session Close
>
>Martin/Werner
>
>Have you had a chance to look at Karl Blaums Lacy Initialization and the
DAO 
>Pattern with Hibernate and Spring example
>http://www.jroller.com/comments/kbaum/Weblog/orm_lazy_initialization_with_d
ao
>
>Martin-
>
>----- Original Message ----- 
>From: "Werner Punz" <we...@gmx.at>
>To: <us...@struts.apache.org>
>Sent: Thursday, October 06, 2005 8:48 AM
>Subject: Re: Premature Session Close
>
>
>>I ran into a situation like that once, the application was using iframes
>> internally which triggered other sessions...
>>
>> Werner
>>
>>
>> Martin Ravell wrote:
>>> I'm running Struts, Tiles, Hibernate 2 and Spring in my project and have
>>> been attempting for some time to get the OpenSessionInView pattern to 
>>> work
>>> without much luck.
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>> 
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>For additional commands, e-mail: user-help@struts.apache.org

Re: Premature Session Close

Posted by Martin Gainty <mg...@hotmail.com>.
Martin/Werner

Have you had a chance to look at Karl Blaums Lacy Initialization and the DAO 
Pattern with Hibernate and Spring example
http://www.jroller.com/comments/kbaum/Weblog/orm_lazy_initialization_with_dao

Martin-

----- Original Message ----- 
From: "Werner Punz" <we...@gmx.at>
To: <us...@struts.apache.org>
Sent: Thursday, October 06, 2005 8:48 AM
Subject: Re: Premature Session Close


>I ran into a situation like that once, the application was using iframes
> internally which triggered other sessions...
>
> Werner
>
>
> Martin Ravell wrote:
>> I’m running Struts, Tiles, Hibernate 2 and Spring in my project and have
>> been attempting for some time to get the OpenSessionInView pattern to 
>> work
>> without much luck.
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Premature Session Close

Posted by Martin Ravell <st...@rave-tech.com.au>.
Hi Werner,

Hmm. I do not use iframes but I am doing Single Sign On. (JBoss/Tomcat with
multiple applications on the one server) 

I wonder if that could complicate matters?


Regards
Marty

>-----Original Message-----
>From: news [mailto:news@sea.gmane.org] On Behalf Of Werner Punz
>Sent: Thursday, 6 October 2005 10:48 PM
>To: user@struts.apache.org
>Subject: Re: Premature Session Close
>
>I ran into a situation like that once, the application was using iframes
>internally which triggered other sessions... 
>
>Werner
>
>
>Martin Ravell wrote:
>> I'm running Struts, Tiles, Hibernate 2 and Spring in my project and have
>> been attempting for some time to get the OpenSessionInView pattern to
work
>> without much luck.
>>
>




---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Premature Session Close

Posted by Werner Punz <we...@gmx.at>.
I ran into a situation like that once, the application was using iframes
internally which triggered other sessions...

Werner


Martin Ravell wrote:
> I’m running Struts, Tiles, Hibernate 2 and Spring in my project and have
> been attempting for some time to get the OpenSessionInView pattern to work
> without much luck.
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org