Spring session with JDBC

Save session attributes in MySQL database

Zinedine Benkhider
j2ee-spring
1 min readJan 2, 2021

--

Spring Session provides an API and implementations for managing a user’s session information while also making it trivial to support clustered sessions without being tied to an application container-specific solution.

In this article we will show how to provides SessionRepository implementation backed by a relational database and configuration support. We will save session attributes in a MySQL database.

Maven Dependencies

You need to add following dependencies in your application’s pom.xml file:

Spring JDBC session configuration

Before to use JDBC backed spring session, we need to add the following properties in application.properties file:

HttpSession in Controller

An object of HttpSession can be used to perform the task: view and manipulate information about a session, such as the session identifier, creation time, and last accessed time. The parameter HttpSession of controller methods represent the current session associated with this request, or if the request does not have a session, it will be created.

Let’s create a simple controller to see session handling in action:

You find source code here:

--

--

Zinedine Benkhider
j2ee-spring

I am passionate about WEB and Mobile development. Very curious to learn and I like to share my knowledge.