public void deactivateContextPartitions(Set<Integer> agentInstanceIds) {
   for (Integer agentInstanceId : agentInstanceIds) {
     ContextControllerTreeAgentInstanceList list = agentInstances.get(agentInstanceId);
     StatementAgentInstanceUtil.stopAgentInstances(
         list.getAgentInstances(), null, servicesContext, false);
   }
 }
 public synchronized void contextPartitionTerminate(
     ContextControllerInstanceHandle contextNestedHandle,
     Map<String, Object> terminationProperties) {
   ContextNestedHandleImpl handle = (ContextNestedHandleImpl) contextNestedHandle;
   ContextControllerTreeAgentInstanceList entry =
       agentInstances.remove(handle.getContextPartitionOrPathId());
   if (entry != null) {
     StatementAgentInstanceUtil.stopAgentInstances(
         entry.getAgentInstances(), terminationProperties, servicesContext, false);
     entry.getAgentInstances().clear();
     contextPartitionIdManager.removeId(contextNestedHandle.getContextPartitionOrPathId());
   }
 }
  public void safeDestroy() {
    if (rootContext != null) {
      // deactivate
      rootContext.deactivate();
      factory.getStateCache().removeContext(contextName);

      for (Map.Entry<Integer, ContextControllerTreeAgentInstanceList> entryCP :
          agentInstances.entrySet()) {
        StatementAgentInstanceUtil.stopAgentInstances(
            entryCP.getValue().getAgentInstances(), null, servicesContext, true);
      }
      agentInstances.clear();
      contextPartitionIdManager.clear();
      statements.clear();
    }
  }