-->
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.  [ 5 posts ] 
Author Message
 Post subject: Indexing method not terminating
PostPosted: Tue Dec 27, 2011 2:30 am 
Newbie

Joined: Wed Apr 11, 2007 8:48 am
Posts: 13
Hello

I have a hibernate and spring capable application and am using Hibernate Search 3.1.1 GA along with Hibernate Annotations 3.4 using MyEclipse 9.0. There is a POJO in my project which is Indexed. When i am creating indexing on the data which is residing in database for this POJO, the indexes are created. However the program does not terminate once the indexes are created.

Code:
public void createIndexes(){
org.hibernate.Session session = getHibernateTemplate().getSessionFactory().openSession();
   try {
      FullTextSession fullTextSession = Search.getFullTextSession(session);
      Transaction tx = fullTextSession.beginTransaction();
      ArrayList projections = (ArrayList) session.createQuery("select user from User user").list();
      for (Iterator iterator = projections.iterator(); iterator.hasNext();) {
          User user = (User) iterator.next();
          fullTextSession.index(user);
      }
         tx.commit(); //index are written at commit time
      } catch (Exception e) {
         e.printStackTrace();
                }
      finally {
           session.close();
                     System.out.println("All done.");
       }
   }


I have to manually terminate the program, once i get to see the print statements which i have written in the finally clause. Any thoughts..

Thanks


Top
 Profile  
 
 Post subject: Re: Indexing method not terminating
PostPosted: Tue Dec 27, 2011 4:36 am 
Newbie

Joined: Wed Apr 11, 2007 8:48 am
Posts: 13
If however i remove the following:

Code:
Transaction tx = fullTextSession.beginTransaction();

and
Code:
tx.commit();


The program terminates after the method execution is finished. But then upon inspecting the indexes using Luke, i found out that the indexes were not written because the transaction was not committed.


Top
 Profile  
 
 Post subject: Re: Indexing method not terminating
PostPosted: Wed Dec 28, 2011 9:19 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi,
do you close the Hibernate SessionFactory ?
The indexing backend uses threads to manage background indexing and they need to be shutdown properly; avoiding to commit the transaction is not triggering indexing at all, so the threads might not have started yet.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Indexing method not terminating
PostPosted: Thu Dec 29, 2011 2:12 am 
Newbie

Joined: Wed Apr 11, 2007 8:48 am
Posts: 13
Hello

Thanks for your reply. I was not closing the Hibernate SessionFactory myself i.e. in my code, also there is declarative transaction management in my application.. that is why i never handled the transactions anywhere in the application. But for this indexing method i am calling the session.beginTransacation() and transaction.commit() methods because otherwise the indexes are not committed if i do not commit the transaction. I will paste the relevant portion of my application context file here as well:

Code:
<tx:annotation-driven transaction-manager="txManager"/>

<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
      <property name="transactionManager" ref="txManager"/>
</bean>
   
<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
   
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
      <property name="configLocation" value="classpath:/hibernate.cfg.xml">   </property>
</bean>


After reading your reply, i closed the Hibernate SessionFactory after closing the session i.e:

Code:
finally{
  session.close();
  getSessionFactory().close();
}


After this my program terminates after the completion of method and the indexes are written as well. But now after i run search on the indexed data i get an exception i.e:
Code:
org.apache.lucene.store.AlreadyClosedException: this IndexReader is closed
   at org.apache.lucene.index.IndexReader.ensureOpen(IndexReader.java:196)
   at org.apache.lucene.index.DirectoryIndexReader.reopen(DirectoryIndexReader.java:143)
   at org.hibernate.search.reader.SharingBufferReaderProvider$PerDirectoryLatestReader.refreshAndGet(SharingBufferReaderProvider.java:242)
   at org.hibernate.search.reader.SharingBufferReaderProvider.openReader(SharingBufferReaderProvider.java:123)
   at org.hibernate.search.query.FullTextQueryImpl.buildSearcher(FullTextQueryImpl.java:718)
   at org.hibernate.search.query.FullTextQueryImpl.list(FullTextQueryImpl.java:288)
   at com.sime.geoserv.server.dao.map.ProjectionDAO.searchResultl(UserDAO.java:249)
   at com.sime.geoserv.server.services.MapService.searchResultl(UserService.java:799)
   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:597)


Here is the search method:
Code:
org.hibernate.Session session = getHibernateTemplate().getSessionFactory().openSession();
String searchQuery = "john";
String[] searchFields = {"name"};
   try {
      QueryParser parser = new MultiFieldQueryParser(searchFields, new StandardAnalyzer());
      org.apache.lucene.search.Query luceneQuery;
      luceneQuery = parser.parse(searchQuery);
      FullTextSession ftSession = Search.getFullTextSession(session);
      org.hibernate.Query query = ftSession.createFullTextQuery(luceneQuery);
      List<Layerattribute> results = [color=#FF0000]query.list(); //<----- This is where the exception occurs.
      for (Iterator iterator = results.iterator(); iterator.hasNext();) {
         User user = (User) iterator.next();
      }
   }


Can you please guide as to what should be done to solve this issue?


Top
 Profile  
 
 Post subject: Re: Indexing method not terminating
PostPosted: Thu Dec 29, 2011 9:14 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi, you should close the SessionFactory not after each usage but when your program is about to shutdown, so after the last query is performed.

How that should work, depends on what kind of program it is: for a webapplication in a container, when using an EntityManager for example the lifecycle would be managed for you by the container. In case it's a custom program, you will likely have a "boot up" initialization routine, and a simmetric "shutdown" and cleanup phase.

When using Spring you should make sure all your services are properly shutdown as well, as documented here: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-shutdown. Just make sure that as you start the Hibernate SessionFactory, someone cleans it up properly as well.

_________________
Sanne
http://in.relation.to/


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