You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by me...@apache.org on 2021/12/10 10:38:49 UTC
[shardingsphere] branch master updated: Refactor DataSourceKiller (#14038)
This is an automated email from the ASF dual-hosted git repository.
menghaoran pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 824ab08 Refactor DataSourceKiller (#14038)
824ab08 is described below
commit 824ab08750a959eb6636c7b9308ac7b4403aa2f8
Author: Liang Zhang <te...@163.com>
AuthorDate: Fri Dec 10 18:38:04 2021 +0800
Refactor DataSourceKiller (#14038)
---
.../config/datasource/killer/DataSourceKiller.java | 4 ++--
.../datasource/killer/DataSourceKillerFactory.java | 6 +++---
.../killer/impl/DefaultDataSourceKiller.java | 17 ++++++++---------
.../datasource/killer/impl/HikariDataSourceKiller.java | 12 ++++++------
.../metadata/resource/ShardingSphereResource.java | 18 +++++++-----------
5 files changed, 26 insertions(+), 31 deletions(-)
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/killer/DataSourceKiller.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/killer/DataSourceKiller.java
index f4cf4d2..79b5054 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/killer/DataSourceKiller.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/killer/DataSourceKiller.java
@@ -28,10 +28,10 @@ import java.sql.SQLException;
public interface DataSourceKiller extends TypedSPI, RequiredSPI {
/**
- * Gracefully kill the data source.
+ * Kill data source gracefully.
*
* @param dataSource data source
* @throws SQLException SQL exception
*/
- void closeDataSource(DataSource dataSource) throws SQLException;
+ void kill(DataSource dataSource) throws SQLException;
}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/killer/DataSourceKillerFactory.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/killer/DataSourceKillerFactory.java
index 8595645..1b38060 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/killer/DataSourceKillerFactory.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/killer/DataSourceKillerFactory.java
@@ -33,12 +33,12 @@ public final class DataSourceKillerFactory {
}
/**
- * Get data source killer.
+ * Get data source killer instance.
*
* @param dataSourceClassName data source class name
- * @return data source killer
+ * @return instance of data source killer
*/
- public static DataSourceKiller getDataSourceKiller(final String dataSourceClassName) {
+ public static DataSourceKiller getInstance(final String dataSourceClassName) {
return TypedSPIRegistry.findRegisteredService(DataSourceKiller.class, dataSourceClassName, new Properties()).orElse(RequiredSPIRegistry.getRegisteredService(DataSourceKiller.class));
}
}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/killer/impl/DefaultDataSourceKiller.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/killer/impl/DefaultDataSourceKiller.java
index 7d7b670..f789f49 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/killer/impl/DefaultDataSourceKiller.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/killer/impl/DefaultDataSourceKiller.java
@@ -17,11 +17,10 @@
package org.apache.shardingsphere.infra.config.datasource.killer.impl;
-import java.sql.SQLException;
+import org.apache.shardingsphere.infra.config.datasource.killer.DataSourceKiller;
import javax.sql.DataSource;
-
-import org.apache.shardingsphere.infra.config.datasource.killer.DataSourceKiller;
+import java.sql.SQLException;
/**
* Default data source killer.
@@ -29,12 +28,7 @@ import org.apache.shardingsphere.infra.config.datasource.killer.DataSourceKiller
public final class DefaultDataSourceKiller implements DataSourceKiller {
@Override
- public String getType() {
- return "Default";
- }
-
- @Override
- public void closeDataSource(final DataSource dataSource) throws SQLException {
+ public void kill(final DataSource dataSource) throws SQLException {
if (dataSource instanceof AutoCloseable) {
try {
((AutoCloseable) dataSource).close();
@@ -47,6 +41,11 @@ public final class DefaultDataSourceKiller implements DataSourceKiller {
}
@Override
+ public String getType() {
+ return "Default";
+ }
+
+ @Override
public boolean isDefault() {
return true;
}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/killer/impl/HikariDataSourceKiller.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/killer/impl/HikariDataSourceKiller.java
index a492e72..cbe433a 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/killer/impl/HikariDataSourceKiller.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/killer/impl/HikariDataSourceKiller.java
@@ -29,12 +29,7 @@ import java.util.concurrent.Executors;
public final class HikariDataSourceKiller implements DataSourceKiller {
@Override
- public String getType() {
- return HikariDataSource.class.getCanonicalName();
- }
-
- @Override
- public void closeDataSource(final DataSource dataSource) {
+ public void kill(final DataSource dataSource) {
HikariDataSource hikariDataSource = (HikariDataSource) dataSource;
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(() -> {
@@ -51,4 +46,9 @@ public final class HikariDataSourceKiller implements DataSourceKiller {
});
executor.shutdown();
}
+
+ @Override
+ public String getType() {
+ return HikariDataSource.class.getCanonicalName();
+ }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/resource/ShardingSphereResource.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/resource/ShardingSphereResource.java
index 41f78e3..5fed5ad 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/resource/ShardingSphereResource.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/resource/ShardingSphereResource.java
@@ -17,21 +17,18 @@
package org.apache.shardingsphere.infra.metadata.resource;
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.config.datasource.killer.DataSourceKillerFactory;
+import org.apache.shardingsphere.infra.database.type.DatabaseType;
+
+import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
-import javax.sql.DataSource;
-
-import org.apache.shardingsphere.infra.config.datasource.killer.DataSourceKiller;
-import org.apache.shardingsphere.infra.config.datasource.killer.DataSourceKillerFactory;
-import org.apache.shardingsphere.infra.database.type.DatabaseType;
-
-import lombok.Getter;
-import lombok.RequiredArgsConstructor;
-
/**
* ShardingSphere resource.
*/
@@ -85,7 +82,6 @@ public final class ShardingSphereResource {
* @throws SQLException SQL exception
*/
public void close(final DataSource dataSource) throws SQLException {
- DataSourceKiller dataSourceKiller = DataSourceKillerFactory.getDataSourceKiller(dataSource.getClass().getName());
- dataSourceKiller.closeDataSource(dataSource);
+ DataSourceKillerFactory.getInstance(dataSource.getClass().getName()).kill(dataSource);
}
}