-->
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.  [ 4 posts ] 
Author Message
 Post subject: error:getListeners is not valid without active transaction
PostPosted: Thu Dec 31, 2009 9:02 am 
Newbie

Joined: Thu Dec 31, 2009 8:54 am
Posts: 2
Hi ,I'm using hibernate search to study full text search. When I run my first app, I got the error below:
Exception in thread "main" org.hibernate.HibernateException: getListeners is not valid without active transaction
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:338)
at $Proxy9.getListeners(Unknown Source)
at org.hibernate.search.backend.impl.EventSourceTransactionContext.getIndexWorkFlushEventListener(EventSourceTransactionContext.java:78)
at org.hibernate.search.backend.impl.EventSourceTransactionContext.<init>(EventSourceTransactionContext.java:41)
at org.hibernate.search.impl.FullTextSessionImpl.<init>(FullTextSessionImpl.java:75)
at org.hibernate.search.Search.getFullTextSession(Search.java:23)
at com.v512.examples.HibernateSearchTest.indexGuestbook(HibernateSearchTest.java:17)
at com.v512.examples.HibernateSearchTest.main(HibernateSearchTest.java:59)


my app:
Code:
Session session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();

      FullTextSession ftSession = org.hibernate.search.Search.getFullTextSession(session);//this line cause the above error!!!!
      ftSession.getTransaction().begin();
      List<Guestbook> guestbooks = session.createQuery("from Guestbook").list();
      for (Guestbook gb : guestbooks) {
         ftSession.index(gb); // 手工方式,把所有持久化对象进行索引
      }
      ftSession.getTransaction().commit();

hibernate.cfg.xml:
<property name="current_session_context_class">thread</property>
<mapping resource="com/v512/examples/Guestbook.hbm.xml" />
<event type="post-update">
<listener class="org.hibernate.search.event.FullTextIndexEventListener" />
</event>
<event type="post-insert">
<listener class="org.hibernate.search.event.FullTextIndexEventListener" />
</event>
<event type="post-delete">
<listener class="org.hibernate.search.event.FullTextIndexEventListener" />
</event>
<event type="post-collection-recreate">
<listener class="org.hibernate.search.event.FullTextIndexEventListener" />
</event>
<event type="post-collection-remove">
<listener class="org.hibernate.search.event.FullTextIndexEventListener" />
</event>
<event type="post-collection-update">
<listener class="org.hibernate.search.event.FullTextIndexEventListener" />
</event>
my lib version:
Quote:
hibernate core:3.3.2 GA
hibernate search:3.1.1 GA


who knows reason? thanks.


Top
 Profile  
 
 Post subject: Re: error:getListeners is not valid without active transaction
PostPosted: Thu Dec 31, 2009 9:12 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi,
just begin the transaction before getFullTextSession(), exactly as the error message suggests:
Code:
Session session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();
session.getTransaction().begin();
FullTextSession ftSession = org.hibernate.search.Search.getFullTextSession(session);
List<Guestbook> guestbooks = session.createQuery("from Guestbook").list();
for (Guestbook gb : guestbooks) {
   ftSession.index(gb);
}
ftSession.getTransaction().commit();


keep in mind that session and ftSession are similar, fstSession just wraps the other by adding functionality:
session.getTransaction() == ftSession.getTransaction()

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


Top
 Profile  
 
 Post subject: Re: error:getListeners is not valid without active transaction
PostPosted: Sun Jan 03, 2010 8:24 am 
Newbie

Joined: Thu Dec 31, 2009 8:54 am
Posts: 2
s.grinovero wrote:
Hi,
just begin the transaction before getFullTextSession(), exactly as the error message suggests:
Code:
Session session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();
session.getTransaction().begin();
FullTextSession ftSession = org.hibernate.search.Search.getFullTextSession(session);
List<Guestbook> guestbooks = session.createQuery("from Guestbook").list();
for (Guestbook gb : guestbooks) {
   ftSession.index(gb);
}
ftSession.getTransaction().commit();


keep in mind that session and ftSession are similar, fstSession just wraps the other by adding functionality:
session.getTransaction() == ftSession.getTransaction()


thank s.grinovero for your reply. Why does it need open a session transaction explicitly? I found closing the transaction can't be done with a error saying " Transaction not successfully started", here is the code:
Code:
Session session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();

      Transaction tx=session.getTransaction();
      tx.begin();
      FullTextSession ftSession = org.hibernate.search.Search.getFullTextSession(session);
      ftSession.getTransaction().begin();
      List<Guestbook> guestbooks = session.createQuery("from Guestbook").list();
      for (Guestbook gb : guestbooks) {
         ftSession.index(gb); // 手工方式,把所有持久化对象进行索引
      }
      
      ftSession.getTransaction().commit();
      
      //my code start
      tx.commit();
      session.getSessionFactory().close();
      //my code end


Top
 Profile  
 
 Post subject: Re: error:getListeners is not valid without active transaction
PostPosted: Sun Jan 03, 2010 8:36 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
Why does it need open a session transaction explicitly?

A very simple reason, because of
Quote:
<property name="current_session_context_class">thread</property>

your Session gets wrapped in a org.hibernate.context.ThreadLocalSessionContext which is mandating an open transaction before any method on Session is allowed to get called.
Search.getFullTextSession(session) is calling a method on the Session, which is not allowed...
See https://forum.hibernate.org/viewtopic.php?f=9&t=998336 for more info; I think we will improve on this on some way. At the moment you have to either start a transaciton before, or don't scope the sessions to threads.

In the code shown in your last post you're committing twice the same transaction, that's not allowed.

_________________
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.  [ 4 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.