Tomcat生成的session持久化到MySQL

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用系列 2核4GB
简介: Telling Tomcat to save session records in MySQL此部分内容摘自 MySQL cookbook 3th。具体内容不做翻译,哈哈,懒The default Tomcat default session storage mechanism uses temporary files.

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

可能存在的问题

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

相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
5月前
|
关系型数据库 MySQL 数据库
百度搜索:蓝易云【【Docker】Docker部署Mysql并设置数据持久化教程】
通过以上步骤,您已经成功地在Docker中部署了MySQL,并设置了数据持久化,确保数据在容器重新启动或迁移时得以保留。
88 0
|
2月前
|
关系型数据库 Java MySQL
Linux安装JDK1.8 & tomcat & MariaDB(MySQL删减版)
本教程提供了在Linux环境下安装JDK1.8、Tomcat和MariaDB的详细步骤。这三个组件的组合为Java Web开发和部署提供了一个强大的基础。通过遵循这些简单的指导步骤,您可以轻松建立起一个稳定、高效的开发和部署环境。希望这个指导对您的开发工作有所帮助。
106 8
|
4月前
|
Java 关系型数据库 MySQL
杨校老师课堂之Java项目部署到云端服务器之安装MySQL、Jdk、Tomcat
杨校老师课堂之Java项目部署到云端服务器之安装MySQL、Jdk、Tomcat
47 0
杨校老师课堂之Java项目部署到云端服务器之安装MySQL、Jdk、Tomcat
|
5月前
|
存储 缓存 算法
MySQL持久化不为人知的一面⭐️卡顿现象的根源与对策
MySQL持久化不为人知的一面⭐️卡顿现象的根源与对策
|
5月前
|
关系型数据库 MySQL 应用服务中间件
centos7在线安装jdk1.8+tomcat+mysql8+nginx+docker
现在,你已经成功在CentOS 7上安装了JDK 1.8、Tomcat、MySQL 8、Nginx和Docker。你可以根据需要配置和使用这些服务。请注意,安装和配置这些服务的详细设置取决于你的具体需求。
464 2
|
5月前
|
关系型数据库 MySQL Java
Linux 安装 JDK、MySQL、Tomcat(图文并茂)
Linux 安装 JDK、MySQL、Tomcat(图文并茂)
323 2
|
5月前
|
JSON 前端开发 Java
管理系统总结(前端:Vue-cli, 后端Jdbc连接mysql数据库,项目部署tomcat里)
管理系统总结(前端:Vue-cli, 后端Jdbc连接mysql数据库,项目部署tomcat里)
|
5月前
|
架构师 Java 关系型数据库
一线架构师开发总结:剖析并发编程+JVM性能,深入Tomcat与MySQL
每一个程序员都有自己清晰的职业规划和终极目标,无论之后是继续钻研技术,还是转管理岗、产品岗,都是需要自己具备有一定的实力,换句话说技术要牛逼。架构师,是很多程序员的终极目标,而成为一名Java架构师,那就需要对自己自身有一定要求,不仅技术能力要过硬,还需要有组织能力和提出解决方案的能力。那么作为架构师,需要掌握哪些技术呢?
一线架构师开发总结:剖析并发编程+JVM性能,深入Tomcat与MySQL
|
Java 应用服务中间件 网络架构
Tomcat之Session管理
先了解Session,Cookie,JSESSIONID JSESSIONID是一个唯一标识号,用来标识服务器端的Session,也用来标识客户端的Cookie,客户端和服务器端通过这个JSESSIONID来一一对应。
1182 0
|
应用服务中间件 Apache 数据库
tomcat中的session管理
Session的管理当一个sesson开始时,Servlet容器会创建一个HttpSession对象,在某些情况下把这些Httpsession对象从内存中转移到文件系统中或数据库中,需要访问的时候在把它们载入到内存中来。
656 0
下一篇
无影云桌面