Skip to content

Commit

Permalink
Add property closeConnOnFatalError to match HikariCP's behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
linghengqian committed Sep 25, 2024
1 parent c392dbe commit 9b72b46
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
11 changes: 10 additions & 1 deletion core/src/main/java/com/alibaba/druid/pool/DruidDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Filter> autoFilters;
private boolean loadSpifilterSkip;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 9b72b46

Please sign in to comment.