title: Tomcat, User Session Storage with JDBCStore, Immediate Session Timeout
body: <p>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. </p>
  
  <p>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:</p>
  
  <p>Snippet from my Tomcat tomcat7/conf/context.xml:</p>
  
  <pre><code>...
  &lt;Manager className="org.apache.catalina.session.PersistentManager"           
          maxIdleBackup="1"
          minIdleSwap="0"
          maxIdleSwap="0"
          processExpiresFrequency="1"
          saveOnRestart='true'
          &gt;
  
          &lt;Store className="org.apache.catalina.session.JDBCStore"                
                  connectionURL="jdbc:postgresql://localhost:5432/tomcat?user=[USER]&amp;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" 
                  /&gt;
  &lt;/Manager&gt;
  ...
  </code></pre>
  
  <p>I have also added this to my tomcat7/conf/catalina.properties:</p>
  
  <pre><code>...
  org.apache.catalina.session.StandardSession.ACTIVITY_CHECK=true
  org.apache.catalina.STRICT_SERVLET_COMPLIANCE=true
  ...
  </code></pre>
  
  <p>I have the PostgreSQL JDBC connector in tomcat7/lib.</p>
  
  <p><strong>Variations I have tried:</strong></p>
  
  <ul>
  <li>Tomcat 6</li>
  <li>Tomcat 7</li>
  <li>MySQL</li>
  <li>PostgreSQL</li>
  </ul>
  
  <p>I have also added the following to conf/logging.properties in the hopes I could debug this futher:</p>
  
  <pre><code>...
  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
  ...
  </code></pre>
  
  <p>The table schema is </p>
  
  <pre><code>+---------------+--------------+------+-----+---------+-------+
  | 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    |       |
  +---------------+--------------+------+-----+---------+-------+
  </code></pre>
  
  <p>Tomcat logs show:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>The postgreSQL logs show:</p>
  
  <pre><code>2013-06-27 14:16:33 IST LOG:  execute &lt;unnamed&gt;: 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 &lt;unnamed&gt;: 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 &lt;unnamed&gt;: 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 &lt;unnamed&gt;: 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]'
  </code></pre>
  
  <p>Thanks in advance.</p>