-->
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: jdbc connection not being released after indexing
PostPosted: Wed Mar 16, 2011 11:40 am 
Newbie

Joined: Wed Jan 06, 2010 6:34 am
Posts: 8
Location: Amsterdam (NL)
Hi
I have a session leaking (Oracle DB, Hibernate Search 3.3.0) after a manual re-indexing.

In my app I run a Lucene index update every 60 min using a Java thread

Indexing code:
Code:
private void batchIndex(Criteria query, Integer batchSize) {
        Session session = null;
        int batch = 0;

        try {
            session = getSession();  // using  SessionFactoryUtils.getSession(sessionFactory, true)

            FullTextSession fullTextSession = Search.getFullTextSession(session);

            query.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY)
                    .setCacheMode(CacheMode.IGNORE)
                    .setFetchSize(batchSize)
                    .setFlushMode(FlushMode.MANUAL);

            ScrollableResults scroll = query.scroll(ScrollMode.FORWARD_ONLY);

            batch = 0;
            while (scroll.next()) {
                batch++;
                fullTextSession.index(scroll.get(0)); //indexing of a single entity
                if (batch % batchSize == 0) {
                    // commit batch
                    fullTextSession.flushToIndexes();
                    fullTextSession.clear();
                }
            }

            // flush what is left
            fullTextSession.flushToIndexes();
            fullTextSession.clear();

            // close to release closing DB open cursor
            scroll.close();

        } finally {
            try {
                // close to release JDBC connection
                //closeSession(session);  // using  SessionFactoryUtils.closeSession
            } catch (Exception e) {
                logger.warn("AbstractIndexer.batchIndex - Exception while closing the Hibernate Session");
                logger.warn(e);
            }
        }

        logger.info("Indexing work completed  for " + getIndexedClass() + ". " + batch + " entity indexed.");
    }




Whenever the index is not updated (no db changes) I can see the jdbc connection being released:
Code:
2011-03-16 16:24:22 INFO  indexer.AbstractIndexer -> Indexing work completed  for class com.sapienza.dccm.model.entity.Document. 0 entity indexed.
2011-03-16 16:24:22 DEBUG jdbc.ConnectionManager -> releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
2011-03-16 16:24:22 DEBUG jdbc.ConnectionManager -> transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!

if there is a change (therefore an update to Lucene index) the JDBC connection doesnt seem to be returned back to the pool...

If I un-comment //closeSession(session) then the JDBC connection seems to go back to the pool but in this case (still figuring out why) my thread dies.

My question:
- why do I need to close the session manually in the second case only (index update is required) but doesnt seem to be the same in the first case?

Tnx in advance

Beppe


Top
 Profile  
 
 Post subject: Re: jdbc connection not being released after indexing
PostPosted: Wed Mar 16, 2011 12:40 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi Beppe,
welcome.
If you don't close the Session, I'm not surprised that it doesn't get back to the pool.

You should investigate why your thread dies. Wrap the whole body of your thread with a try/catch statement able to log all kind of errors, usually if a thread dies it's because some exception occurred: not being your main thread, you won't get a stacktrace.

I think it might have something to do with the way you get the Session.
I don't know what this means:
Code:
SessionFactoryUtils.getSession(sessionFactory, true)

?
make sure that the "getSession" is actually opening a new Session for you. otherwise the close() will prevent the second indexing from working fine.

Did you try the MassIndexer ? should be much easier than to deal with your own threads, and is likely faster too.

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


Top
 Profile  
 
 Post subject: Re: jdbc connection not being released after indexing
PostPosted: Wed Mar 16, 2011 5:03 pm 
Newbie

Joined: Wed Jan 06, 2010 6:34 am
Posts: 8
Location: Amsterdam (NL)
Ciao Sanne :-)

I need to update incrementally the index every hour (whilst the app is running) so I dont think the MassIndexer suits me.

Tnx for the advice, I am looking into my thread now, it is possibly the way I open the session: SessionFactoryUtils.getSession(sessionFactory, true) .. I am using the getSession() method from SessionFactoryUtils, which returns the session bound to thread, it doesnt seem right like you say, I would lose the session after the first iteration

Beppe


Top
 Profile  
 
 Post subject: Re: jdbc connection not being released after indexing
PostPosted: Wed Mar 16, 2011 7:07 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
yes that must be it,
fammi sapere come va :)

vriendelijke groeten,

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


Top
 Profile  
 
 Post subject: Re: jdbc connection not being released after indexing
PostPosted: Fri Mar 18, 2011 6:58 am 
Newbie

Joined: Wed Jan 06, 2010 6:34 am
Posts: 8
Location: Amsterdam (NL)
That did it :-)

I open a new session and process my index updates, closing the session (finally), a bit of code cleaning and refactoring, now it is looking better

Grazie ancora ;-)

Beppe


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.