Leverage spring.ignore.xml flag to avoid SQLStateSQLExceptionTranslator

Closes gh-25335
This commit is contained in:
Sébastien Deleuze 2020-06-29 20:35:44 +02:00
parent a92441186c
commit 06b364fefe
2 changed files with 36 additions and 5 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -22,6 +22,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.SpringProperties;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -34,11 +35,19 @@ import org.springframework.util.Assert;
* See {@link org.springframework.jdbc.core.JdbcTemplate}. * See {@link org.springframework.jdbc.core.JdbcTemplate}.
* *
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Sebastien Deleuze
* @since 28.11.2003 * @since 28.11.2003
* @see org.springframework.jdbc.core.JdbcTemplate * @see org.springframework.jdbc.core.JdbcTemplate
*/ */
public abstract class JdbcAccessor implements InitializingBean { public abstract class JdbcAccessor implements InitializingBean {
/**
* Boolean flag controlled by a {@code spring.xml.ignore} system property that instructs Spring to
* ignore XML, i.e. to not initialize the XML-related infrastructure.
* <p>The default is "false".
*/
private static final boolean shouldIgnoreXml = SpringProperties.getFlag("spring.xml.ignore");
/** Logger available to subclasses. */ /** Logger available to subclasses. */
protected final Log logger = LogFactory.getLog(getClass()); protected final Log logger = LogFactory.getLog(getClass());
@ -87,8 +96,10 @@ public abstract class JdbcAccessor implements InitializingBean {
* @see java.sql.DatabaseMetaData#getDatabaseProductName() * @see java.sql.DatabaseMetaData#getDatabaseProductName()
*/ */
public void setDatabaseProductName(String dbName) { public void setDatabaseProductName(String dbName) {
if (!shouldIgnoreXml) {
this.exceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(dbName); this.exceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(dbName);
} }
}
/** /**
* Set the exception translator for this instance. * Set the exception translator for this instance.
@ -118,7 +129,10 @@ public abstract class JdbcAccessor implements InitializingBean {
exceptionTranslator = this.exceptionTranslator; exceptionTranslator = this.exceptionTranslator;
if (exceptionTranslator == null) { if (exceptionTranslator == null) {
DataSource dataSource = getDataSource(); DataSource dataSource = getDataSource();
if (dataSource != null) { if (shouldIgnoreXml) {
exceptionTranslator = new SQLExceptionSubclassTranslator();
}
else if (dataSource != null) {
exceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(dataSource); exceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(dataSource);
} }
else { else {

View File

@ -20,6 +20,7 @@ import java.sql.SQLException;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.springframework.core.SpringProperties;
import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
@ -43,6 +44,7 @@ import org.springframework.lang.Nullable;
* may also throw such exceptions in their {@code flush} and {@code beforeCommit} phases. * may also throw such exceptions in their {@code flush} and {@code beforeCommit} phases.
* *
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Sebastien Deleuze
* @since 5.3 * @since 5.3
* @see DataSourceTransactionManager * @see DataSourceTransactionManager
* @see #setDataSource * @see #setDataSource
@ -51,6 +53,14 @@ import org.springframework.lang.Nullable;
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class JdbcTransactionManager extends DataSourceTransactionManager { public class JdbcTransactionManager extends DataSourceTransactionManager {
/**
* Boolean flag controlled by a {@code spring.xml.ignore} system property that instructs Spring to
* ignore XML, i.e. to not initialize the XML-related infrastructure.
* <p>The default is "false".
*/
private static final boolean shouldIgnoreXml = SpringProperties.getFlag("spring.xml.ignore");
@Nullable @Nullable
private volatile SQLExceptionTranslator exceptionTranslator; private volatile SQLExceptionTranslator exceptionTranslator;
@ -87,8 +97,10 @@ public class JdbcTransactionManager extends DataSourceTransactionManager {
* @see java.sql.DatabaseMetaData#getDatabaseProductName() * @see java.sql.DatabaseMetaData#getDatabaseProductName()
*/ */
public void setDatabaseProductName(String dbName) { public void setDatabaseProductName(String dbName) {
if (!shouldIgnoreXml) {
this.exceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(dbName); this.exceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(dbName);
} }
}
/** /**
* Set the exception translator for this instance. * Set the exception translator for this instance.
@ -116,7 +128,12 @@ public class JdbcTransactionManager extends DataSourceTransactionManager {
synchronized (this) { synchronized (this) {
exceptionTranslator = this.exceptionTranslator; exceptionTranslator = this.exceptionTranslator;
if (exceptionTranslator == null) { if (exceptionTranslator == null) {
if (shouldIgnoreXml) {
exceptionTranslator = new SQLExceptionSubclassTranslator();
}
else {
exceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(obtainDataSource()); exceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(obtainDataSource());
}
this.exceptionTranslator = exceptionTranslator; this.exceptionTranslator = exceptionTranslator;
} }
return exceptionTranslator; return exceptionTranslator;