StatelessSession fills up StatefulPersistenceContext

I’m using a StatelessSession to read and process millions of entries.

Surprisingly (to me) the StatelessSession is backed by a StatefulPersistenceContext which is getting filled up with entries, eventually using up all the memory.

Is there a “best way” to flush/clear the StatefulPersistenceContext?
There is no way through the StatelessSession interface.

I’m using Hibernate 5.1.10.Final (through GORM 6.1.8)

Thanks in advance!

The temporaryPersistenceContext is defined like this:

private PersistenceContext temporaryPersistenceContext = new StatefulPersistenceContext( this );

And, after every operation, it’s cleared:

public List list(String query, QueryParameters queryParameters) throws HibernateException {
	HQLQueryPlan plan = getQueryPlan( query, false );
	boolean success = false;
	List results = Collections.EMPTY_LIST;
	try {
		results = plan.performList( queryParameters, this );
		success = true;
	finally {
		afterOperation( success );
	return results;

If you have a use case where this one is not cleared, you should create a replicating test case and open a Jira issue with the test case attached to it.

1 Like

You are of course absolutely correct!

Another place in our codebase a query was created by:

Query query = sessionFactory.currentSession.createQuery(queryString)

Expecting the currentSession to be a previously defined StatelessSession.

Thanks for your quick reply.
I’m sorry for the inconvenience.