Skip to content

Commit 782e5ee

Browse files
Add How to Customize Expired Sessions Clean-up Job
Issue gh-2827
1 parent 2372410 commit 782e5ee

File tree

1 file changed

+92
-6
lines changed
  • spring-session-docs/modules/ROOT/pages/configuration

1 file changed

+92
-6
lines changed

spring-session-docs/modules/ROOT/pages/configuration/jdbc.adoc

Lines changed: 92 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
[[jdbc-configurations]]
22
= JDBC
33

4-
Now that you have your application xref:guides/boot-jdbc.adoc[configured to use JDBC], you might want to start customizing things:
4+
Spring Session JDBC is a module that enables session management using https://en.wikipedia.org/wiki/Java_Database_Connectivity[JDBC] as the data store.
55

6-
- I want to use Spring Session JDBC
6+
- I want to <<adding-spring-session-jdbc,use Spring Session JDBC>>
77
- I want to <<session-storage-details,know how is the JDBC schema defined>>
88
- I want to <<customizing-table-name,customize the table name>>
99
- I want to <<customize-sql-queries,customize the SQL queries>>
1010
- I want to save the <<session-attributes-as-json,session attributes as JSON>> instead of an array of bytes
1111
- I want to <<specifying-datasource,use a different `DataSource`>> for Spring Session JDBC
1212
- I want to <<customizing-transaction-operations,customize the JDBC transactions>>
13+
- I want to customize the <<customizing-cleanup-job,expired sessions clean-up job>>
1314

1415
[[adding-spring-session-jdbc]]
1516
== Adding Spring Session JDBC To Your Application
@@ -177,11 +178,11 @@ Spring Session JDBC ships with a few implementations of `SessionRepositoryCustom
177178
[[session-attributes-as-json]]
178179
== Saving Session Attributes as JSON
179180

180-
Sometimes it is useful to save the session attributes in different formats, like JSON, which might have native support in the RDBMS allowing better function and operators compatibility in SQL queries.
181-
182181
By default, Spring Session JDBC saves the session attributes values as an array of bytes, such array is result from the JDK Serialization of the attribute value.
183182

184-
For this example, we are going to use https://www.postgresql.org/[PostgreSQL] as our RDBMS.
183+
Sometimes it is useful to save the session attributes in different formats, like JSON, which might have native support in the RDBMS allowing better function and operators compatibility in SQL queries.
184+
185+
For this example, we are going to use https://www.postgresql.org/[PostgreSQL] as our RDBMS as well as serializing the session attribute values using JSON instead of JDK serialization.
185186
Let's start by creating the `SPRING_SESSION_ATTRIBUTES` table with a `jsonb` type for the `attribute_values` column.
186187

187188
[tabs]
@@ -312,7 +313,7 @@ public class SessionConfig {
312313
<1> Uses the https://www.postgresql.org/docs/current/functions-binarystring.html[PostgreSQL encode] function to convert from `bytea` to `text`
313314

314315
And that's it, you should now be able to see the session attributes saved as JSON in the database.
315-
There is a sample available where you can see the whole implementation and run the tests.
316+
There is a https://github.com/spring-projects/spring-session/tree/main/spring-session-samples/spring-session-sample-boot-jdbc-json-attribute[sample available] where you can see the whole implementation and run the tests.
316317

317318
[[specifying-datasource]]
318319
== Specifying an alternative `DataSource`
@@ -409,3 +410,88 @@ public class SessionConfig {
409410
----
410411
======
411412

413+
[[customizing-cleanup-job]]
414+
== Customizing the Expired Sessions Clean-Up Job
415+
416+
In order to avoid overloading your database with expired sessions, Spring Session JDBC executes a clean-up job every minute that deletes the expired sessions (and its attributes).
417+
There are several reasons that you might want to customize the clean-up job, let's see the most common in the following sections.
418+
However, the customizations on the default job are limited, and that is intentional, Spring Session is not meant to provide a robust batch processing since there are a lot of frameworks or libraries that do a better job at that.
419+
Therefore, if you want more customization power, consider <<disabling-the-job,disabling the default job>> and providing your own.
420+
A good alternative is to use https://docs.spring.io/spring-batch/reference/spring-batch-intro.html[Spring Batch] which provides a robust solution for batch processing applications.
421+
422+
[[customizing-cleaned-up-frequency]]
423+
=== Customizing How Often Expired Sessions Are Cleaned Up
424+
425+
You can customize the {spring-framework-ref-docs}/integration/scheduling.html#scheduling-cron-expression[cron expression] that defines how often the clean-up job runs by using the `cleanupCron` attribute in `@EnableJdbcHttpSession`:
426+
427+
[tabs]
428+
======
429+
Java::
430+
+
431+
[source,java]
432+
----
433+
@Configuration
434+
@EnableJdbcHttpSession(cleanupCron = "0 0 * * * *") // top of every hour of every day
435+
public class SessionConfig {
436+
437+
}
438+
----
439+
======
440+
441+
Or, if you are using Spring Boot, set the `spring.session.jdbc.cleanup-cron` property:
442+
443+
[tabs]
444+
======
445+
application.properties::
446+
+
447+
[source,properties]
448+
----
449+
spring.session.jdbc.cleanup-cron="0 0 * * * *"
450+
----
451+
======
452+
453+
[[disabling-the-job]]
454+
=== Disabling the Job
455+
456+
To disable the job you must pass `Scheduled.CRON_DISABLED` to the `cleanupCron` attribute in `@EnableJdbcHttpSession`:
457+
458+
[tabs]
459+
======
460+
Java::
461+
+
462+
[source,java]
463+
----
464+
@Configuration
465+
@EnableJdbcHttpSession(cleanupCron = Scheduled.CRON_DISABLED)
466+
public class SessionConfig {
467+
468+
}
469+
----
470+
======
471+
472+
=== Customizing the Delete By Expiry Time Query
473+
474+
You can customize the query that deletes expired sessions by using `JdbcIndexedSessionRepository.setDeleteSessionsByExpiryTimeQuery` through a `SessionRepositoryCustomizer<JdbcIndexedSessionRepository>` bean:
475+
476+
[tabs]
477+
======
478+
Java::
479+
+
480+
[source,java]
481+
----
482+
@Configuration
483+
@EnableJdbcHttpSession
484+
public class SessionConfig {
485+
486+
@Bean
487+
public SessionRepositoryCustomizer<JdbcIndexedSessionRepository> customizer() {
488+
return (sessionRepository) -> sessionRepository.setDeleteSessionsByExpiryTimeQuery("""
489+
DELETE FROM %TABLE_NAME%
490+
WHERE EXPIRY_TIME < ?
491+
AND OTHER_COLUMN = 'value'
492+
""");
493+
}
494+
495+
}
496+
----
497+
======

0 commit comments

Comments
 (0)