Class JdbcSessionRepository

java.lang.Object
org.springframework.ai.session.jdbc.JdbcSessionRepository
All Implemented Interfaces:
SessionRepository

public final class JdbcSessionRepository extends Object implements SessionRepository
The SessionRepository on a relational database: AI_SESSION (id, expiry, metadata JSON, event version), AI_SESSION_INDEX (the indexed metadata attributes, one row per attribute) and AI_SESSION_EVENT (the log, ordered by a database-assigned seq). The schema scripts are under classpath:org/springframework/ai/session/jdbc/.

On every save the metadata entries whose keys are indexed, the principal by default, are copied into AI_SESSION_INDEX, on the first 512 characters of the value, and findByMetadata(java.lang.String, java.lang.String) is one query over that table, with the whole value checked on the sessions it returns. An event is stored in the four columns SessionEventCodec defines: the event type, the message type, the message text, its own column so a store can search it, and a JSON payload.

The event_version column of AI_SESSION advances on every appendEvent(org.springframework.ai.session.SessionEvent) that appends a new event and on every successful appendEvents(java.lang.String, java.util.List<org.springframework.ai.session.SessionEvent>, long), which claims the version with UPDATE … WHERE event_version = ? before inserting. appendEvent(org.springframework.ai.session.SessionEvent) looks the event id up under that row lock before inserting, so a replay is a no-op without a duplicate-key error, which would mark a caller's surrounding transaction rollback-only, or abort it on PostgreSQL. A read of the active view selects the newest COMPACTION row, fetches the rows it names plus every row from it on, and lets EventFilter.apply(List) order, filter and window them. Every mutation runs in a TransactionTemplate; the class is thread-safe as the DataSource is.

Since:
2.1.0
Author:
Christian Tzolov