Session API Connection Management

The Session API’s SessionManager is ultimately responsible for overseeing connection pool management. Connections may be reused across sessions, and in most cases, you should not need to worry about directly creating, caching, or closing connections to Cyc servers.

Normally, you should only need to close individual sessions when you are done with them; the SessionManager will close the underlying connection once there are no resources which rely upon it. However, it is possible to close the SessionManager, which will force it to close all connections.

Closing sessions

Closing a CycSession is straightforward. By default, the Cyc APIs all shall the same session within the local thread, and CycSessions implement, so you can simply close the current session via when you are done in a given thread. CycSessions can also be wrapped within try-with-resources statements:

try (CycSession session = CycSession.getCurrent()) {
  // Some calls to KB API, Query API, etc...

// The session has been automatically closed. The next call will return a new session:
CycSession newSession = CycSession.getCurrent();

This will close the current session and release it from the local thread. Once a session has been closed, the next call to CycSession.getCurrentSession() will create and return a new session.

Important note: Closing a session ensures that that the SessionManager will no longer return it for the current thread, but will not necessarily close the underlying connection to the Cyc server. Such details are handled by the SessionManager, which may allow resources to be shared between sessions; closing a particular session allows the SessionManager to make better decisions about resource management.


To be notified when a CycSession is closed, you can register a SessionListener:

session.addListener(new SessionListener() {
  public void onClose(Thread closingThread) {
    // Do something

Closing and reloading the SessionManager

When your application is about to exit, you should close the current SessionManager; this will cause it to close all CycSessions it has created, as well as all Cyc server connections which were underlying those sessions. SessionManagers also implement and can be wrapped within try-with-resources statements:

public static void main(String[] args) throws IOException {
  try (SessionManager sessionMgr = SessionManager.getInstance()) {
    // Some calls to KB API, Query API, etc...

You may check whether a SessionManager instance is closed:

boolean closed = SessionManager.getInstance().isClosed();

Once a SessionManager instance has been closed, it can no longer be used to create or retrieve sessions. However, you may load a new SessionManager instance:

SessionManager.getInstance().isClosed();      // true
SessionManager.getInstance().isClosed();      // false