Telling Tomcat to save session records in MySQL

此部分内容摘自 MySQL cookbook 3th。具体内容不做翻译,哈哈,懒

The default Tomcat default session storage mechanism uses temporary files. To save

sessions using JDBC with MySQL instead, follow this procedure:

  1. Create a table to hold session records.
  2. Make sure that Tomcat can access the proper JDBC driver.
  3. Modify the appropriate Tomcat configuration file to specify use of a persistent ses‐

    sion manager for the relevant application context.

None of these steps involve modifying the sample session script in any way, which

reflects how Tomcat implements session support above the application level.

Create the Tomcat session table.

  1. Tomcat stores several types of information in the session table:
  2. The session ID. By default, IDs are 32-character MD5 values.
  3. The application name.
  4. The session data. This is a serialized string.
  5. Whether the session is valid, as a single byte.
  6. The maximum permitted inactivity time, as a 32-bit integer measured in seconds.
  7. The last access time, as a 64-bit integer.

The following table satisfies those specifications; create it now before proceeding:

CREATE TABLE tomcat_session
(
id VARCHAR(32) NOT NULL,
app VARCHAR(255),
data LONGBLOB,
valid_session CHAR(1) NOT NULL,
max_inactive INT NOT NULL,
update_time BIGINT NOT NULL,
PRIMARY KEY (id),
INDEX (app)
);

Place the JDBC driver where Tomcat can find it.

Because Tomcat itself manages sessions, it must be able to access the JDBC driver

used to store sessions in a database. It’s common to install drivers in the lib directory

of the Tomcat tree so that they’re available both to Tomcat and to applications.(备注:如果war中中已经有引用 mysql jdbc driver 则不需要专门将驱动jar包拷贝到 tomcat 的lib 目录下)

Modify the Tomcat configuration file.

To tell Tomcat to use the tomcat_session table, modify the mcb application context

file. Change location into the webapps/mcb/META-INF under the Tomcat we

bapps directory, copy context.xml.jdbc to context.xml, and restart Tomcat.

If you look in context.xml, you’ll find a <Context> element containing a <Manager> element that specifies the use of JDBC for MySQL-based session storage:

<Manager
className="org.apache.catalina.session.PersistentManager"
saveOnRestart="true"
maxIdleBackup="600"
maxIdleSwap="1200"
minIdleSwap="900">
<Store
className="org.apache.catalina.session.JDBCStore"
driverName="com.mysql.jdbc.Driver"
connectionURL=
"jdbc:mysql://localhost/cookbook?user=cbuser&amp;password=cbpass&amp;useSSL=false"
sessionTable="tomcat_session"
sessionIdCol="id"
sessionAppCol="app"
sessionDataCol="data"
sessionValidCol="valid_session"
sessionMaxInactiveCol="max_inactive"
sessionLastAccessedCol="update_time"
/>
</Manager>

The <Manager> element attributes specify general session-related options. Within the

<Manager> element body, the <Store> element provides attributes pertaining to the

JDBC driver. The following discussion focuses on the attributes shown in the example,

but there are others you can use. For more information, see the Tomcat session-

management documentation.

The <Manager> attributes shown in the example have the following meanings:

  • className:The Java class that implements persistent session storage. It must be

    org.apache.catalina.session.PersistentManager .
  • saveOnRestart:Whether application sessions survive server restarts. Set it to true to have Tomcat

    save current sessions when it shuts down (and reload them when it starts up).
  • maxIdleBackup:The number of seconds before inactive sessions are eligible for being saved to MySQL. A value of -1 (the default) means “never.”
  • maxIdleSwap:The number of seconds before idle sessions should be swapped (saved to MySQL and passivated out of server memory). A value of -1 (the default) means “never.”

    If not -1 , the value should be at least as great as maxIdleBackup .
  • minIdleSwap:The number of seconds before idle sessions are eligible to be swapped. A value of -1 (the default) means “never.” If not -1 , the value should be less than maxIdleSwap

Within the <Manager> element, the <Store> element indicates how to connect to the

database server, the names of the database and table for storing session records, and the

