-->
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.  [ 8 posts ] 
Author Message
 Post subject: Fetching Data Lazily On A New Page After Hibernate Search
PostPosted: Tue Mar 04, 2008 7:14 pm 
Regular
Regular

Joined: Fri Oct 05, 2007 1:17 pm
Posts: 78
I am using JPA/Hibernate and Hibernate Search within my Seam application. I have a Widget entity that lazily loads its pricing history. The use case is pretty typical:

* Search for widgets on a form with a set of criteria.
* Display all widget search results from Hibernate Search in a table below the search form. The results include all the items found in the Widget table itself including the widget number, which is a link to another page.
* When you click on the widget number, you come to a widget details page that fetches all the 1:M relationships widgets have with other entities to get information like pricing history. None of these are indexed or have anything to do with Hibernate Search.

Note: Because I am using the RichFaces dataScroller with server-side paging, I have to use my own fancy data model and cannot use the standard Seam annotations like @DataModel and @DataModelSection.

The way I am doing my own version of @DataModelSection is to do the following:

<h:commandLink value="#{widget.number}" action="#{searchAction.setSelectedWidget(widget)}" />

where widget is the iteration variable.

Then through pages.xml, I navigate to the widget details page. All of that works fine until I get this:

Code:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.myapp.persistence.Widget.pricingHistory, 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.PersistentBag.size(PersistentBag.java:225)
   at javax.faces.model.ListDataModel.isRowAvailable(ListDataModel.java:99)
   at javax.faces.model.ListDataModel.setRowIndex(ListDataModel.java:174)
   at javax.faces.model.ListDataModel.setWrappedData(ListDataModel.java:209)
   at javax.faces.model.ListDataModel.<init>(ListDataModel.java:68)
   at org.ajax4jsf.component.SequenceDataAdaptor.getDataModel(SequenceDataAdaptor.java:79)
   at org.ajax4jsf.component.SequenceDataAdaptor.createDataModel(SequenceDataAdaptor.java:64)
   at org.ajax4jsf.component.UIDataAdaptor.getExtendedDataModel(UIDataAdaptor.java:592)
   at org.ajax4jsf.component.UIDataAdaptor.getRowIndex(UIDataAdaptor.java:299)
   at jrockit.reflect.VirtualNativeMethodInvoker.invoke(Ljava.lang.Object;[Ljava.lang.Object;)Ljava.lang.Object;(Unknown Source)
   at java.lang.reflect.Method.invoke(Ljava.lang.Object;[Ljava.lang.Object;I)Ljava.lang.Object;(Unknown Source)
   at com.sun.facelets.util.DevTools.writeAttributes(DevTools.java:240)
   at com.sun.facelets.util.DevTools.writeStart(DevTools.java:284)
   at com.sun.facelets.util.DevTools.writeComponent(DevTools.java:189)
   at com.sun.facelets.util.DevTools.writeComponent(DevTools.java:207)
   at com.sun.facelets.util.DevTools.writeComponent(DevTools.java:207)
   at com.sun.facelets.util.DevTools.writeComponent(DevTools.java:207)
   at com.sun.facelets.util.DevTools.writeComponent(DevTools.java:207)
   at com.sun.facelets.util.DevTools.debugHtml(DevTools.java:133)


Now I understand that the Widget has been detatched from the JPA EntityManager by this time. But when I click that link and come to the details page, I would like the entity to be smart enough to open another session behind the scenes and grab the information it now needs.

Here is my components.xml:

Code:
<persistence:entity-manager-factory name="emf" persistence-unit-name="myWebapp" />
<persistence:managed-persistence-context name="em" auto-create="true" entity-manager-factory="#{emf}" />
<transaction:entity-transaction entity-manager="#{em}"/>


I read at http://docs.jboss.com/seam/2.0.0.GA/reference/en/html/persistence.html#persistence.seam-managed-transactions that my setup in components.xml where I specify this:

<persistence:managed-persistence-context name="em" auto-create="true" entity-manager-factory="#{emf}" />

should set a conversation-scoped persistence context. Clearly, that is not the case. Could the FullTextEntityManager be an issue here?

Thanks for any insight.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 05, 2008 2:21 am 
Regular
Regular

Joined: Fri Oct 05, 2007 1:17 pm
Posts: 78
In addition, I was curious how/where the FTEM closes the session.

When I get back to the office, I am going to try injecting an EntityManager instance with @In and manually constructing an instance of FTEM in a method annotated with @Create--rather than have the FTEM injected directly. Since my setup should mean that my EntityManager is conversation-scoped, maybe this will help the session stay open for the duration of the conversation.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 06, 2008 6:41 pm 
Regular
Regular

Joined: Fri Oct 05, 2007 1:17 pm
Posts: 78
In case this helps, here is the log for when I click the link to go to the details page:

Code:
06 Mar 2008 14:41:23,662: DEBUG org.hibernate.transaction.JDBCTransaction - commit
06 Mar 2008 14:41:23,662: DEBUG org.hibernate.impl.SessionImpl - automatically flushing session
06 Mar 2008 14:41:23,662: DEBUG org.hibernate.jdbc.JDBCContext - before transaction completion
06 Mar 2008 14:41:23,662: DEBUG org.hibernate.impl.SessionImpl - before transaction completion
06 Mar 2008 14:41:23,662: DEBUG org.hibernate.transaction.JDBCTransaction - re-enabling autocommit
06 Mar 2008 14:41:23,662: DEBUG org.hibernate.transaction.JDBCTransaction - committed JDBC Connection
06 Mar 2008 14:41:23,662: DEBUG org.hibernate.jdbc.JDBCContext - after transaction completion
06 Mar 2008 14:41:23,662: DEBUG org.hibernate.jdbc.ConnectionManager - aggressively releasing JDBC connection
06 Mar 2008 14:41:23,662: DEBUG org.hibernate.jdbc.ConnectionManager - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
06 Mar 2008 14:41:23,662: DEBUG org.hibernate.impl.SessionImpl - after transaction completion
06 Mar 2008 14:41:23,672: DEBUG org.hibernate.transaction.JDBCTransaction - begin
06 Mar 2008 14:41:23,672: DEBUG org.hibernate.jdbc.ConnectionManager - opening JDBC connection
06 Mar 2008 14:41:23,672: DEBUG org.hibernate.transaction.JDBCTransaction - current autocommit status: true
06 Mar 2008 14:41:23,672: DEBUG org.hibernate.transaction.JDBCTransaction - disabling autocommit
06 Mar 2008 14:41:23,672: DEBUG org.hibernate.jdbc.JDBCContext - after transaction begin
06 Mar 2008 14:41:23,672: DEBUG org.hibernate.impl.SessionImpl - setting flush mode to: MANUAL


I should also mention that the Seam debug page does not have the FullTextEntityManager, or any EntityManager for that matter, in any context.

Again, any insight into how to control the commit of the FTEM and the closing of the session is much appreciated.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 08, 2008 1:06 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
A conversation has to be started and ended, are you marking the conversation boundaries?

FTEM lifecycle is exactly the same as a regular EM. Actually FTEM, is sjust a wrapper on top of EM

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 08, 2008 1:52 pm 
Regular
Regular

Joined: Fri Oct 05, 2007 1:17 pm
Posts: 78
See, that's the confusing thing. I am indeed in the midst of a conversation. The action running the show is in conversational scope. The object that is holding onto the FTEM is in the conversation context. And at no point in the interaction I describe is the conversation ended.

I have even verified that the object holding onto the FTEM is in the conversational context through a SeamTest.

Perhaps some code is in order.

Here is the relevant part of the action:

Code:
@Name("searchAction")
@Scope(value = ScopeType.CONVERSATION)
public class SearchAction extends Object implements Serializable {
.
.
.
   @In(create = true)
   @Out(scope = ScopeType.CONVERSATION)
   private Search search;
}



Now the component called "search" is the one that actually uses Hibernate Search to look for widgets. It therefore has the FTEM with which to do that search. Here is the relevant part of that class:

Code:
@Name("search")
public class SearchImpl extends Object implements Search, Serializable {
.
.
.
   @In
   private FullTextEntityManager em;
.
.
.
}


Finally, I just checked the Seam Debug page. It actually seems like everything is in the conversation context as it should be.

Here is the FTEM:

Code:
Component (em)
class class org.jboss.seam.persistence.ManagedPersistenceContext
componentName em
entityManager org.jboss.seam.persistence.FullTextEntityManagerProxy@1c29764
entityManagerFactory org.jboss.seam.core.Expressions$1@1c28d46
entityManagerFactoryFromJndiOrValueBinding org.hibernate.ejb.EntityManagerFactoryImpl@240acac
filters []
persistenceUnitJndiName java:/em
toString() ManagedPersistenceContext(java:/em)


And here is the "search" component:

Code:
Component (search)
class class gov.nist.srm.persistence.search.SearchImpl_$$_javassist_1
component Component(srmSearch)
currentPage 0
description   
keyWords 
name test
number 
resultsPerPage 10
totalResultCount 1
toString() com.myapp.persistence.search.SearchImpl@1c4a001

Since everything is in the conversation context and the conversation isn't over, why is the Session closed?

Any insight here is appreciated. I know I can probably apply some hack to work around this, but this seems like something I would like to solve correctly as soon as possible.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 08, 2008 1:53 pm 
Regular
Regular

Joined: Fri Oct 05, 2007 1:17 pm
Posts: 78
Oh and here is a snippet of the log:

Code:
06 Mar 2008 14:41:23,662: DEBUG org.hibernate.transaction.JDBCTransaction - commit
06 Mar 2008 14:41:23,662: DEBUG org.hibernate.impl.SessionImpl - automatically flushing session
06 Mar 2008 14:41:23,662: DEBUG org.hibernate.jdbc.JDBCContext - before transaction completion
06 Mar 2008 14:41:23,662: DEBUG org.hibernate.impl.SessionImpl - before transaction completion
06 Mar 2008 14:41:23,662: DEBUG org.hibernate.transaction.JDBCTransaction - re-enabling autocommit
06 Mar 2008 14:41:23,662: DEBUG org.hibernate.transaction.JDBCTransaction - committed JDBC Connection
06 Mar 2008 14:41:23,662: DEBUG org.hibernate.jdbc.JDBCContext - after transaction completion
06 Mar 2008 14:41:23,662: DEBUG org.hibernate.jdbc.ConnectionManager - aggressively releasing JDBC connection
06 Mar 2008 14:41:23,662: DEBUG org.hibernate.jdbc.ConnectionManager - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
06 Mar 2008 14:41:23,662: DEBUG org.hibernate.impl.SessionImpl - after transaction completion
06 Mar 2008 14:41:23,672: DEBUG org.hibernate.transaction.JDBCTransaction - begin
06 Mar 2008 14:41:23,672: DEBUG org.hibernate.jdbc.ConnectionManager - opening JDBC connection
06 Mar 2008 14:41:23,672: DEBUG org.hibernate.transaction.JDBCTransaction - current autocommit status: true
06 Mar 2008 14:41:23,672: DEBUG org.hibernate.transaction.JDBCTransaction - disabling autocommit
06 Mar 2008 14:41:23,672: DEBUG org.hibernate.jdbc.JDBCContext - after transaction begin
06 Mar 2008 14:41:23,672: DEBUG org.hibernate.impl.SessionImpl - setting flush mode to: MANUAL


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 09, 2008 6:57 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
give it a try with a regular criteria query, I expect the same to happen.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 10, 2008 12:10 am 
Regular
Regular

Joined: Fri Oct 05, 2007 1:17 pm
Posts: 78
No need, Emmanuel. I trust you!

My next order of business is figuring out why my conversation is ending, so this is definitely more an issue of my understanding of Seam than a Hibernate Search issue.

I just got a tip that the Seam debug page might be swallowing an exception that causes a rollback and therefore ends the conversation.

Thanks.


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