JDBC-backed session map implementation for Selenium Grid providing database-backed storage for distributed WebDriver sessions.
—
Core session storage and retrieval operations with database persistence for Selenium Grid.
Adds a WebDriver session to the database for distributed storage.
/**
* Adds a session to the database
* @param session The session to store, containing ID, URI, capabilities, and metadata
* @return true if the session was successfully added, false otherwise
* @throws JdbcException if database operation fails
*/
public boolean add(Session session);Usage Example:
import org.openqa.selenium.grid.data.Session;
import org.openqa.selenium.remote.SessionId;
import org.openqa.selenium.ImmutableCapabilities;
import java.net.URI;
import java.time.Instant;
// Create a session object
Session session = new Session(
new SessionId("session-12345"),
URI.create("http://node1.grid.com:5555"),
new ImmutableCapabilities("browserName", "chrome", "version", "91.0"),
new ImmutableCapabilities("browserName", "chrome", "platformName", "linux"),
Instant.now()
);
// Add to session map
boolean added = sessionMap.add(session);
if (added) {
System.out.println("Session successfully stored in database");
}Retrieves a session from the database by its session ID.
/**
* Retrieves a session from the database
* @param id The session ID to look up
* @return The complete session object with all metadata
* @throws NoSuchSessionException if the session doesn't exist
* @throws JdbcException if database operation fails
*/
public Session get(SessionId id) throws NoSuchSessionException;Usage Example:
import org.openqa.selenium.NoSuchSessionException;
try {
SessionId sessionId = new SessionId("session-12345");
Session session = sessionMap.get(sessionId);
System.out.println("Session URI: " + session.getUri());
System.out.println("Session capabilities: " + session.getCapabilities());
System.out.println("Session start time: " + session.getStartTime());
} catch (NoSuchSessionException e) {
System.err.println("Session not found: " + e.getMessage());
}Removes a session from the database by its session ID.
/**
* Removes a session from the database
* @param id The session ID to remove
* @throws JdbcException if database operation fails
* Note: Does not throw if session doesn't exist - removal is idempotent
*/
public void remove(SessionId id);Usage Example:
SessionId sessionId = new SessionId("session-12345");
sessionMap.remove(sessionId);
System.out.println("Session removed from database");Retrieves just the URI where a session is running, without the full session object.
/**
* Gets the URI where a session is running
* @param id The session ID to look up
* @return The URI where the session is hosted
* @throws NoSuchSessionException if the session doesn't exist
*/
public URI getUri(SessionId id) throws NoSuchSessionException;Usage Example:
try {
SessionId sessionId = new SessionId("session-12345");
URI nodeUri = sessionMap.getUri(sessionId);
System.out.println("Session is running on: " + nodeUri);
} catch (NoSuchSessionException e) {
System.err.println("Session not found: " + e.getMessage());
}Removes all sessions associated with a specific node URI. Used for cleanup when nodes are removed or restarted.
/**
* Removes all sessions running on a specific node URI
* @param sessionUri The URI of the node whose sessions should be removed
* @throws JdbcException if database operation fails
*/
public void removeByUri(URI sessionUri);Usage Example:
// Remove all sessions from a node that has gone offline
URI nodeUri = URI.create("http://node1.grid.com:5555");
((JdbcBackedSessionMap) sessionMap).removeByUri(nodeUri);
System.out.println("All sessions from node removed");Verifies that the database connection is active and ready to handle operations.
/**
* Checks if the session map is ready to handle operations
* @return true if the database connection is active, false otherwise
*/
public boolean isReady();Usage Example:
if (sessionMap.isReady()) {
System.out.println("Session map is ready for operations");
// Proceed with session operations
} else {
System.err.println("Session map is not ready - check database connection");
}Closes the database connection when the session map is no longer needed.
/**
* Closes the database connection
* Implements Closeable interface for resource management
*/
public void close();Usage Example:
// Using try-with-resources for automatic cleanup
try (JdbcBackedSessionMap sessionMap = new JdbcBackedSessionMap(tracer, connection, bus)) {
// Use the session map
sessionMap.add(session);
Session retrieved = sessionMap.get(session.getId());
} // Connection automatically closed here
// Or manual cleanup
sessionMap.close();The JDBC session map automatically integrates with Selenium Grid's event system for cleanup operations:
This integration ensures that the database doesn't accumulate stale session data as the grid operates.
The session map expects a database table with the following structure:
CREATE TABLE sessions_map (
session_ids VARCHAR(255),
session_uri TEXT,
session_stereotype TEXT,
session_caps TEXT,
session_start VARCHAR(255)
);Session data is stored as JSON-serialized strings in the appropriate columns, allowing the full session state to be reconstructed when retrieved.
Install with Tessl CLI
npx tessl i tessl/maven-org-seleniumhq-selenium--selenium-session-map-jdbc