I am using Hibernate to query my MySQL database to find all of the segments in a particular journey. (For example a journey from A->D might have segments A->B, B->C, C->D.)
I run this query very frequently and it completes very quickly. The database is indexed in such a way that performance is very good. The code for the query is as follows:
public List<Segment> getSegmentsForUID(String trainUid) {
Query segmentQuery = session.createQuery("select segment from Segment segment inner join segment.journey as journey where segment.journey " +
"in (:journies) and journey.trainUid = '" + trainUid + "'");
segmentQuery.setParameterList("journies", dailyJournies);
queryCount++;
return segmentQuery.getResultList();
}
Where dailyJournies
is set in the initilisation of the class and is never changed again.
My problem is that after doing this query many times (usually in the region between 2,000 and 3,000 queries) I always receive a:
Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded
on the return segmentQuery.getResultList();
line.
Since no other part of this class is being touched after initilization and since the exception is always thrown on this line, I can't imagine it being a problem elsewhere.
My original thought after a bit of digging was that the Hibernate session was growing too large so I started to close and reopen the session after each 1,000 queries but this didn't make any difference. I've also tried using the following code:
Cache cache = session.getSessionFactory().getCache();
cache.evictAllRegions();
To clear the session cache manually but this didn't do anything either.
Has anyone else come across this before or is there something glaringly obvious that I'm missing?