0

I'm receiving the following error while using session.clear() in my batch transaction with hibernate.

org.hibernate.HibernateException: Error while indexing in Hibernate Search (before transaction completion)

I'm using the following code

    try {
        for (TimeSheetEntity timeSheet : timeSheets) {
            timeSheet.setActive(false);
            try {
                session.update(timeSheet);

                count++;

                if (count % 250 == 0 || totalCount == count) {
                    System.out.println(count);

                    session.flush();
                    session.clear();
                }
            } catch (HibernateException ex) {
                logger.error(ArchiveImpl.class.getName(), ExceptionUtils.getStackTrace(ex) + "[" + timeSheet.getId() + "] ");
            } catch (Exception ex) {
                logger.error(ArchiveImpl.class.getName(), ExceptionUtils.getStackTrace(ex) + "[" + timeSheet.getId() + "] ");
            }
        }

        sessionManager.commit();
    } catch (HibernateException ex) {
        System.out.println(ex);
        logger.error(ArchiveImpl.class.getName(), ExceptionUtils.getStackTrace(ex));
        sessionManager.abort();
    } finally {
    }

Now if I remove session.clear(); everything works as it's suppose to. Any ideas why I can't use clear within my batch transaction with hibernate search?

2 Answers 2

3

The method Session#flush() flushes changes to the database. Changes to the index managed by Hibernate Search are flushed by using FullTextSession#flushToIndexes().

If you need to clear() your session, you should flush both of these, or some entities will still be enqueued for indexing but won't be managed anymore, which is a problem.

Hibernate Search won't automatically flush to indexes when you do a flush to database, as a flush to the indexes will not be undone in case you abort the transaction, so it's better if you decide explicitly which one (or both) of the flushes you want to happen.

Sign up to request clarification or add additional context in comments.

1 Comment

Exactly the answer I was looking for, worked perfectly thanks.
0

Well, your call to session.clear(); is within the for loop. If you want to close the session, do that after the loop has ended.

3 Comments

That doesn't entirely make sense to me, if you don't call session.clear in the for loop, you run the risk of an out of memory exception. Session.clear() should be just clearing the L1 cache, not closing the session right? I have other apps where I do this exact same thing and I have no issues. The only thing I can think of is perhaps relational tables that are being lazy loaded might be the culprit? Example of a batch insert stackoverflow.com/questions/3788048/…
Then have you tried using a transaction for your update operations?
Yes, this is handled by the frameworks session manager tapestry.apache.org/5.3/apidocs/org/apache/tapestry5/hibernate/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.