Skip to content

Commit cc4a15d

Browse files
Use SQLErrorCodeSQLExceptionTranslator in JdbcTemplate
Closes gh-2108
1 parent 65b994c commit cc4a15d

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcIndexedSessionRepository.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2020 the original author or authors.
2+
* Copyright 2014-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -500,6 +500,9 @@ public int getBatchSize() {
500500
catch (DataIntegrityViolationException ex) {
501501
// parent record not found - we are ignoring this error because we
502502
// assume that a concurrent request has removed the session
503+
if (logger.isTraceEnabled()) {
504+
logger.trace("Not able to create session attributes", ex);
505+
}
503506
}
504507
}
505508
else {
@@ -517,6 +520,9 @@ public int getBatchSize() {
517520
catch (DataIntegrityViolationException ex) {
518521
// parent record not found - we are ignoring this error because we
519522
// assume that a concurrent request has removed the session
523+
if (logger.isTraceEnabled()) {
524+
logger.trace("Not able to create session attributes", ex);
525+
}
520526
}
521527
}
522528
}

spring-session-jdbc/src/main/java/org/springframework/session/jdbc/config/annotation/web/http/JdbcHttpSessionConfiguration.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,6 +39,7 @@
3939
import org.springframework.jdbc.core.JdbcTemplate;
4040
import org.springframework.jdbc.support.JdbcUtils;
4141
import org.springframework.jdbc.support.MetaDataAccessException;
42+
import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator;
4243
import org.springframework.jdbc.support.lob.DefaultLobHandler;
4344
import org.springframework.jdbc.support.lob.LobHandler;
4445
import org.springframework.scheduling.annotation.EnableScheduling;
@@ -259,6 +260,7 @@ public void setImportMetadata(AnnotationMetadata importMetadata) {
259260

260261
private static JdbcTemplate createJdbcTemplate(DataSource dataSource) {
261262
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
263+
jdbcTemplate.setExceptionTranslator(new SQLErrorCodeSQLExceptionTranslator(dataSource));
262264
jdbcTemplate.afterPropertiesSet();
263265
return jdbcTemplate;
264266
}

spring-session-jdbc/src/test/java/org/springframework/session/jdbc/config/annotation/web/http/JdbcHttpSessionConfigurationTests.java

+12
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import org.springframework.core.annotation.Order;
3333
import org.springframework.core.convert.ConversionService;
3434
import org.springframework.jdbc.core.JdbcOperations;
35+
import org.springframework.jdbc.core.JdbcTemplate;
36+
import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator;
3537
import org.springframework.jdbc.support.lob.LobHandler;
3638
import org.springframework.mock.env.MockEnvironment;
3739
import org.springframework.session.FlushMode;
@@ -300,6 +302,16 @@ void sessionRepositoryCustomizer() {
300302
MAX_INACTIVE_INTERVAL_IN_SECONDS);
301303
}
302304

305+
@Test
306+
void defaultConfigurationJdbcTemplateHasExpectedExceptionTranslator() {
307+
registerAndRefresh(DataSourceConfiguration.class, DefaultConfiguration.class);
308+
309+
JdbcIndexedSessionRepository repository = this.context.getBean(JdbcIndexedSessionRepository.class);
310+
JdbcTemplate jdbcTemplate = (JdbcTemplate) ReflectionTestUtils.getField(repository, "jdbcOperations");
311+
assertThat(jdbcTemplate).isNotNull();
312+
assertThat(jdbcTemplate.getExceptionTranslator()).isInstanceOf(SQLErrorCodeSQLExceptionTranslator.class);
313+
}
314+
303315
private void registerAndRefresh(Class<?>... annotatedClasses) {
304316
this.context.register(annotatedClasses);
305317
this.context.refresh();

0 commit comments

Comments
 (0)