From 8e866890cbb408b3d4666b9ae68f07e2eaa3dd3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ru=CC=88diger=20Schulz?= Date: Tue, 7 Jan 2025 20:15:52 +0100 Subject: [PATCH] #3299: use postgres function convert_from() instead of encode() to insert JSON byte stream into PreparedStatement. This works with session attributes containing special characters in the JSON blob. --- .../modules/ROOT/pages/configuration/jdbc.adoc | 6 +++--- .../src/main/java/sample/config/SessionConfig.java | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/spring-session-docs/modules/ROOT/pages/configuration/jdbc.adoc b/spring-session-docs/modules/ROOT/pages/configuration/jdbc.adoc index b8420e0d7..af51a4247 100644 --- a/spring-session-docs/modules/ROOT/pages/configuration/jdbc.adoc +++ b/spring-session-docs/modules/ROOT/pages/configuration/jdbc.adoc @@ -161,7 +161,7 @@ public class QueryCustomizer private static final String UPDATE_SESSION_ATTRIBUTE_QUERY = """ UPDATE %TABLE_NAME%_ATTRIBUTES - SET ATTRIBUTE_BYTES = encode(?, 'escape')::jsonb + SET ATTRIBUTE_BYTES = convert_from(?, 'UTF8')::jsonb WHERE SESSION_PRIMARY_ID = ? AND ATTRIBUTE_NAME = ? """; @@ -309,12 +309,12 @@ public class SessionConfig { private static final String CREATE_SESSION_ATTRIBUTE_QUERY = """ INSERT INTO %TABLE_NAME%_ATTRIBUTES (SESSION_PRIMARY_ID, ATTRIBUTE_NAME, ATTRIBUTE_BYTES) - VALUES (?, ?, encode(?, 'escape')::jsonb) <1> + VALUES (?, ?, convert_from(?, 'UTF8')::jsonb) <1> """; private static final String UPDATE_SESSION_ATTRIBUTE_QUERY = """ UPDATE %TABLE_NAME%_ATTRIBUTES - SET ATTRIBUTE_BYTES = encode(?, 'escape')::jsonb + SET ATTRIBUTE_BYTES = convert_from(?, 'UTF8')::jsonb WHERE SESSION_PRIMARY_ID = ? AND ATTRIBUTE_NAME = ? """; diff --git a/spring-session-samples/spring-session-sample-boot-jdbc-json-attribute/src/main/java/sample/config/SessionConfig.java b/spring-session-samples/spring-session-sample-boot-jdbc-json-attribute/src/main/java/sample/config/SessionConfig.java index ee2f4e507..75c4792a0 100644 --- a/spring-session-samples/spring-session-sample-boot-jdbc-json-attribute/src/main/java/sample/config/SessionConfig.java +++ b/spring-session-samples/spring-session-sample-boot-jdbc-json-attribute/src/main/java/sample/config/SessionConfig.java @@ -23,7 +23,7 @@ public class SessionConfig implements BeanClassLoaderAware { private static final String CREATE_SESSION_ATTRIBUTE_QUERY = """ INSERT INTO %TABLE_NAME%_ATTRIBUTES (SESSION_PRIMARY_ID, ATTRIBUTE_NAME, ATTRIBUTE_BYTES) - VALUES (?, ?, encode(?, 'escape')::jsonb) + VALUES (?, ?, convert_from(?, 'UTF8')::jsonb) """; private ClassLoader classLoader;