Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat, User Session Storage with JDBCStore, Immediate Session Timeout

I am attempting to store user session data in a database. I have followed a few articles and the Tomcat documentation on how to do this.

I am now using Tomcat7 and PostgreSQL and can confirm the INSERTS are working but theres still a problem. Tomcat is inserting the user session into the database and then is immediately deleting it:

Snippet from my Tomcat tomcat7/conf/context.xml:

...
<Manager className="org.apache.catalina.session.PersistentManager"           
        maxIdleBackup="1"
        minIdleSwap="0"
        maxIdleSwap="0"
        processExpiresFrequency="1"
        saveOnRestart='true'
        >

        <Store className="org.apache.catalina.session.JDBCStore"                
                connectionURL="jdbc:postgresql://localhost:5432/tomcat?user=[USER]&amp;password=[PASSWORD]"
                driverName="org.postgresql.Driver"
                sessionAppCol="app_name"
                sessionDataCol="session_data"
                sessionIdCol="session_id"
                sessionLastAccessedCol="last_access"
                sessionMaxInactiveCol="max_inactive"
                sessionTable="tomcat_sessions"
                sessionValidCol="valid_session" 
                />
</Manager>
...

I have also added this to my tomcat7/conf/catalina.properties:

...
org.apache.catalina.session.StandardSession.ACTIVITY_CHECK=true
org.apache.catalina.STRICT_SERVLET_COMPLIANCE=true
...

I have the PostgreSQL JDBC connector in tomcat7/lib.

Variations I have tried:

  • Tomcat 6
  • Tomcat 7
  • MySQL
  • PostgreSQL

I have also added the following to conf/logging.properties in the hopes I could debug this futher:

...
org.apache.catalina.session.PersistentManager.level = ALL
org.apache.catalina.session.PersistentManager.useParentHandlers = true
org.apache.catalina.session.level = ALL
org.apache.catalina.session.useParentHandlers = true
...

The table schema is

+---------------+--------------+------+-----+---------+-------+
| Field         | Type         | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+-------+
| session_id    | varchar(100) | NO   | PRI | NULL    |       |
| valid_session | char(1)      | NO   |     | NULL    |       |
| max_inactive  | int(32)      | NO   |     | NULL    |       |
| last_access   | int(64)      | NO   |     | NULL    |       |
| app_name      | varchar(255) | YES  | MUL | NULL    |       |
| session_data  | mediumblob   | YES  |     | NULL    |       |
+---------------+--------------+------+-----+---------+-------+

Tomcat logs show:

Jun 27, 2013 2:16:33 PM org.apache.catalina.session.PersistentManagerBase processExpires
FINE: Start expire sessions PersistentManager at 1372338993228 sessioncount 1
Jun 27, 2013 2:16:33 PM org.apache.catalina.session.PersistentManagerBase processMaxIdleSwaps
FINE: Swapping session 22115A5964B70168B1752D5415DAD31C to Store, idle for 36 seconds
Jun 27, 2013 2:16:33 PM org.apache.catalina.session.PersistentManagerBase processExpires
FINE: End expire sessions PersistentManager processingTime 168 expired sessions: 0

The postgreSQL logs show:

2013-06-27 14:16:33 IST LOG:  execute <unnamed>: INSERT INTO tomcat_sessions (session_id, app_name, session_data, valid_session, max_inactive, last_access) VALUES ($1, $2, $3, $4, $5, $6)
2013-06-27 14:16:33 IST DETAIL:  parameters: $1 = '22115A5964B70168B1752D5415DAD31C', $2 = '/Catalina/localhost/[SERVLET_NAME]', $3 = '\xaced...f72', $4 = '1', $5 = '1800', $6 = '1372338956718'
2013-06-27 14:16:33 IST LOG:  execute <unnamed>: SELECT session_id FROM tomcat_sessions WHERE app_name = $1
2013-06-27 14:16:33 IST DETAIL:  parameters: $1 = '/Catalina/localhost/[SERVLET_NAME]'
2013-06-27 14:16:33 IST LOG:  execute <unnamed>: SELECT session_id, session_data FROM tomcat_sessions WHERE session_id = $1 AND app_name = $2
2013-06-27 14:16:33 IST DETAIL:  parameters: $1 = '22115A5964B70168B1752D5415DAD31C', $2 = '/Catalina/localhost/[SERVLET_NAME]'
2013-06-27 14:16:33 IST LOG:  execute <unnamed>: DELETE FROM tomcat_sessions WHERE session_id = $1  AND app_name = $2
2013-06-27 14:16:33 IST DETAIL:  parameters: $1 = '22115A5964B70168B1752D5415DAD31C', $2 = '/Catalina/localhost/[SERVLET_NAME]'

Thanks in advance.

like image 415
messinga Avatar asked Jan 13 '23 01:01

messinga


1 Answers

I switched to MySQL database and using BIGINT instead of INT(64). It solved the problem for me...

I have no idea why...Love being a programmer!

like image 166
messinga Avatar answered Jan 16 '23 00:01

messinga