You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: spring-session-docs/modules/ROOT/pages/configuration/jdbc.adoc
+7-4Lines changed: 7 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -155,13 +155,13 @@ public class QueryCustomizer
155
155
private static final String CREATE_SESSION_ATTRIBUTE_QUERY = """
156
156
INSERT INTO %TABLE_NAME%_ATTRIBUTES (SESSION_PRIMARY_ID, ATTRIBUTE_NAME, ATTRIBUTE_BYTES) <1>
157
157
VALUES (?, ?, ?)
158
-
ON CONFLICT (SESSION_PRIMARY_ID, ATTRIBUTE_NAME)
159
-
DO NOTHING
158
+
ON CONFLICT (SESSION_PRIMARY_ID, ATTRIBUTE_NAME) <2>
159
+
DO UPDATE SET ATTRIBUTE_BYTES = EXCLUDED.ATTRIBUTE_BYTES
160
160
""";
161
161
162
162
private static final String UPDATE_SESSION_ATTRIBUTE_QUERY = """
163
163
UPDATE %TABLE_NAME%_ATTRIBUTES
164
-
SET ATTRIBUTE_BYTES = convert_from(?, 'UTF8')::jsonb
164
+
SET ATTRIBUTE_BYTES = encode(?, 'escape')
165
165
WHERE SESSION_PRIMARY_ID = ?
166
166
AND ATTRIBUTE_NAME = ?
167
167
""";
@@ -177,6 +177,7 @@ public class QueryCustomizer
177
177
======
178
178
179
179
<1> The `%TABLE_NAME%` placeholder in the query will be replaced by the configured table name being used by `JdbcIndexedSessionRepository`.
180
+
<2> `ON CONFLICT` handles duplicate keys by updating the existing row instead of throwing an error, making the insert operation idempotent.
180
181
181
182
[TIP]
182
183
====
@@ -310,6 +311,8 @@ public class SessionConfig {
310
311
private static final String CREATE_SESSION_ATTRIBUTE_QUERY = """
311
312
INSERT INTO %TABLE_NAME%_ATTRIBUTES (SESSION_PRIMARY_ID, ATTRIBUTE_NAME, ATTRIBUTE_BYTES)
312
313
VALUES (?, ?, convert_from(?, 'UTF8')::jsonb) <1>
314
+
ON CONFLICT (SESSION_PRIMARY_ID, ATTRIBUTE_NAME)
315
+
DO UPDATE SET ATTRIBUTE_BYTES = EXCLUDED.ATTRIBUTE_BYTES
313
316
""";
314
317
315
318
private static final String UPDATE_SESSION_ATTRIBUTE_QUERY = """
@@ -331,7 +334,7 @@ public class SessionConfig {
331
334
----
332
335
======
333
336
334
-
<1> Uses the https://www.postgresql.org/docs/current/functions-binarystring.html[PostgreSQL encode] function to convert from `bytea` to `text`
337
+
<1> Uses the https://www.postgresql.org/docs/current/functions-binarystring.html[PostgreSQL convert_from] function to convert the `bytea` value to `text`, which is then cast to `jsonb`.
335
338
336
339
And that's it, you should now be able to see the session attributes saved as JSON in the database.
337
340
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.
Copy file name to clipboardExpand all lines: spring-session-samples/spring-session-sample-boot-jdbc-json-attribute/src/main/java/sample/config/SessionConfig.java
+3-1Lines changed: 3 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -24,11 +24,13 @@ public class SessionConfig implements BeanClassLoaderAware {
0 commit comments