-->
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.  [ 2 posts ] 
Author Message
 Post subject: Help! hsearch and spring give Session is closed Exception
PostPosted: Thu Mar 06, 2008 6:01 am 
Newbie

Joined: Thu Mar 06, 2008 5:16 am
Posts: 1
This is my project environment:


Code:
jdk 1.6

spring 2.5.2
hibernate-entitymanager 3.3.1.GA
hibernate-annotations 3.3.0.GA
hibernate-core 3.2.6.GA

hibernate-search 3.0.1.GA
lucene-core 2.3.0


I am trying for hibernate search and spring.

My service class:

Code:
private EntityManager entityManager;
private FullTextEntityManager searchManager;
@PersistenceContext
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}

public FullTextEntityManager getSearchManager() {
if (null == searchManager) {
searchManager = Search.createFullTextEntityManager(getEntityManage r());
}
return searchManager;
}

public Page<T> search(Page page, String keywords, String[] fields, SortField... sortFields) {
FullTextQuery query = createSearchQuery(keywords, fields, sortFields);
int resultSize = query.getResultSize();
page.setResultCount(resultSize);
if (resultSize < 1) return page;
page.setItems(query.setFirstResult(page.getFirstRe sult()).setMaxResults(page.getSize()).getResultLis t());
return page;
}
public FullTextQuery createSearchQuery(String keywords, String[] fields, Analyzer analyzer, Class... classes) {
QueryParser parser = fields.length > 1 ? new MultiFieldQueryParser(fields, analyzer) : new QueryParser(fields[0], analyzer);
org.apache.lucene.search.Query query;
try {
query = parser.parse(keywords);
} catch (ParseException e) {
throw new org.gaozou.kevin.utility.SimpleException("Could not parse keywords: " + keywords, e);
}
return getSearchManager().createFullTextQuery(query, classes);
}


My applicationContext.xml:

Code:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityMana gerFactoryBean">
<property name="persistenceUnitName" value="tvlist"/>
</bean>

<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionM anager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* org..manager..*(..)) or execution(* org..*Manager*.*(..)) or execution(* org..*DAO*.*(..))"/>
</aop:config>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="list*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="page*" read-only="true"/>
<tx:method name="search*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>


The search works at once,
but when I try to search second time it gives this Exception:


Code:
org.hibernate.SessionException: Session is closed!
org.hibernate.impl.AbstractSessionImpl.errorIfClos ed(AbstractSessionImpl.java:49)
org.hibernate.impl.SessionImpl.createCriteria(Sess ionImpl.java:1508)
org.hibernate.search.engine.QueryLoader.load(Query Loader.java:62)
org.hibernate.search.query.FullTextQueryImpl.list( FullTextQueryImpl.java:246)
org.hibernate.search.jpa.impl.FullTextQueryImpl.ge tResultList(FullTextQueryImpl.java:91)


When I change @PersistenceContext to @PersistenceContext(type=PersistenceContextType.EX TENDED), it works.
But the 'extended EntityManager' is not thread-safe, so it's better to use @PersistenceContext.


Please any one help me to resolve this problem.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 08, 2008 12:56 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
basically your service is injected with 2 different EMs, but since you keep around the FTEM form the old EM, you have a session closed exception.
One way to avoid that is to wrap the EM into a FTEM each time

_________________
Emmanuel


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