BATCH-538:Stateful sessions are now closed properly in HibernateCursorItemReader.
This commit is contained in:
@@ -102,8 +102,8 @@ public class HibernateCursorItemReader extends ExecutionContextUserSupport imple
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (statelessSession != null) {
|
||||
statelessSession.close();
|
||||
if (statefulSession != null) {
|
||||
statefulSession.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package org.springframework.batch.item.database;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.easymock.MockControl;
|
||||
import org.hibernate.Query;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.classic.Session;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
|
||||
/**
|
||||
* Tests for {@link HibernateCursorItemReader} using standard hibernate {@link Session}.
|
||||
@@ -13,4 +17,32 @@ public class HibernateCursorItemReaderStatefulIntegrationTests extends Hibernate
|
||||
return false;
|
||||
}
|
||||
|
||||
//Ensure close is called on the stateful session correctly.
|
||||
public void testStatfulClose(){
|
||||
|
||||
MockControl sessionFactoryControl = MockControl.createControl(SessionFactory.class);
|
||||
SessionFactory sessionFactory = (SessionFactory) sessionFactoryControl.getMock();
|
||||
MockControl sessionControl = MockControl.createControl(Session.class);
|
||||
Session session = (Session) sessionControl.getMock();
|
||||
MockControl resultsControl = MockControl.createControl(Query.class);
|
||||
Query scrollableResults = (Query) resultsControl.getMock();
|
||||
HibernateCursorItemReader itemReader = new HibernateCursorItemReader();
|
||||
itemReader.setSessionFactory(sessionFactory);
|
||||
itemReader.setQueryString("testQuery");
|
||||
itemReader.setUseStatelessSession(false);
|
||||
|
||||
sessionFactory.openSession();
|
||||
sessionFactoryControl.setReturnValue(session);
|
||||
session.createQuery("testQuery");
|
||||
sessionControl.setReturnValue(scrollableResults);
|
||||
session.close();
|
||||
sessionControl.setReturnValue(null);
|
||||
sessionFactoryControl.replay();
|
||||
sessionControl.replay();
|
||||
itemReader.open(new ExecutionContext());
|
||||
itemReader.close(new ExecutionContext());
|
||||
sessionFactoryControl.verify();
|
||||
sessionControl.verify();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user