我正在尝试将用户会话数据存储在数据库中。我已经关注了一些文章和关于如何执行此操作的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连接器。
我尝试的变化:
我还在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]'
提前致谢。
答案 0 :(得分:4)
我切换到MySQL数据库并使用BIGINT而不是INT(64)。它解决了我的问题...
我不知道为什么......爱成为一名程序员!
答案 1 :(得分:2)
Tomcat 7.0.x中存在一个错误。您需要升级到7.0.56+或8。
答案 2 :(得分:1)
你需要检查一些事情......
验证tomcat是否实际连接到数据库
一旦你确定它正在连接..用户是否有权更新表tomcat_sessions
如果要向会话添加数据,则所有对象都需要可序列化,或者无法重新加载
希望这有帮助