diff --git a/core/src/main/java/com/alibaba/druid/pool/DruidDataSource.java b/core/src/main/java/com/alibaba/druid/pool/DruidDataSource.java index 4d58d62086..2879f4fcb5 100644 --- a/core/src/main/java/com/alibaba/druid/pool/DruidDataSource.java +++ b/core/src/main/java/com/alibaba/druid/pool/DruidDataSource.java @@ -147,6 +147,7 @@ public class DruidDataSource extends DruidAbstractDataSource private boolean asyncInit; protected boolean killWhenSocketReadTimeout; protected boolean checkExecuteTime; + protected boolean closeConnOnFatalError = true; private static List autoFilters; private boolean loadSpifilterSkip; @@ -185,6 +186,14 @@ public void setAsyncInit(boolean asyncInit) { this.asyncInit = asyncInit; } + public boolean isCloseConnOnFatalError() { + return closeConnOnFatalError; + } + + public void setCloseConnOnFatalError(boolean closeConnOnFatalError) { + this.closeConnOnFatalError = closeConnOnFatalError; + } + @Deprecated public void configFromPropety(Properties properties) { configFromPropeties(properties); @@ -1640,7 +1649,7 @@ protected final void handleFatalError( ReentrantLock fatalErrorCountLock = hasHolderDataSource ? holder.getDataSource().lock : conn.lock; fatalErrorCountLock.lock(); try { - if ((!conn.closed) && !conn.disable) { + if ((!conn.closed) && !conn.disable && isCloseConnOnFatalError()) { conn.disable(error); requireDiscard = true; } diff --git a/core/src/main/java/com/alibaba/druid/util/DruidDataSourceUtils.java b/core/src/main/java/com/alibaba/druid/util/DruidDataSourceUtils.java index ae249162e8..3f61a0154e 100644 --- a/core/src/main/java/com/alibaba/druid/util/DruidDataSourceUtils.java +++ b/core/src/main/java/com/alibaba/druid/util/DruidDataSourceUtils.java @@ -326,5 +326,6 @@ public static void configFromProperties(DruidDataSource druidDataSource, Propert trySetIntProperty(properties, "druid.socketTimeout", druidDataSource::setSocketTimeout); trySetIntProperty(properties, "druid.transactionQueryTimeout", druidDataSource::setTransactionQueryTimeout); trySetIntProperty(properties, "druid.loginTimeout", druidDataSource::setLoginTimeout); + trySetBooleanProperty(properties, "druid.closeConnOnFatalError", druidDataSource::setCloseConnOnFatalError); } } diff --git a/core/src/test/java/com/alibaba/druid/util/DruidDataSourceUtilsTest.java b/core/src/test/java/com/alibaba/druid/util/DruidDataSourceUtilsTest.java index bb3c499329..179ae7c16d 100644 --- a/core/src/test/java/com/alibaba/druid/util/DruidDataSourceUtilsTest.java +++ b/core/src/test/java/com/alibaba/druid/util/DruidDataSourceUtilsTest.java @@ -396,6 +396,8 @@ public void testConfigFromProperties() { properties.put("druid.disableException", druidDisableException); String druidInstanceKey = "@lizongbo"; properties.put("druid.instanceKey", druidInstanceKey); + String druidCloseConnOnFatalError = "false"; + properties.put("druid.closeConnOnFatalError", druidCloseConnOnFatalError); DruidDataSource dataSource = new DruidDataSource(); dataSource.configFromPropeties(properties); @@ -455,6 +457,7 @@ public void testConfigFromProperties() { assertEquals(druidKillWhenSocketReadTimeout, String.valueOf(dataSource.isKillWhenSocketReadTimeout())); //assertEquals(druidCheckExecuteTime, String.valueOf(dataSource.isCheckExecuteTime())); //assertEquals(druidLoadSpifilterSkip, String.valueOf(dataSource.isLoadSpifilterSkip())); + assertEquals(druidCloseConnOnFatalError, String.valueOf(dataSource.isCloseConnOnFatalError())); } public void testGenTestCode() {