Use SQLErrorCodeSQLExceptionTranslator in JdbcTemplate

Closes gh-2108
This commit is contained in:
Marcus Da Coregio
2022-07-18 09:04:16 -03:00
committed by Marcus Hert Da Coregio
parent 65b994cad1
commit cc4a15db79
3 changed files with 22 additions and 2 deletions

View File

@@ -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);
}
}
}
}

View File

@@ -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;
}

View File

@@ -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();