// At this point in the code, the fact has been persisted to the database
// (within a transaction via the SingleSessionCommandService)
Collection<FactHandle> factHandles = commandKSession.getFactHandles();
assertTrue("At least one fact should have been inserted by the ksession.insert() method above.", !factHandles.isEmpty());
FactHandle origFactHandle = factHandles.iterator().next();
assertTrue("The stored fact should contain the same number as the value inserted (but does not).",
Integer.parseInt(((DefaultFactHandle) origFactHandle).getObject().toString()) == integerFact.intValue() );
// Save the sessionInfo id in order to retrieve it later
int sessionInfoId = commandKSession.getId();
// Clean up the session, environment, etc.
PersistenceContextManager pcm = (PersistenceContextManager) commandKSession.getEnvironment().get(EnvironmentName.PERSISTENCE_CONTEXT_MANAGER);
commandKSession.dispose();
pcm.dispose();
emf.close();
// Reload session from the database
emf = Persistence.createEntityManagerFactory(DROOLS_PERSISTENCE_UNIT_NAME);
context.put(ENTITY_MANAGER_FACTORY, emf);
env = createEnvironment(context);
// Re-initialize the knowledge session:
StatefulKnowledgeSession newCommandKSession
= JPAKnowledgeService.loadStatefulKnowledgeSession(sessionInfoId, kbase, null, env);
// Test that the session has been successfully reinitialized
factHandles = newCommandKSession.getFactHandles();
assertTrue("At least one fact should have been persisted by the ksession.insert above.",
!factHandles.isEmpty() && factHandles.size() == 1);
FactHandle retrievedFactHandle = factHandles.iterator().next();
assertTrue("If the retrieved and original FactHandle object are the same, then the knowledge session has NOT been reloaded!",
origFactHandle != retrievedFactHandle);
assertTrue("The retrieved fact should contain the same info as the original (but does not).",
Integer.parseInt(((DefaultFactHandle) retrievedFactHandle).getObject().toString()) == integerFact.intValue() );