Problem: On every restart of my tomcat7 instance, all session was getting lost i.e all users were logged out and had to login again. There was no explicit persistence manager configured with the tomcat, therefore, it was using standard implementation of persistence manager (refer:
link).
Since it was using standard implementation, the users should not be logged out unless in context.xml file of tomcat (present in folder /etc/tomcat7) <Manager pathname=""/> tag was un-commented (as mentioned in comments of that file). But even without un-commenting the line, session was getting lost.
Solution : Use Persistence Manager and persist data in a datastore.
Implementation: I followed whats written under the "Persistent Manager Implementation" section of
this document and got it working !!
step 1 : In /usr/share/tomcat7/bin/startup.sh add the following line at the end of the file
export CATALINA_OPTS="-Dorg.apache.catalina.session.StandardSession.ACTIVITY_CHECK=true"
step 2: Under your MySQL instance or any other database run this query to create sessions table
CREATE TABLE `tomcat_sessions` (
`session_id` varchar(100) NOT NULL,
`valid_session` char(1) NOT NULL,
`max_inactive` int(11) NOT NULL,
`last_access` bigint(20) NOT NULL,
`app_name` varchar(255) DEFAULT NULL,
`session_data` mediumblob,
PRIMARY KEY (`session_id`),
KEY `kapp_name` (`app_name`)
);
step 3: Configure your tomcat instance to use the persistence manager and use a store. Edit the context.xml file present at /etc/tomcat7/context.xml and add following lines under <Context> tag
<Manager className="org.apache.catalina.session.PersistentManager" maxIdleBackup="1" >
<Store className="org.apache.catalina.session.JDBCStore"
connectionURL="jdbc:mysql://localhost:3306/your_db_name"
connectionName="your_db_username"
connectionPassword="your_db_password"
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>
step 4:
Download , extract, rename as mysql-connector-java.jar and put it in /usr/share/tomcat7/lib/ directory. Don't put any symlink, it won't work.
/usr/share/tomcat7/lib/mysql-connector-java.jar
step 5: Restart tomcat and check data in the table `tomcat_sessions`.
To restart enter: sudo service tomcat7 restart