Tomcat-generated session persisted to MySQL

Source: Internet
Author: User
Tags session id mysql version sessions server memory ssl connection

Telling Tomcat to save session records in MySQL

This section is excerpted from MySQL cookbook 3th. Specific content do not translate, haha, lazy

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 this 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, 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 is 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 bes 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, they ' re available both to Tomcat and to applications. (Note: If you already have references to MySQL JDBC driver in the war, you do not need to specifically Drive jar package Copy to Tomcat's lib directory)

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 element, specifies the use of the <Manager> JDBC for MYSQ l-based Session Storage:

  <managerclassname= "Org.apache.catalina.session.PersistentManager" saveonrestart= "true "Maxidlebackup=" "maxidleswap=" "minidleswap=" "><store classname=" org.apache.catalina.session . Jdbcstore "Drivername=" Com.mysql.jdbc.Driver "connectionurl=" Jdbc:mysql://localhost/cookbook?user=cbuser&amp ;p assword=cbpass&amp;usessl=false "sessiontable=" tomcat_session "sessionidcol=" id "sessionAppCol=" app "ses Siondatacol= "Data" sessionvalidcol= "Valid_session" sessionmaxinactivecol= "max_inactive" sessionLastAccessedCol= "u Pdate_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 is others you can use. For more information, see the Tomcat session-
Management documentation.

The <Manager> attributes shown in the example has 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 having Tomcat
      Save current sessions if it shuts down (and reload them if it starts up).
    • maxidlebackup:the number of seconds before inactive sessions is eligible for being saved to MySQL. A value of-1 (the default) means "Never."
    • maxidleswap:the number of seconds before idle sessions should BES swapped (saved to MySQL and passivated out of server Memory). A value of-1 (the default) means "never."
      If not-1, the value should is at least as great as maxidlebackup.
    • minidleswap:the number of seconds before idle sessions is eligible to be swapped. A value of-1 (the default) means "never." If not-1, the value should is 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 T He 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.

    • The
    • connectionurl:the URL for connecting to the database server, with characters that is 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 are written as the & entity : Jdbc:mysql://localhost/cookbook?user=cbuser&password=cbpass
    • sessiontable the table in which to Store session Records. For we example, this is the Tomcat_session table described earlier.

(The database that contains the table appears in the Connectionurl value.) The remaining

Possible problems with MySQL large version fingertip reference class name package path change

This section is excerpted from the following: Com.mysql.jdbc.Driver and Com.mysql.cj.jdbc.Driver differences servertimezone settings

com.mysql.jdbc.Driveris in Mysql-connector-java 5,
com.mysql.cj.jdbc.DriverIt's in Mysql-connector-java 6.

1. JDBC Connection MYSQL5 com.mysql.jdbc.Driver :

driverClassName=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=falseusername=rootpassword=1234

2, JDBC Connection Mysql6 com.mysql.cj.jdbc.Driver , you need to specify the time zone Servertimezone:

driverClassName=com.mysql.cj.jdbc.Driverurl=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=falseusername=rootpassword=1234

When setting the time zone, if set SERVERTIMEZONE=UTC, will be 8 hours earlier than Chinese time, if in China, can choose Asia/shanghai or Asia/hongkong, for example:

driverClassName=com.mysql.cj.jdbc.Driverurl=jdbc:mysql://localhost:3306/test?serverTimezone=Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=falseusername=rootpassword=
Java connection to MySQL database, prompting establishing SSL connection without warning

This content is excerpted from: Java connection MySQL database, prompt establishing SSL connection without warning

Java when connecting to MySQL database, output the following warning message * *

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.

Solutions

    1. Adding parameters after a JDBC connection useSSL=false
url=jdbc:mysql://localhost:3306/es?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&useSSL=false
    1. If the above method is invalid, the mysql-connector-java dependent version is reduced, as follows
<dependency>    <groupId>mysql</groupId>    <artifactId>mysql-connector-java</artifactId>    <version>5.1.38</version></dependency>
Summarize

In general, the span between the minor versions does not affect use, but the usage difference between the large versions will be large, so you have to confirm the MySQL version and find the appropriate driver.

Tomcat by default is to put this part of the session related information in the file, through the above configuration can put the corresponding information into MySQL, if large concurrent large data volume, the performance should be better. In fact, if you have more than one tomcat that allows these tomcat to connect to the database, you can implement a distributed session sharing. Of course, in the case of large concurrency big data, it is often better to put the session information into the Redis, performance should be better.

Welcome reprint, but please indicate this article link, thank you.

2018.8.19 17:57

Tomcat-generated session persisted to MySQL

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.