Tomcat Session Persistence
English |
---|
This article will explain on how to configure the Tomcat Persistent Manager, in the event the load balancer does not support sticky sessions, we can implement Persistent Manager for clustering, which has the capability to swap active (but idle) sessions out to a persistent storage mechanism, as well as to save all sessions across a normal restart of Tomcat. |
Info |
---|
Note : This KB is only for the Tomcat configuration, for other clustering configurations eg. setting up shared directory etc please refer to Server Clustering Guide page here. |
In server.xml file, we need to add the jvmRoute.
Code Block |
---|
|
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1"> |
In catalina.properties, add the
Code Block |
---|
|
org.apache.catalina.session.StandardSession.ACTIVITY_CHECK=true |
to ensure that the persistent manager works correctly.
For this guide we use a JDBC Based Store to save sessions in individual rows of a preconfigured table in a database that is accessed via a JDBC driver. Create a database named tomcat and table with the following SQL queries:
Using MySQL/MariaDB;
Code Block |
---|
|
create database tomcat;
grant all privileges on tomcat.* to 'tomcat'@'%' identified by 'tomcat';
use tomcat;
create table tomcat_sessions (
session_id varchar(100) not null primary key,
valid_session char(1) not null,
max_inactive int not null,
last_access bigint not null,
app_name varchar(255),
session_data mediumblob,
KEY kapp_name(app_name)
); |
Using MSSQL;
Code Block |
---|
|
CREATE DATABASE tomcat;
CREATE LOGIN tomcat WITH PASSWORD = 'tomcat';
CREATE USER tomcat FOR LOGIN tomcat;
USE tomcat;
CREATE TABLE tomcat_sessions (
session_id VARCHAR(100) NOT NULL PRIMARY KEY,
valid_session CHAR(1) NOT NULL,
max_inactive INT NOT NULL,
last_access BIGINT NOT NULL,
app_name VARCHAR(255),
session_data VARBINARY(MAX),
);
CREATE INDEX kapp_name ON tomcat_sessions (app_name);
#For user permission on tomcat database
ALTER ROLE db_owner ADD MEMBER tomcat; |
Using PostgreSQL;
Code Block |
---|
|
-- Create database
CREATE DATABASE tomcat;
-- Create user and grant privileges
CREATE USER tomcat WITH PASSWORD 'tomcat';
GRANT ALL PRIVILEGES ON DATABASE tomcat TO tomcat;
ALTER DATABASE tomcat OWNER TO tomcat;
-- Connect to the database
\c tomcat tomcat;
-- Create table
CREATE TABLE tomcat_sessions (
session_id character varying(100) NOT NULL,
valid_session character(1) NOT NULL,
max_inactive integer NOT NULL,
last_access bigint NOT NULL,
app_name character varying(255),
session_data bytea,
CONSTRAINT tomcat_sessions_pkey PRIMARY KEY (session_id)
);
CREATE INDEX app_name_index
ON tomcat_sessions
USING btree
(app_name); |
In order for the JDBC Based Store to successfully connect to the database, we need to place the JAR file containing the correct JDBC driver into [TOMCAT_PATH]\lib\ directory. Take note that if using MySQL, place the MySQL JDBC driver and if using MSSQL place the MSSQL JDBC driver (same case for the PostgreSQL, need to use the PostgreSQL JDBC driver).
Last but not least, add the following content into [TOMCAT_PATH]\conf\context.xml
Using MySQL/MariaDB;
Code Block |
---|
|
<Resources cachingAllowed="true" cacheMaxSize="100000" />
<Valve className="org.apache.catalina.valves.PersistentValve"/>
<Manager className="org.apache.catalina.session.PersistentManager"
maxIdleBackup="1"
maxIdleSwap="1"
minIdleSwap="0"
processExpiresFrequency="1"
saveOnRestart='true'>
<Store className="org.apache.catalina.session.JDBCStore"
connectionURL="jdbc:mysql://joget-db-server-ip/tomcat?user=tomcat&password=tomcat"
driverName="com.mysql.jdbc.Driver"
sessionAppCol="app_name"
sessionDataCol="session_data"
sessionIdCol="session_id"
sessionLastAccessedCol="last_access"
sessionMaxInactiveCol="max_inactive"
sessionTable="tomcat_sessions"
sessionValidCol="valid_session"/>
</Manager> |
Using MSSQL;
Code Block |
---|
|
<Resources cachingAllowed="true" cacheMaxSize="100000" />
<Valve className="org.apache.catalina.valves.PersistentValve"/>
<Manager className="org.apache.catalina.session.PersistentManager"
maxIdleBackup="1"
maxIdleSwap="1"
minIdleSwap="0"
processExpiresFrequency="1"
saveOnRestart='true'>
<Store className="org.apache.catalina.session.JDBCStore"
connectionURL="jdbc:sqlserver://joget-db-server-ip:1433&databaseName=tomcat&user=tomcat&password=tomcat&encrypt=false&trustServerCertificate=false"
driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
sessionAppCol="app_name"
sessionDataCol="session_data"
sessionIdCol="session_id"
sessionLastAccessedCol="last_access"
sessionMaxInactiveCol="max_inactive"
sessionTable="tomcat_sessions"
sessionValidCol="valid_session"/>
</Manager> |
Using PostgreSQL;
Code Block |
---|
|
<Resources cachingAllowed="true" cacheMaxSize="100000" />
<Valve className="org.apache.catalina.valves.PersistentValve"/>
<Manager className="org.apache.catalina.session.PersistentManager"
maxIdleBackup="1"
maxIdleSwap="1"
minIdleSwap="0"
processExpiresFrequency="1"
saveOnRestart='true'>
<Store className="org.apache.catalina.session.JDBCStore"
connectionURL="jdbc:postgresql://[YourDBIPHere]:[YourDBPortHere]/tomcat?user=tomcat&password=tomcat"
driverName="org.postgresql.Driver"
sessionAppCol="app_name"
sessionDataCol="session_data"
sessionIdCol="session_id"
sessionLastAccessedCol="last_access"
sessionMaxInactiveCol="max_inactive"
sessionTable="public.tomcat_sessions"
sessionValidCol="valid_session"/>
</Manager> |
Info |
---|
Additional note : For file based store method, you can refer to the Tomcat documentation here. |