Introduce JdbcTransactionManager with SQLExceptionTranslator support
Closes gh-24064
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 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.
|
||||
@@ -63,6 +63,7 @@ import static org.springframework.core.testfixture.TestGroup.PERFORMANCE;
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @since 04.07.2003
|
||||
* @see org.springframework.jdbc.support.JdbcTransactionManagerTests
|
||||
*/
|
||||
public class DataSourceTransactionManagerTests {
|
||||
|
||||
@@ -284,8 +285,7 @@ public class DataSourceTransactionManagerTests {
|
||||
boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
|
||||
assertThat(condition1).as("Synchronization not active").isTrue();
|
||||
|
||||
ConnectionHolder conHolder = new ConnectionHolder(con);
|
||||
conHolder.setTransactionActive(true);
|
||||
ConnectionHolder conHolder = new ConnectionHolder(con, true);
|
||||
TransactionSynchronizationManager.bindResource(ds, conHolder);
|
||||
final RuntimeException ex = new RuntimeException("Application exception");
|
||||
try {
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -21,7 +21,6 @@ import java.sql.SQLException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.jdbc.BadSqlGrammarException;
|
||||
import org.springframework.jdbc.UncategorizedSQLException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@@ -54,14 +53,7 @@ public class SQLStateExceptionTranslatorTests {
|
||||
@Test
|
||||
public void invalidSqlStateCode() {
|
||||
SQLException sex = new SQLException("Message", "NO SUCH CODE", 1);
|
||||
try {
|
||||
throw this.trans.translate("task", sql, sex);
|
||||
}
|
||||
catch (UncategorizedSQLException ex) {
|
||||
// OK
|
||||
assertThat(sql.equals(ex.getSql())).as("SQL is correct").isTrue();
|
||||
assertThat(sex.equals(ex.getSQLException())).as("Exception matches").isTrue();
|
||||
}
|
||||
assertThat(this.trans.translate("task", sql, sex)).isNull();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -72,26 +64,14 @@ public class SQLStateExceptionTranslatorTests {
|
||||
@Test
|
||||
public void malformedSqlStateCodes() {
|
||||
SQLException sex = new SQLException("Message", null, 1);
|
||||
testMalformedSqlStateCode(sex);
|
||||
assertThat(this.trans.translate("task", sql, sex)).isNull();
|
||||
|
||||
sex = new SQLException("Message", "", 1);
|
||||
testMalformedSqlStateCode(sex);
|
||||
assertThat(this.trans.translate("task", sql, sex)).isNull();
|
||||
|
||||
// One char's not allowed
|
||||
sex = new SQLException("Message", "I", 1);
|
||||
testMalformedSqlStateCode(sex);
|
||||
}
|
||||
|
||||
|
||||
private void testMalformedSqlStateCode(SQLException sex) {
|
||||
try {
|
||||
throw this.trans.translate("task", sql, sex);
|
||||
}
|
||||
catch (UncategorizedSQLException ex) {
|
||||
// OK
|
||||
assertThat(sql.equals(ex.getSql())).as("SQL is correct").isTrue();
|
||||
assertThat(sex.equals(ex.getSQLException())).as("Exception matches").isTrue();
|
||||
}
|
||||
assertThat(this.trans.translate("task", sql, sex)).isNull();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 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.
|
||||
@@ -26,7 +26,6 @@ import org.springframework.dao.DataAccessResourceFailureException;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.dao.TransientDataAccessResourceException;
|
||||
import org.springframework.jdbc.BadSqlGrammarException;
|
||||
import org.springframework.jdbc.UncategorizedSQLException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
@@ -46,39 +45,39 @@ public class SQLStateSQLExceptionTranslatorTests {
|
||||
|
||||
|
||||
@Test
|
||||
public void testTranslateNullException() throws Exception {
|
||||
public void testTranslateNullException() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
||||
new SQLStateSQLExceptionTranslator().translate("", "", null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTranslateBadSqlGrammar() throws Exception {
|
||||
public void testTranslateBadSqlGrammar() {
|
||||
doTest("07", BadSqlGrammarException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTranslateDataIntegrityViolation() throws Exception {
|
||||
public void testTranslateDataIntegrityViolation() {
|
||||
doTest("23", DataIntegrityViolationException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTranslateDataAccessResourceFailure() throws Exception {
|
||||
public void testTranslateDataAccessResourceFailure() {
|
||||
doTest("53", DataAccessResourceFailureException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTranslateTransientDataAccessResourceFailure() throws Exception {
|
||||
public void testTranslateTransientDataAccessResourceFailure() {
|
||||
doTest("S1", TransientDataAccessResourceException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTranslateConcurrencyFailure() throws Exception {
|
||||
public void testTranslateConcurrencyFailure() {
|
||||
doTest("40", ConcurrencyFailureException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTranslateUncategorized() throws Exception {
|
||||
doTest("00000000", UncategorizedSQLException.class);
|
||||
public void testTranslateUncategorized() {
|
||||
assertThat(new SQLStateSQLExceptionTranslator().translate("", "", new SQLException(REASON, "00000000"))).isNull();
|
||||
}
|
||||
|
||||
|
||||
@@ -86,7 +85,7 @@ public class SQLStateSQLExceptionTranslatorTests {
|
||||
SQLException ex = new SQLException(REASON, sqlState);
|
||||
SQLExceptionTranslator translator = new SQLStateSQLExceptionTranslator();
|
||||
DataAccessException dax = translator.translate(TASK, SQL, ex);
|
||||
assertThat(dax).as("Translation must *never* result in a null DataAccessException being returned.").isNotNull();
|
||||
assertThat(dax).as("Specific translation must not result in a null DataAccessException being returned.").isNotNull();
|
||||
assertThat(dax.getClass()).as("Wrong DataAccessException type returned as the result of the translation").isEqualTo(dataAccessExceptionType);
|
||||
assertThat(dax.getCause()).as("The original SQLException must be preserved in the translated DataAccessException").isNotNull();
|
||||
assertThat(dax.getCause()).as("The exact same original SQLException must be preserved in the translated DataAccessException").isSameAs(ex);
|
||||
|
||||
Reference in New Issue
Block a user