Top Description Inners Fields Constructors Methods
org.mortbay.jetty.servlet

public Class JDBCSessionIdManager

extends AbstractSessionIdManager
Class Inheritance
Imports
java.io.ByteArrayInputStream, .InputStream, java.sql.Blob, .Connection, .DatabaseMetaData, .DriverManager, .PreparedStatement, .ResultSet, .SQLException, .Statement, java.util.ArrayList, .HashSet, .List, .Random, .Timer, .TimerTask, javax.naming.InitialContext, javax.servlet.http.HttpServletRequest, .HttpSession, javax.sql.DataSource, org.mortbay.jetty.Handler, .Server, .SessionManager, org.mortbay.jetty.handler.ContextHandler, org.mortbay.log.Log

JDBCSessionIdManager SessionIdManager implementation that uses a database to store in-use session ids, to support distributed sessions.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public class
JDBCSessionIdManager.DatabaseAdaptor

DatabaseAdaptor Handles differences between databases.

Field Summary

Modifier and TypeField and Description
protected String
protected String
protected String
protected DataSource
protected JDBCSessionIdManager.DatabaseAdaptor
protected String
protected String
protected String
protected String
protected String
protected long
protected String
protected long
protected String
protected HashSet<String>
protected String
protected String
protected TimerTask
protected Timer
Inherited from org.mortbay.jetty.servlet.AbstractSessionIdManager:
_random_server_weakRandom_workerNameSESSION_ID_RANDOM_ALGORITHMSESSION_ID_RANDOM_ALGORITHM_ALT

Constructor Summary

AccessConstructor and Description
public
public

Method Summary

Modifier and TypeMethod and Description
public void
addSession(HttpSession
The session
session
)

Implements org.mortbay.jetty.SessionIdManager.addSession.

Add a session to the list of known sessions for a given ID.
private void
delete(String id)

Remove a session id from the table.

public void
public void
private boolean
exists(String id)

Check if a session id exists.

public String
getClusterId(String nodeId)

Implements org.mortbay.jetty.SessionIdManager.getClusterId.

Get the session id without any node identifier suffix.
protected Connection
getConnection()

Get a connection from the driver or datasource.

public String
public String
public String
public String
getNodeId(String
The ID of the session
clusterId
,
HttpServletRequest
The request that for the session (or null)
request
)

Implements org.mortbay.jetty.SessionIdManager.getNodeId.

Get the session id, including this node's id as a suffix.
public long
public boolean
idInUse(String
The session ID without any cluster node extension
id
)

Implements org.mortbay.jetty.SessionIdManager.idInUse.

private void
private void
insert(String id)

Insert a new used session id into the table.

public void
invalidateAll(String
The session ID without any cluster node extension
id
)

Implements org.mortbay.jetty.SessionIdManager.invalidateAll.

Invalidate the session matching the id on all contexts.
private void
prepareTables()

Set up the tables in the database

public void
removeSession(HttpSession session)

Implements org.mortbay.jetty.SessionIdManager.removeSession.

Remove session from the list of known sessions for a given ID.
public void
private void
scavenge()

Look for sessions in the database that have expired.

public void
public void
setDriverInfo(String driverClassName, String connectionUrl)

Configure jdbc connection information via a jdbc Driver

public void
Inherited from org.mortbay.jetty.servlet.AbstractSessionIdManager:
getRandomgetWorkerNameinitRandomnewSessionIdsetRandomsetWorkerName

Field Detail

_connectionUrlback to summary
protected String _connectionUrl
_createSessionIdTableback to summary
protected String _createSessionIdTable
_createSessionTableback to summary
protected String _createSessionTable
_datasourceback to summary
protected DataSource _datasource
_dbAdaptorback to summary
protected JDBCSessionIdManager.DatabaseAdaptor _dbAdaptor
_deleteIdback to summary
protected String _deleteId
_deleteOldExpiredSessionsback to summary
protected String _deleteOldExpiredSessions
_driverClassNameback to summary
protected String _driverClassName
_insertIdback to summary
protected String _insertId
_jndiNameback to summary
protected String _jndiName
_lastScavengeTimeback to summary
protected long _lastScavengeTime
_queryIdback to summary
protected String _queryId
_scavengeIntervalMsback to summary
protected long _scavengeIntervalMs
_selectExpiredSessionsback to summary
protected String _selectExpiredSessions
_sessionIdsback to summary
protected HashSet<String> _sessionIds
_sessionIdTableback to summary
protected String _sessionIdTable
_sessionTableback to summary
protected String _sessionTable
_taskback to summary
protected TimerTask _task
_timerback to summary
protected Timer _timer

Constructor Detail

JDBCSessionIdManagerback to summary
public JDBCSessionIdManager(Server server)
JDBCSessionIdManagerback to summary
public JDBCSessionIdManager(Server server, Random random)

Method Detail

addSessionback to summary
public void addSession(HttpSession session)

Implements org.mortbay.jetty.SessionIdManager.addSession.

Doc from org.mortbay.jetty.SessionIdManager.addSession.

Add a session to the list of known sessions for a given ID.

Parameters
session:HttpSession

The session

deleteback to summary
private void delete(String id) throws SQLException

Remove a session id from the table.

doStartback to summary
public void doStart()

Overrides org.mortbay.jetty.servlet.AbstractSessionIdManager.doStart.

