-->
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.  [ 12 posts ] 
Author Message
 Post subject: A question about getFullTextSession method
PostPosted: Wed Jul 15, 2009 1:14 pm 
Newbie

Joined: Wed Jul 15, 2009 12:34 pm
Posts: 18
Hi there, in Hibernate Search 3.1 online documentation we can see code samples like this:

Code:
FullTextSession fullTextSession = Search.getFullTextSession(session);
Transaction tx = fullTextSession.beginTransaction();
for (Customer customer : customers) {
    fullTextSession.index(customer);
}
tx.commit(); //index are written at commit time   


However, getFullTextSession method requires an active transaction, doesn't it? The above code won't execute, right? We need something like:

Code:
Transaction tx = session.beginTransaction();
FullTextSession fullTextSession = Search.getFullTextSession(session);
for (Customer customer : customers) {
    fullTextSession.index(customer);
}
tx.commit(); //index are written at commit time   


Is this the correct way to use the getFullTextSession? In Hibernate Search 3.0, we didn't need to begin a transaction before creating a FullTextSession with the createFullTextSession method.


Top
 Profile  
 
 Post subject: Re: A question about getFullTextSession method
PostPosted: Fri Jul 17, 2009 5:37 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
The first example is correct. You don't need a running transaction to get hold of a FullTextSession. What makes you believe that the code is not correct?

--Hardy


Top
 Profile  
 
 Post subject: Re: A question about getFullTextSession method
PostPosted: Fri Jul 17, 2009 9:02 pm 
Newbie

Joined: Wed Jul 15, 2009 12:34 pm
Posts: 18
Because, if I try to run the first example, I get this:

Code:
org.hibernate.HibernateException: getListeners is not valid without active transaction
   at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:338)
   at $Proxy37.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)
        ....


Top
 Profile  
 
 Post subject: Re: A question about getFullTextSession method
PostPosted: Sat Jul 18, 2009 5:34 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hmm, what type of session are you using? It seems you are using a transaction bound session outside a transaction. Have a look at this post viewtopic.php?t=972658

You also might find this general discussion of Hibernate's sesion management interesting: https://www.hibernate.org/42.html

So it all depends on your environment and setup you are trying to run in.

--Hardy


Top
 Profile  
 
 Post subject: Re: A question about getFullTextSession method
PostPosted: Sat Jul 18, 2009 1:14 pm 
Newbie

Joined: Wed Jul 15, 2009 12:34 pm
Posts: 18
I think I still didn't get it. I'm using ThreadLocalSessionContext and I'm using the HibernateUtil approach to get access to the SessionFactory. I'm not running my program in a container. I didn't have any problems until I upgraded to Hibernate Search 3.1.1.


Top
 Profile  
 
 Post subject: Re: A question about getFullTextSession method
PostPosted: Tue Jul 21, 2009 9:39 pm 
Newbie

Joined: Wed Jul 15, 2009 12:34 pm
Posts: 18
Funny thing is that I tested Hibernate Search 3.1.0, and I don't get the exception. It's only with 3.1.1. Why is that?


Top
 Profile  
 
 Post subject: Re: A question about getFullTextSession method
PostPosted: Wed Jul 22, 2009 5:17 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

I talked to Sanne and he thinks your problem could be related to HSEARCH-178

We will look into it.

--Hardy


Top
 Profile  
 
 Post subject: Re: A question about getFullTextSession method
PostPosted: Wed Jul 22, 2009 5:26 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi,
could you post the versions of other Hibernate libs involved?
I'd like to solve this but have never seen an error like
Quote:
org.hibernate.HibernateException: getListeners is not valid without active transaction

so I need to be able to reproduce first.

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


Top
 Profile  
 
 Post subject: Re: A question about getFullTextSession method
PostPosted: Wed Jul 22, 2009 7:58 pm 
Newbie

Joined: Wed Jul 15, 2009 12:34 pm
Posts: 18
The other Hibernate libs versions I'm using are:

Hibernate Core 3.3.1 GA
Hibernate Annotations 3.4.0 GA
Hibernate Validator 3.1.0 GA

So the exception occurs with Hibernate Search 3.1.1 GA whenever I have code like this:

Code:
Session session = HibernateUtil.getSessionFactory.getCurrentSession();
FullTextSession fullTextSession = Search.getFullTextSession(session);
Transaction tx = fullTextSession.beginTransaction();
for (Customer customer : customers) {
    fullTextSession.index(customer);
}
tx.commit(); //index are written at commit time   


The same code works fine in version 3.1.0 GA.


Top
 Profile  
 
 Post subject: Re: A question about getFullTextSession method
PostPosted: Wed Dec 16, 2009 8:28 am 
Newbie

Joined: Wed Dec 16, 2009 8:21 am
Posts: 1
Session session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();

Transaction tx = session.beginTransaction();

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();
tx.commit();



I have the same problem with you .
Exception:
Exception in thread "main" org.hibernate.HibernateException: getListeners is not valid without active transaction.
What can i do next ?


Top
 Profile  
 
 Post subject: Re: A question about getFullTextSession method
PostPosted: Wed Dec 16, 2009 10:49 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hello, I think I found the reason for this error.
This call is not legal when using a thread bound session.
I just found this javadoc on org.hibernate.context.ThreadLocalSessionContext, which is the kind of Session I guess you're using?
Quote:
A {@link CurrentSessionContext} impl which scopes the notion of current
session by the current thread of execution. Unlike the JTA counterpart,
threads do not give us a nice hook to perform any type of cleanup making
it questionable for this impl to actually generate Session instances. In
the interest of usability, it was decided to have this default impl
actually generate a session upon first request and then clean it up
after the {@link org.hibernate.Transaction} associated with that session
is committed/rolled-back. In order for ensuring that happens, the sessions
generated here are unusable until after {@link Session#beginTransaction()}
has been called
. If <tt>close()</tt> is called on a session managed by
this class, it will be automatically unbound.

So this explains that, when using threadbound Sessions, you should always wrap it all in transactions, this includes the call to Search.getFullTextSession(session).

You're not experiencing this same problem in 3.1.0 because in 3.1.1 there was added a check to verify that you are actually using transactions, as many other people where having trouble because they forgot to start transactions, even when making changes to entities.
So sorry for the trouble in 3.1.1, the correct solution is to use transactions!

Hardy, do you think we could improve on this? The ThreadLocalSessionContext could be less strict on this checks?

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


Top
 Profile  
 
 Post subject: Re: A question about getFullTextSession method
PostPosted: Tue Dec 22, 2009 10:17 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
If we want to change something in ThreadLocalSessionContext we should talk with Steve to make sure there are no side effects. What we should definitely do so is to update the documentation or add some warning/note. HSEARCH-437.


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