names of the columns in the table:

  • className:The name of a class that implements the org.apache.catalina.Store interface.

    For JDBC-based storage managers, the value is org.apache.catalina.session.JDBCStore .

  • driverName:The class name for the JDBC driver. For the Connector/J driver, the value is com.mysql.jdbc.Driver.

  • connectionURL:The URL for connecting to the database server, with characters that are special in XML properly encoded. The following URL connects to the MySQL server on the local host, using a database, username, and password of cookbook , cbuser , and cbpass , respectively. Notice that the & character that separates the user and pass word connection parameters is written as the & entity: jdbc:mysql://localhost/cookbook?user=cbuser&password=cbpass

  • sessionTable The table in which to store session records. For our example, this is the tomcat_session table described earlier.

(The database that contains the table appears in the connectionURL value.) The remaining attributes in the example indicate the column names in the session table. These attributes are sessionIdCol , sessionAppCol , sessionDataCol, sessionValidCol , sessionMaxInactiveCol , and sessionLastAccessedCol , which correspond in the obvious way to columns of the tomcat_session table.

可能存在的问题

MySQL 大版本指尖引用类名包路径改变

此部分内容摘自:com.mysql.jdbc.Driver 和 com.mysql.cj.jdbc.Driver的区别 serverTimezone设定

com.mysql.jdbc.Driver 是 mysql-connector-java 5中的,

com.mysql.cj.jdbc.Driver 是 mysql-connector-java 6中的

1、JDBC连接Mysql5 com.mysql.jdbc.Driver:

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false
username=root
password=1234

2、JDBC连接Mysql6 com.mysql.cj.jdbc.Driver, 需要指定时区serverTimezone:

driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false
username=root
password=1234

在设定时区的时候,如果设定serverTimezone=UTC,会比中国时间早8个小时,如果在中国,可以选择Asia/Shanghai或者Asia/Hongkong,例如:

driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?serverTimezone=Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false
username=root
password=

Java连接MySQL数据库,提示Establishing SSL connection without警告

此内容摘自:Java连接MySQL数据库,提示Establishing SSL connection without警告

Java在连接MySQL数据库时,输出如下警告信息**

Tue Jul 11 18:04:07 CST 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

解决办法

  1. 在jdbc连接后添加 useSSL=false 参数
url=jdbc:mysql://localhost:3306/es?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&useSSL=false
  1. 如果以上办法无效,则降低 mysql-connector-java 依赖版本,如下
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>

总结

一般来说小版本之间的跨度不影响使用,但是大版本之间的使用差别将会很大,所以得要确认MySQL的版本并找到对应最合适的驱动。

tomcat 默认是将这部分session相关的信息放在文件里边的,通过上述的配置能够将对应的信息放到MySQL中,如果大并发大数据量的情况下性能应该更好一些。实际上如果有多个tomcat,可以让这些Tomcat都连接到该数据库,则可以实现分布式session的共享。当然在大并发大数据的情况下往往更好的做法是将session的信息放到redis 中,性能应该会更好一些。

欢迎转载,但请注明本文链接,谢谢你。

2018.8.19 17:57