Start up the id manager. Makes necessary database tables and starts a Session scavenger thread.

See Also
org.mortbay.jetty.servlet.AbstractSessionIdManager#doStart()
doStopback to summary
public void doStop() throws Exception

Overrides org.mortbay.component.AbstractLifeCycle.doStop.

Stop the scavenger.

See Also
org.mortbay.component.AbstractLifeCycle#doStop()
existsback to summary
private boolean exists(String id) throws SQLException

Check if a session id exists.

getClusterIdback to summary
public String getClusterId(String nodeId)

Implements org.mortbay.jetty.SessionIdManager.getClusterId.

Get the session id without any node identifier suffix.

See Also
org.mortbay.jetty.SessionIdManager#getClusterId(java.lang.String)
getConnectionback to summary
protected Connection getConnection() throws SQLException

Get a connection from the driver or datasource.

getConnectionUrlback to summary
public String getConnectionUrl()
getDatasourceNameback to summary
public String getDatasourceName()
getDriverClassNameback to summary
public String getDriverClassName()
getNodeIdback to summary
public String getNodeId(String clusterId, HttpServletRequest request)

Implements org.mortbay.jetty.SessionIdManager.getNodeId.

Get the session id, including this node's id as a suffix.

Parameters
clusterId:String

Doc from org.mortbay.jetty.SessionIdManager.getNodeId.

The ID of the session

request:HttpServletRequest

Doc from org.mortbay.jetty.SessionIdManager.getNodeId.

The request that for the session (or null)

Returns:String

Doc from org.mortbay.jetty.SessionIdManager.getNodeId.

The session ID qualified with the node ID.

See Also
org.mortbay.jetty.SessionIdManager#getNodeId(java.lang.String, javax.servlet.http.HttpServletRequest)
getScavengeIntervalback to summary
public long getScavengeInterval()
idInUseback to summary
public boolean idInUse(String id)

Implements org.mortbay.jetty.SessionIdManager.idInUse.

Parameters
id:String

Doc from org.mortbay.jetty.SessionIdManager.idInUse.

The session ID without any cluster node extension

Returns:boolean

Doc from org.mortbay.jetty.SessionIdManager.idInUse.

True if the session ID is in use by at least one context.

initializeDatabaseback to summary
private void initializeDatabase() throws Exception
insertback to summary
private void insert(String id) throws SQLException

Insert a new used session id into the table.

invalidateAllback to summary
public void invalidateAll(String id)

Implements org.mortbay.jetty.SessionIdManager.invalidateAll.

Invalidate the session matching the id on all contexts.

Parameters
id:String

Doc from org.mortbay.jetty.SessionIdManager.invalidateAll.

The session ID without any cluster node extension

See Also
org.mortbay.jetty.SessionIdManager#invalidateAll(java.lang.String)
prepareTablesback to summary
private void prepareTables() throws SQLException

Set up the tables in the database

removeSessionback to summary
public void removeSession(HttpSession session)

Implements org.mortbay.jetty.SessionIdManager.removeSession.

Doc from org.mortbay.jetty.SessionIdManager.removeSession.

Remove session from the list of known sessions for a given ID.

removeSessionback to summary
public void removeSession(String id)
scavengeback to summary
private void scavenge()

Look for sessions in the database that have expired. We do this in the SessionIdManager and not the SessionManager so that we only have 1 scavenger, otherwise if there are n SessionManagers there would be n scavengers, all contending for the database. We look first for sessions that expired in the previous interval, then for sessions that expired previously - these are old sessions that no node is managing any more and have become stuck in the database.

setDatasourceNameback to summary
public void setDatasourceName(String jndi)
setDriverInfoback to summary
public void setDriverInfo(String driverClassName, String connectionUrl)

Configure jdbc connection information via a jdbc Driver

setScavengeIntervalback to summary
public void setScavengeInterval(long sec)
org.mortbay.jetty.servlet back to summary

public Class JDBCSessionIdManager.DatabaseAdaptor

extends Object
Class Inheritance
  • java.lang.Object
  • org.mortbay.jetty.servlet.JDBCSessionIdManager.DatabaseAdaptor

DatabaseAdaptor Handles differences between databases. Postgres uses the getBytes and setBinaryStream methods to access a "bytea" datatype, which can be up to 1Gb of binary data. MySQL is happy to use the "blob" type and getBlob() methods instead. TODO if the differences become more major it would be worthwhile refactoring this class.

Field Summary

Modifier and TypeField and Description
pack-priv String
pack-priv boolean
pack-priv boolean

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public String
convertIdentifier(String identifier)

Convert a camel case identifier into either upper or lower depending on the way the db stores identifiers.

public InputStream
getBlobInputStream(ResultSet result, String columnName)

public String
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

_dbNameback to summary
pack-priv String _dbName
_isLowerback to summary
pack-priv boolean _isLower
_isUpperback to summary
pack-priv boolean _isUpper

Constructor Detail

DatabaseAdaptorback to summary
public DatabaseAdaptor(DatabaseMetaData dbMeta) throws SQLException

Method Detail

convertIdentifierback to summary
public String convertIdentifier(String identifier)

Convert a camel case identifier into either upper or lower depending on the way the db stores identifiers.

getBlobInputStreamback to summary
public InputStream getBlobInputStream(ResultSet result, String columnName) throws SQLException
getBlobTypeback to summary
public String getBlobType()