Languages
[Edit]
EN

Spring Boot - store session ID in MySQL database

8 points
Created by:
Yusef-Ewing
479

In this article, we would like to show how in Spring Boot 2, store session in MySQL database, preventing losing session when the server is restarted.

Note: the presented solution was tested under Spring Boot 2.4 that was working with Tomcat 9 and MySQL 8.

 

Simple steps:

1. Add JDBC session dependency to pom.xml:

Note: change 2.4.3 to your spring version.

 

2. Enable JDBC storage in application.properties file:

Notes:

  • my_database will be shared between session store and spring boot application,
  • spring.session.jdbc.initialize-schema=embedded disables tables auto creation - in step 3 you can find how to create tables by self (next step).

 

3. Execute the following query on MySQL database:

Note: the query is located under path /org/springframework/session/jdbc/schema-mysql.sql.

 

See also

Edit
  1. Spring Boot - persist session

Alternative titles

  1. Spring Boot - persist session (IDs stored in MySQL database)
  2. Spring Boot - save session in MySQL and prevent session loosing on Tomcat restart
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join