org.eclipse.jetty.server.session

## Class JDBCSessionManager

- java.lang.Object
- - org.eclipse.jetty.util.component.AbstractLifeCycle
  - - org.eclipse.jetty.server.session.AbstractSessionManager
    - - org.eclipse.jetty.server.session.JDBCSessionManager

All Implemented Interfaces:
  SessionManager, LifeCycle

    public class JDBCSessionManager
    extends AbstractSessionManager

JDBCSessionManager SessionManager that persists sessions to a database to enable clustering. Session data is persisted to the JettySessions table: rowId (unique in cluster: webapp name/path + virtualhost + sessionId) contextPath (of the context owning the session) sessionId (unique in a context) lastNode (name of node last handled session) accessTime (time in milliseconds session was accessed) lastAccessTime (previous time in milliseconds session was accessed) createTime (time in milliseconds session created) cookieTime (time in milliseconds session cookie created) lastSavedTime (last time in milliseconds session access times were saved) expiryTime (time in milliseconds that the session is due to expire) map (attribute map) As an optimization, to prevent thrashing the database, we do not persist the accessTime and lastAccessTime every time the session is accessed. Rather, we write it out every so often. The frequency is controlled by the saveIntervalSec field.

### Nested Class Summary

Nested Classes
`protected class` `JDBCSessionManager.ClassLoadingObjectInputStream` ClassLoadingObjectInputStream Used to persist the session attribute map
`class` `JDBCSessionManager.Session` Session Session instance.

### Nested classes/interfaces inherited from class org.eclipse.jetty.server.session.AbstractSessionManager

`AbstractSessionManager.SessionIf`

### Nested classes/interfaces inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle

`AbstractLifeCycle.AbstractLifeCycleListener`

### Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.LifeCycle

`LifeCycle.Listener`

### Field Summary

Fields
`protected JDBCSessionIdManager` `_jdbcSessionIdMgr`
`protected long` `_saveIntervalSec`

### Fields inherited from class org.eclipse.jetty.server.session.AbstractSessionManager

`__defaultSessionTrackingModes, __distantFuture, _checkingRemoteSessionIdEncoding, _context, _dftMaxIdleSecs, _httpOnly, _loader, _maxCookieAge, _nodeIdInSessionId, _refreshCookieAge, _secureCookies, _secureRequestOnly, _sessionAttributeListeners, _sessionComment, _sessionCookie, _sessionDomain, _sessionHandler, _sessionIdManager, _sessionIdPathParameterName, _sessionIdPathParameterNamePrefix, _sessionListeners, _sessionPath, _sessionsStats, _sessionTimeStats, _sessionTrackingModes, SESSION_KNOWN_ONLY_TO_AUTHENTICATED`

### Fields inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle

`_listeners, FAILED, RUNNING, STARTED, STARTING, STOPPED, STOPPING`

### Fields inherited from interface org.eclipse.jetty.server.SessionManager

`__CheckRemoteSessionEncoding, __DefaultSessionCookie, __DefaultSessionDomain, __DefaultSessionIdPathParameterName, __MaxAgeProperty, __SessionCookieProperty, __SessionDomainProperty, __SessionIdPathParameterNameProperty, __SessionPathProperty`

### Constructor Summary

Constructors
Constructor and Description
`JDBCSessionManager()`

### Method Summary

