我正在尝试将用户会话数据存储在数据库中。我已经关注了几篇文章和关于如何做到这一点的 Tomcat 文档。
我现在正在使用 Tomcat7 和 PostgreSQL,并且可以确认 INSERTS 正在工作,但仍然存在问题。Tomcat 将用户会话插入数据库,然后立即将其删除:
来自我的 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]&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>
...
我也将此添加到我的 tomcat7/conf/catalina.properties 中:
...
org.apache.catalina.session.StandardSession.ACTIVITY_CHECK=true
org.apache.catalina.STRICT_SERVLET_COMPLIANCE=true
...
我在 tomcat7/lib 中有 PostgreSQL JDBC 连接器。
我尝试过的变体:
- 雄猫 6
- 雄猫 7
- MySQL
- PostgreSQL
我还在 conf/logging.properties 中添加了以下内容,希望可以进一步调试:
...
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
...
表架构是
+---------------+--------------+------+-----+---------+-------+
| 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日志显示:
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
postgreSQL 日志显示:
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]'
提前致谢。