Tomcat生成的session持久化到MySQL的更多相关文章

  1. Tomcat下 session 持久化问题(重启服务器session 仍然存在)

    感谢大佬:https://www.iteye.com/blog/xiaolongfeixiang-560800 关于在线人数统计,大都使用SessionListener监听器实现. SessionLi ...

  2. [转]session 持久化问题(重启服务器session 仍然存在)

    转:http://xiaolongfeixiang.iteye.com/blog/560800 关于在线人数统计,大都使用SessionListener监听器实现. SessionListener 触 ...

  3. Tomcat 之session 持久化2

    通过前文 Tomcat 之session 持久化1 ,我们已经大概了解了这么个机制.但是我没能详细展开其底层的原理. 这篇文章,我想稍微深入一点点,再继续聊一聊其底层. Tomcat 之session ...

  4. 【Session】Tomcat中Session持久化到文件系统或数据库

    参考的优秀文章 Tomcat Session 持久化 Package org.apache.catalina.session 最近同事在做Session外置的功能,我对Session持久化.共享也不太 ...

  5. Tomcat 之session 持久化1

    Tomcat 之session 持久化原理 几个概念: Manager 接口,其实就是指的是对  其Sesison 的管理, 其默认实现是StandardManager (内部没有任何Store对象实 ...

  6. tomcat架构分析 (Session管理)

    Session管理是JavaEE容器比较重要的一部分,在app中也经常会用到.在开发app时,我们只是获取一个session,然后向session中存取数据,然后再销毁session.那么如何产生se ...

  7. Tomcat容器的Session管理

    Session管理是JavaEE容器比较重要的一部分,在app中也经常会用到.在开发app时,我们只是获取一个session,然后向session中存取数据,然后再销毁session.那么如何产生se ...

  8. Tomcat 中的 Session 和 Cookie

    HTTP 是一种无状态通信协议,每个请求之间相互独立,服务器不能识别曾经来过的请求.而对于 Web 应用,它的活动都是依赖某个状态的,比如用户登录,此时使用 HTTP 就需要它在一次登录请求后,有为后 ...

  9. 三大框架 之 Hibernate生成策略与缓存策略(主键生成策略、持久化、持久化类划分、一级缓存、事物管理)

    目录 Hibernate生成策略与缓存策略 主键生成策略 主键分类 主键的生成策略 持久化 什么是持久化 什么是持久化类 持久化类编写规则 持久化类的划分 三种状态区分 持久态对象特征 一级缓存 什么 ...

随机推荐

  1. 简易OA漫谈之工作流设计(四,怎么支持会签)

    会签:就是一个步骤要所有人都审批完成才能完成. 上一步提交到会签步骤时,该步骤的每个人都能收到一条待办任务, 会签审批人每次提交时都检查该步骤所有会签人是否已经审批,如果全部审批就产生下一手任务. 演 ...

  2. select top 1 和select top 1 with ties * from SC 的区别

    select top1 : * FROM SC ORDER BY score DESC 运行结果如下: sc表数据查询select top 1 S# C# Score 001 03 120 WITH ...

  3. Rhino学习教程——1.4

    状态栏 状态栏位于整个工作界面的下方,主要显示了一些系统操作时的信息. 根据不同的功能,可以将状态栏分成4个部分. 一.坐标系统 在状态栏左侧显示了当前所使用的坐标系统(“世界”或“工作平面”,可以通 ...

  4. jjava:将jar包引入环境变量的一个骚操作以及因此搞出来的扑街问题

    现在我有一个java文件,我只想javac启动,但是这货import了一堆jar里面的东西. 于是我下回了所有的jar包,将这些jar包丢到jdk1.8.0_162\jre\lib\ext里面就ok了 ...

  5. Problem after converting keras model into Tensorflow pb - 将keras模型转换为Tensorflow pb后的问题

    I'm using keras 2.1.* with tensorflow 1.13.* backend. I save my model during training with .h5 forma ...

  6. springboot解决文件上传大小限制

    (1)在配置文件(application.properties)加入如下代码 springboot2.0以下配置为: spring.http.multipart.maxFileSize = 10Mb ...

  7. springboot 多端口启动

    以eclipse(STS)为例, 选中项目右键Run Configurations 点击Spring Boot App,选中需要设定多端口项目,在启动参数一栏输入:-Dserver.port=7003 ...

  8. C# 比较两文本相似度

    这个比较文本用到的主要是余弦定理比较文本相似度,具体原理右转某度,主要适用场景是在考试系统中的简答题概述,可根据权重自动打分,感觉实用性蛮广的. 先说下思路: 文本分词,中文于英文不同,规范的英文每个 ...

  9. 【基于微信小程序的社区电商平台】需求分析心得——小豆芽

    一.项目内容 基于微信小程序,做一个社区电商平台,抓住社区电商的特点,做出特色,与微信集成,实现商品的个性化发布,以及个性化营销. 个性化发布:用户可以在应用上直接发布自己的商品,通过搜索心愿单可以查 ...

  10. 如何将Team Viewer 从商务版重新安装成个人/非商务版 变成免费版本

    问题分析: 由于安装时选择如何使用 Team Viewer13 时,选择商务用途,则其为非免费的,只能试用一段时间,后面想要转成个人/非商务用途,则不行,因为 其安装时绑定了该电脑的 MAC地址(网卡 ...