Methods
`protected void` `addSession(AbstractSession session)` Add a newly created session to our in-memory list for this node and persist it.
`void` `cacheInvalidate(JDBCSessionManager.Session session)` A method that can be implemented in subclasses to support distributed caching of sessions.
`protected void` `deleteSession(JDBCSessionManager.Session data)` Delete a session from the database.
`void` `doStart()` Start the session manager.
`void` `doStop()` Stop the session manager.
`protected void` `expire(List<?> sessionIds)` Expire any Sessions we have in memory matching the list of expired Session ids.
`long` `getSaveInterval()`
`JDBCSessionManager.Session` `getSession(String idInCluster)` A session has been requested by its id on this node.
`int` `getSessions()` Get the number of sessions.
`protected void` `invalidateSession(String idInCluster)` Invalidate a session.
`protected void` `invalidateSessions()`
`protected JDBCSessionManager.Session` `loadSession(String id, String canonicalContextPath, String vhost)` Load a session from the database
`protected AbstractSession` `newSession(javax.servlet.http.HttpServletRequest request)` Make a new Session.
`void` `removeSession(AbstractSession session, boolean invalidate)` Remove session from manager
`protected boolean` `removeSession(String idInCluster)` Delete an existing session, both from the in-memory map and the database.
`void` `setSaveInterval(long sec)` Set the time in seconds which is the interval between saving the session access time to the database.
`protected void` `storeSession(JDBCSessionManager.Session session)` Insert a session into the database.
`protected void` `updateSession(JDBCSessionManager.Session data)` Update data on an existing persisted session.
`protected void` `updateSessionNode(JDBCSessionManager.Session data)` Update the node on which the session was last seen to be my node.

### Methods inherited from class org.eclipse.jetty.server.session.AbstractSessionManager

`access, addEventListener, addSession, clearEventListeners, complete, doSessionAttributeListeners, getClusterId, getContext, getContextHandler, getDefaultSessionTrackingModes, getEffectiveSessionTrackingModes, getHttpOnly, getHttpSession, getIdManager, getMaxCookieAge, getMaxInactiveInterval, getMaxSessions, getMetaManager, getMinSessions, getNodeId, getRefreshCookieAge, getSecureCookies, getSessionCookie, getSessionCookie, getSessionCookieConfig, getSessionDomain, getSessionHandler, getSessionIdManager, getSessionIdPathParameterName, getSessionIdPathParameterNamePrefix, getSessionMap, getSessionPath, getSessionsMax, getSessionsTotal, getSessionTimeMax, getSessionTimeMean, getSessionTimeStdDev, getSessionTimeTotal, isCheckingRemoteSessionIdEncoding, isNodeIdInSessionId, isSecureRequestOnly, isUsingCookies, isUsingURLs, isValid, newHttpSession, removeEventListener, removeSession, renewSession, resetStats, setCheckingRemoteSessionIdEncoding, setHttpOnly, setIdManager, setMaxInactiveInterval, setNodeIdInSessionId, setRefreshCookieAge, setSecureRequestOnly, setSessionCookie, setSessionHandler, setSessionIdManager, setSessionIdPathParameterName, setSessionTrackingModes, setUsingCookies, statsReset`

### Methods inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle

`addLifeCycleListener, getState, getState, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, start, stop`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Methods inherited from interface org.eclipse.jetty.util.component.LifeCycle

`addLifeCycleListener, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, start, stop`

### Field Detail

#### _jdbcSessionIdMgr

    protected JDBCSessionIdManager _jdbcSessionIdMgr

#### _saveIntervalSec

    protected long _saveIntervalSec

### Constructor Detail

#### JDBCSessionManager

    public JDBCSessionManager()

### Method Detail

#### setSaveInterval

    public void setSaveInterval(long sec)

Set the time in seconds which is the interval between saving the session access time to the database. This is an optimization that prevents the database from being overloaded when a session is accessed very frequently. On session exit, if the session attributes have NOT changed, the time at which we last saved the accessed time is compared to the current accessed time. If the interval is at least saveIntervalSecs, then the access time will be persisted to the database. If any session attribute does change, then the attributes and the accessed time are persisted.

Parameters:
  `sec` -

#### getSaveInterval

    public long getSaveInterval()

#### cacheInvalidate

    public void cacheInvalidate(JDBCSessionManager.Session session)

A method that can be implemented in subclasses to support distributed caching of sessions. This method will be called whenever the session is written to the database because the session data has changed. This could be used eg with a JMS backplane to notify nodes that the session has changed and to delete the session from the node's cache, and re-read it from the database.

Parameters:
  `session` -

#### getSession

    public JDBCSessionManager.Session getSession(String idInCluster)

