From cc4a15db79fd49dc8e7172da4e072bf4c2c869bd Mon Sep 17 00:00:00 2001 From: Marcus Da Coregio Date: Mon, 18 Jul 2022 09:04:16 -0300 Subject: [PATCH] Use SQLErrorCodeSQLExceptionTranslator in JdbcTemplate Closes gh-2108 --- .../session/jdbc/JdbcIndexedSessionRepository.java | 8 +++++++- .../web/http/JdbcHttpSessionConfiguration.java | 4 +++- .../web/http/JdbcHttpSessionConfigurationTests.java | 12 ++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcIndexedSessionRepository.java b/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcIndexedSessionRepository.java index fb941a87..3ffb9731 100644 --- a/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcIndexedSessionRepository.java +++ b/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcIndexedSessionRepository.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2020 the original author or authors. + * Copyright 2014-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -500,6 +500,9 @@ public class JdbcIndexedSessionRepository catch (DataIntegrityViolationException ex) { // parent record not found - we are ignoring this error because we // assume that a concurrent request has removed the session + if (logger.isTraceEnabled()) { + logger.trace("Not able to create session attributes", ex); + } } } else { @@ -517,6 +520,9 @@ public class JdbcIndexedSessionRepository catch (DataIntegrityViolationException ex) { // parent record not found - we are ignoring this error because we // assume that a concurrent request has removed the session + if (logger.isTraceEnabled()) { + logger.trace("Not able to create session attributes", ex); + } } } } diff --git a/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/config/annotation/web/http/JdbcHttpSessionConfiguration.java b/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/config/annotation/web/http/JdbcHttpSessionConfiguration.java index 4d7c1a3c..35ab729e 100644 --- a/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/config/annotation/web/http/JdbcHttpSessionConfiguration.java +++ b/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/config/annotation/web/http/JdbcHttpSessionConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2019 the original author or authors. + * Copyright 2014-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -39,6 +39,7 @@ import org.springframework.core.type.AnnotationMetadata; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.support.JdbcUtils; import org.springframework.jdbc.support.MetaDataAccessException; +import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator; import org.springframework.jdbc.support.lob.DefaultLobHandler; import org.springframework.jdbc.support.lob.LobHandler; import org.springframework.scheduling.annotation.EnableScheduling; @@ -259,6 +260,7 @@ public class JdbcHttpSessionConfiguration extends SpringHttpSessionConfiguration private static JdbcTemplate createJdbcTemplate(DataSource dataSource) { JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); + jdbcTemplate.setExceptionTranslator(new SQLErrorCodeSQLExceptionTranslator(dataSource)); jdbcTemplate.afterPropertiesSet(); return jdbcTemplate; } diff --git a/spring-session-jdbc/src/test/java/org/springframework/session/jdbc/config/annotation/web/http/JdbcHttpSessionConfigurationTests.java b/spring-session-jdbc/src/test/java/org/springframework/session/jdbc/config/annotation/web/http/JdbcHttpSessionConfigurationTests.java index ba12af74..1461458c 100644 --- a/spring-session-jdbc/src/test/java/org/springframework/session/jdbc/config/annotation/web/http/JdbcHttpSessionConfigurationTests.java +++ b/spring-session-jdbc/src/test/java/org/springframework/session/jdbc/config/annotation/web/http/JdbcHttpSessionConfigurationTests.java @@ -32,6 +32,8 @@ import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.core.annotation.Order; import org.springframework.core.convert.ConversionService; import org.springframework.jdbc.core.JdbcOperations; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator; import org.springframework.jdbc.support.lob.LobHandler; import org.springframework.mock.env.MockEnvironment; import org.springframework.session.FlushMode; @@ -300,6 +302,16 @@ class JdbcHttpSessionConfigurationTests { MAX_INACTIVE_INTERVAL_IN_SECONDS); } + @Test + void defaultConfigurationJdbcTemplateHasExpectedExceptionTranslator() { + registerAndRefresh(DataSourceConfiguration.class, DefaultConfiguration.class); + + JdbcIndexedSessionRepository repository = this.context.getBean(JdbcIndexedSessionRepository.class); + JdbcTemplate jdbcTemplate = (JdbcTemplate) ReflectionTestUtils.getField(repository, "jdbcOperations"); + assertThat(jdbcTemplate).isNotNull(); + assertThat(jdbcTemplate.getExceptionTranslator()).isInstanceOf(SQLErrorCodeSQLExceptionTranslator.class); + } + private void registerAndRefresh(Class... annotatedClasses) { this.context.register(annotatedClasses); this.context.refresh();