Skip to content

Commit

Permalink
HSEARCH-2920 Correctly handle session closing in BeforeChunkBatchlet
Browse files Browse the repository at this point in the history
The stop() method will not be called systematically at the end, it will
just be called when the user explicitly requests to stop the job.
So we should take care of closing the session through other means.
  • Loading branch information
yrodiere authored and Sanne committed Oct 25, 2017
1 parent 84aef80 commit fd0249c
Showing 1 changed file with 8 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.hibernate.search.jsr352.massindexing.impl.JobContextData;
import org.hibernate.search.jsr352.massindexing.impl.util.PersistenceUtil;
import org.hibernate.search.jsr352.massindexing.impl.util.SerializationUtil;
import org.hibernate.search.util.impl.Closer;
import org.hibernate.search.util.logging.impl.LoggerFactory;

import static org.hibernate.search.jsr352.massindexing.MassIndexingJobParameters.OPTIMIZE_AFTER_PURGE;
Expand Down Expand Up @@ -50,9 +51,6 @@ public class BeforeChunkBatchlet extends AbstractBatchlet {
@BatchProperty(name = MassIndexingJobParameters.TENANT_ID)
private String tenantId;

private Session session;
private FullTextSession fts;

@Override
public String process() throws Exception {
boolean purgeAllOnStart = SerializationUtil.parseBooleanParameter( PURGE_ALL_ON_START, serializedPurgeAllOnStart );
Expand All @@ -61,20 +59,16 @@ public String process() throws Exception {
if ( purgeAllOnStart ) {
JobContextData jobData = (JobContextData) jobContext.getTransientUserData();
EntityManagerFactory emf = jobData.getEntityManagerFactory();
session = PersistenceUtil.openSession( emf, tenantId );
fts = Search.getFullTextSession( session );
jobData.getEntityTypes().forEach( clz -> fts.purgeAll( clz ) );
try (Session session = PersistenceUtil.openSession( emf, tenantId )) {
FullTextSession fts = Search.getFullTextSession( session );
jobData.getEntityTypes().forEach( clz -> fts.purgeAll( clz ) );

if ( optimizeAfterPurge ) {
log.startOptimization();
ContextHelper.getSearchIntegrator( session ).optimize();
if ( optimizeAfterPurge ) {
log.startOptimization();
ContextHelper.getSearchIntegrator( session ).optimize();
}
}
}
return null;
}

@Override
public void stop() throws Exception {
session.close();
}
}

0 comments on commit fd0249c

Please sign in to comment.