A session has been requested by its id on this node. Load the session by id AND context path from the database. Multiple contexts may share the same session id (due to dispatching) but they CANNOT share the same contents. Check if last node id is my node id, if so, then the session we have in memory cannot be stale. If another node used the session last, then we need to refresh from the db. NOTE: this method will go to the database, so if you only want to check for the existence of a Session in memory, use _sessions.get(id) instead.

Specified by:
  `getSession` in class `AbstractSessionManager`
Parameters:
  `idInCluster` - The session ID in the cluster, stripped of any worker name.
Returns:
  A Session or null if none exists.
See Also:
  `AbstractSessionManager.getSession(java.lang.String)`

#### getSessions

    public int getSessions()

Get the number of sessions.

Overrides:
  `getSessions` in class `AbstractSessionManager`
See Also:
  `AbstractSessionManager.getSessions()`

#### doStart

    public void doStart()
                 throws Exception

Start the session manager.

Overrides:
  `doStart` in class `AbstractSessionManager`
Throws:
  `Exception`
See Also:
  `AbstractSessionManager.doStart()`

#### doStop

    public void doStop()
                throws Exception

Stop the session manager.

Overrides:
  `doStop` in class `AbstractSessionManager`
Throws:
  `Exception`
See Also:
  `AbstractSessionManager.doStop()`

#### invalidateSessions

    protected void invalidateSessions()

Specified by:
  `invalidateSessions` in class `AbstractSessionManager`

#### invalidateSession

    protected void invalidateSession(String idInCluster)

Invalidate a session.

Parameters:
  `idInCluster` -

#### removeSession

    protected boolean removeSession(String idInCluster)

Delete an existing session, both from the in-memory map and the database.

Specified by:
  `removeSession` in class `AbstractSessionManager`
See Also:
  `AbstractSessionManager.removeSession(java.lang.String)`

#### addSession

    protected void addSession(AbstractSession session)

Add a newly created session to our in-memory list for this node and persist it.

Specified by:
  `addSession` in class `AbstractSessionManager`
See Also:
  `AbstractSessionManager.addSession(org.eclipse.jetty.server.session.AbstractSession)`

#### newSession

    protected AbstractSession newSession(javax.servlet.http.HttpServletRequest request)

Make a new Session.

Specified by:
  `newSession` in class `AbstractSessionManager`
Returns:
  the new session
See Also:
  `AbstractSessionManager.newSession(javax.servlet.http.HttpServletRequest)`

#### removeSession

    public void removeSession(AbstractSession session,
                     boolean invalidate)

Remove session from manager

Overrides:
  `removeSession` in class `AbstractSessionManager`
Parameters:
  `session` - The session to remove
  `invalidate` - True if `HttpSessionListener.sessionDestroyed(HttpSessionEvent)` and `SessionIdManager.invalidateAll(String)` should be called.

#### expire

    protected void expire(List<?> sessionIds)

Expire any Sessions we have in memory matching the list of expired Session ids.

Parameters:
  `sessionIds` -

#### loadSession

    protected JDBCSessionManager.Session loadSession(String id,
                                         String canonicalContextPath,
                                         String vhost)
                                              throws Exception

Load a session from the database

Parameters:
  `id` -
Returns:
  the session data that was loaded
Throws:
  `Exception`

#### storeSession

    protected void storeSession(JDBCSessionManager.Session session)
                         throws Exception

Insert a session into the database.

Parameters:
  `data` -
Throws:
  `Exception`

#### updateSession

    protected void updateSession(JDBCSessionManager.Session data)
                          throws Exception

Update data on an existing persisted session.

Parameters:
  `data` - the session
Throws:
  `Exception`

#### updateSessionNode

    protected void updateSessionNode(JDBCSessionManager.Session data)
                              throws Exception

Update the node on which the session was last seen to be my node.

Parameters:
  `data` - the session
Throws:
  `Exception`

#### deleteSession

    protected void deleteSession(JDBCSessionManager.Session data)
                          throws Exception

Delete a session from the database. Should only be called when the session has been invalidated.

Parameters:
  `data` -
Throws:
  `Exception`