-->
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.  [ 3 posts ] 
Author Message
 Post subject: LazyInitializationException usin OpenSessionInView?!?! Odd..
PostPosted: Mon Jun 30, 2008 9:31 am 
Newbie

Joined: Fri Sep 14, 2007 9:41 am
Posts: 8
What I'm doing here is: I log-in and call another Struts action to show the user some documents stored in the database....The first call actually works, but the others always thows this error. The strange thing is that that reading the logs it don't close the session neither the transaction, so why is it throwing this $*&#$ing error?

Can someone help?

Hibernate version: 3.1

Mapping documents:

Code:
<hibernate-mapping>
    <class name="com.pix.hibernate.entities.Usuarios" table="usuarios" schema="dbo" catalog="vanpix">
        <id name="idusuarios" type="integer">
            <column name="idusuarios" />
            <generator class="native" />
        </id>
        <many-to-one name="tabStatus" class="com.pix.hibernate.entities.TabStatus" fetch="select">
            <column name="usr_status" not-null="true" />
        </many-to-one>
        <property name="usrLogin" type="string">
            <column name="usr_login" length="10" not-null="true" unique="true" />
        </property>
        <property name="usrNome" type="string">
            <column name="usr_nome" length="60" not-null="true" />
        </property>
        <property name="usrSenha" type="string">
            <column name="usr_senha" length="32" not-null="true" />
        </property>
        <property name="usrEmail" type="string">
            <column name="usr_email" length="70" not-null="true" />
        </property>
        <property name="usrCpf" type="string">
            <column name="usr_cpf" length="11" not-null="true" />
        </property>
        <set name="cxpostalUsuarioses" inverse="true">
            <key>
                <column name="idusuarios" not-null="true" />
            </key>
            <one-to-many class="com.pix.hibernate.entities.CxpostalUsuarios" />
        </set>
        <set name="docAssinados" inverse="true">
            <key>
                <column name="idusuarios" not-null="true" />
            </key>
            <one-to-many class="com.pix.hibernate.entities.DocAssinado" />
        </set>
    </class>
</hibernate-mapping>

<hibernate-mapping>
    <class name="com.pix.hibernate.entities.Documentos" table="documentos" schema="dbo" catalog="vanpix">
        <id name="iddocumentos" type="long">
            <column name="iddocumentos" />
            <generator class="native" />
        </id>
        <many-to-one name="cxpostal" class="com.pix.hibernate.entities.Cxpostal" fetch="select">
            <column name="idcxpostal" not-null="true" />
        </many-to-one>
        <property name="docIdentificacao" type="string">
            <column name="doc_identificacao" length="20" not-null="true" />
        </property>
        <property name="docDtentrada" type="timestamp">
            <column name="doc_dtentrada" length="23" not-null="true" />
        </property>
        <property name="docTamanho" type="long">
            <column name="doc_tamanho" not-null="true" />
        </property>
        <property name="docDtenvio" type="timestamp">
            <column name="doc_dtenvio" length="23" />
        </property>
        <set name="docAssinados" inverse="true">
            <key>
                <column name="iddocumentos" not-null="true" />
            </key>
            <one-to-many class="com.pix.hibernate.entities.DocAssinado" />
        </set>
    </class>
</hibernate-mapping>



Code between sessionFactory.openSession() and session.close():
Usin OpenSessionInView


Full stack trace of any exception that occurs:
Code:
0:15:41,490 ERROR LazyInitializationException:19 - failed to lazily initialize a collection of role: com.pix.hibernate.entities.Cxpostal.documentoses, no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.pix.hibernate.entities.Cxpostal.documentoses, no session or session was closed
   at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
   at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
   at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:97)
   at org.hibernate.collection.PersistentSet.size(PersistentSet.java:114)
   at com.pix.vo.ClientInfoVO.getDocumentosCxAtual(ClientInfoVO.java:69)
   at com.pix.struts.action.ListaDocumentosAction.execute(ListaDocumentosAction.java:51)


Name and version of the database you are using:
MSSql Server 2000

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:
10:15:19,286 DEBUG SessionImpl:219 - opened session at timestamp: 4975950722195456
10:15:19,302 DEBUG ThreadLocalSessionContext:290 - allowing method [beginTransaction] in non-transacted context
10:15:19,302 DEBUG ThreadLocalSessionContext:300 - allowing proxied method [beginTransaction] to proceed to real session
10:15:19,302 DEBUG JDBCTransaction:54 - begin
10:15:19,302 DEBUG ConnectionManager:415 - opening JDBC connection
10:15:19,428 DEBUG JDBCTransaction:59 - current autocommit status: false
10:15:19,428 DEBUG JDBCContext:194 - after transaction begin
10:15:36,061 DEBUG ThreadLocalSessionContext:300 - allowing proxied method [isOpen] to proceed to real session
10:15:36,801 DEBUG ThreadLocalSessionContext:300 - allowing proxied method [isOpen] to proceed to real session
10:15:41,490 ERROR LazyInitializationException:19 - failed to lazily initialize a collection of role: com.pix.hibernate.entities.Cxpostal.documentoses, no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.pix.hibernate.entities.Cxpostal.documentoses, no session or session was closed

The code throwing the exception:
Code:
public List getDocumentosCxAtual(){
      Cxpostal cx = ((Cxpostal)cxsPostais.get(caixaPostalAtual));
      Set documentos = cx.getDocumentoses();
      ArrayList al = new  ArrayList();
      if(documentos.size() >0){
         Iterator it = documentos.iterator();
         while (it.hasNext()) {
            DocumentoVO dVO = new DocumentoVO();
            Documentos object = (Documentos) it.next();
            dVO.setDocDtentrada(object.getDocDtentrada());
            dVO.setDocDtenvio(object.getDocDtenvio());
            dVO.setDocIdentificacao(object.getDocIdentificacao());
            dVO.setDocTamanho(object.getDocTamanho());
            dVO.setIddocumentos(object.getIddocumentos());
            dVO.setNecessitaQtdAssinaturas(cx.getCxQtdAssinatura());
            Set d = object.getDocAssinados();
            if(d.contains(object)){
               dVO.setIsAssinado(true);
            }
            al.add(dVO);
         }
         if(al!=null && al.size() >= 2){
            Collections.sort(al);
         }
      }
      return al;
   }


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 30, 2008 10:57 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Perhaps the session is being closed when the session goes out of scope, or perhaps, the second piece of being code being called is on a different JVM or in a different thread, and does not have access to the same Hibernate Session.

Is everything running on the same JVM? In the same thread?

How are you implementing the Open Session in View pattern? Are you using a ServletFilter? Are any runtime exceptions being thrown? Just a few things to think about.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 30, 2008 12:38 pm 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
Are you using session.evict / session.evictAll?

Most of the time the exception means your session got closed, but it might as well mean that the object was removed from the session

_________________
Please rate useful posts.


Schauderhaft: Softwaredevelopment, Projectmanagement, Qualitymanagement and all things "schauderhaft"


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