You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ki...@apache.org on 2020/09/04 07:38:12 UTC
[shardingsphere] branch master updated: Refactor for support
sharding jdbc output (#7222)
This is an automated email from the ASF dual-hosted git repository.
kimmking 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 6093ea8 Refactor for support sharding jdbc output (#7222)
6093ea8 is described below
commit 6093ea8c371c035c792112bc8364be6e3955ecf0
Author: avalon5666 <64...@users.noreply.github.com>
AuthorDate: Fri Sep 4 15:36:38 2020 +0800
Refactor for support sharding jdbc output (#7222)
---
.../scaling/core/datasource/DataSourceFactory.java | 8 +-
.../scaling/core/datasource/DataSourceManager.java | 20 +++--
.../scaling/core/datasource/DataSourceWrapper.java | 27 +++++++
.../core/datasource/HikariDataSourceWrapper.java | 85 ++++++++++++++++++++++
.../core/datasource/DataSourceManagerTest.java | 3 +-
5 files changed, 128 insertions(+), 15 deletions(-)
diff --git a/shardingsphere-scaling/shardingsphere-scaling-core/src/main/java/org/apache/shardingsphere/scaling/core/datasource/DataSourceFactory.java b/shardingsphere-scaling/shardingsphere-scaling-core/src/main/java/org/apache/shardingsphere/scaling/core/datasource/DataSourceFactory.java
index 7173265..b2cbecf 100644
--- a/shardingsphere-scaling/shardingsphere-scaling-core/src/main/java/org/apache/shardingsphere/scaling/core/datasource/DataSourceFactory.java
+++ b/shardingsphere-scaling/shardingsphere-scaling-core/src/main/java/org/apache/shardingsphere/scaling/core/datasource/DataSourceFactory.java
@@ -21,8 +21,6 @@ import com.zaxxer.hikari.HikariDataSource;
import org.apache.shardingsphere.scaling.core.config.DataSourceConfiguration;
import org.apache.shardingsphere.scaling.core.config.JDBCDataSourceConfiguration;
-import javax.sql.DataSource;
-
/**
* Data source factory.
*/
@@ -34,18 +32,18 @@ public final class DataSourceFactory {
* @param dataSourceConfiguration data source configuration
* @return new data source
*/
- public DataSource newInstance(final DataSourceConfiguration dataSourceConfiguration) {
+ public DataSourceWrapper newInstance(final DataSourceConfiguration dataSourceConfiguration) {
if (dataSourceConfiguration instanceof JDBCDataSourceConfiguration) {
return newInstanceDataSourceByJDBC((JDBCDataSourceConfiguration) dataSourceConfiguration);
}
throw new UnsupportedOperationException("Unsupported data source configuration");
}
- private DataSource newInstanceDataSourceByJDBC(final JDBCDataSourceConfiguration dataSourceConfiguration) {
+ private DataSourceWrapper newInstanceDataSourceByJDBC(final JDBCDataSourceConfiguration dataSourceConfiguration) {
HikariDataSource result = new HikariDataSource();
result.setJdbcUrl(dataSourceConfiguration.getJdbcUrl());
result.setUsername(dataSourceConfiguration.getUsername());
result.setPassword(dataSourceConfiguration.getPassword());
- return result;
+ return new HikariDataSourceWrapper(result);
}
}
diff --git a/shardingsphere-scaling/shardingsphere-scaling-core/src/main/java/org/apache/shardingsphere/scaling/core/datasource/DataSourceManager.java b/shardingsphere-scaling/shardingsphere-scaling-core/src/main/java/org/apache/shardingsphere/scaling/core/datasource/DataSourceManager.java
index 7673232..f24d568 100644
--- a/shardingsphere-scaling/shardingsphere-scaling-core/src/main/java/org/apache/shardingsphere/scaling/core/datasource/DataSourceManager.java
+++ b/shardingsphere-scaling/shardingsphere-scaling-core/src/main/java/org/apache/shardingsphere/scaling/core/datasource/DataSourceManager.java
@@ -17,13 +17,14 @@
package org.apache.shardingsphere.scaling.core.datasource;
-import com.zaxxer.hikari.HikariDataSource;
import lombok.Getter;
import lombok.NoArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.scaling.core.config.DataSourceConfiguration;
import org.apache.shardingsphere.scaling.core.config.SyncConfiguration;
import javax.sql.DataSource;
+import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -31,16 +32,17 @@ import java.util.concurrent.ConcurrentHashMap;
/**
* Data source manager.
*/
+@Slf4j
@NoArgsConstructor
public final class DataSourceManager implements AutoCloseable {
private final DataSourceFactory dataSourceFactory = new DataSourceFactory();
@Getter
- private final Map<DataSourceConfiguration, HikariDataSource> cachedDataSources = new ConcurrentHashMap<>();
+ private final Map<DataSourceConfiguration, DataSourceWrapper> cachedDataSources = new ConcurrentHashMap<>();
@Getter
- private final Map<DataSourceConfiguration, HikariDataSource> sourceDatasources = new ConcurrentHashMap<>();
+ private final Map<DataSourceConfiguration, DataSourceWrapper> sourceDatasources = new ConcurrentHashMap<>();
public DataSourceManager(final List<SyncConfiguration> syncConfigs) {
createDatasources(syncConfigs);
@@ -54,14 +56,14 @@ public final class DataSourceManager implements AutoCloseable {
private void createSourceDatasources(final List<SyncConfiguration> syncConfigs) {
for (SyncConfiguration syncConfiguration : syncConfigs) {
DataSourceConfiguration dataSourceConfig = syncConfiguration.getDumperConfiguration().getDataSourceConfiguration();
- HikariDataSource hikariDataSource = (HikariDataSource) dataSourceFactory.newInstance(dataSourceConfig);
+ DataSourceWrapper hikariDataSource = dataSourceFactory.newInstance(dataSourceConfig);
cachedDataSources.put(dataSourceConfig, hikariDataSource);
sourceDatasources.put(dataSourceConfig, hikariDataSource);
}
}
private void createTargetDatasources(final DataSourceConfiguration dataSourceConfig) {
- cachedDataSources.put(dataSourceConfig, (HikariDataSource) dataSourceFactory.newInstance(dataSourceConfig));
+ cachedDataSources.put(dataSourceConfig, dataSourceFactory.newInstance(dataSourceConfig));
}
/**
@@ -78,7 +80,7 @@ public final class DataSourceManager implements AutoCloseable {
if (cachedDataSources.containsKey(dataSourceConfig)) {
return cachedDataSources.get(dataSourceConfig);
}
- HikariDataSource result = (HikariDataSource) dataSourceFactory.newInstance(dataSourceConfig);
+ DataSourceWrapper result = dataSourceFactory.newInstance(dataSourceConfig);
cachedDataSources.put(dataSourceConfig, result);
return result;
}
@@ -89,9 +91,11 @@ public final class DataSourceManager implements AutoCloseable {
*/
@Override
public void close() {
- for (HikariDataSource each : cachedDataSources.values()) {
- if (!each.isClosed()) {
+ for (DataSourceWrapper each : cachedDataSources.values()) {
+ try {
each.close();
+ } catch (IOException e) {
+ log.warn("An exception occurred while closing the data source", e);
}
}
cachedDataSources.clear();
diff --git a/shardingsphere-scaling/shardingsphere-scaling-core/src/main/java/org/apache/shardingsphere/scaling/core/datasource/DataSourceWrapper.java b/shardingsphere-scaling/shardingsphere-scaling-core/src/main/java/org/apache/shardingsphere/scaling/core/datasource/DataSourceWrapper.java
new file mode 100644
index 0000000..7e5231f
--- /dev/null
+++ b/shardingsphere-scaling/shardingsphere-scaling-core/src/main/java/org/apache/shardingsphere/scaling/core/datasource/DataSourceWrapper.java
@@ -0,0 +1,27 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.scaling.core.datasource;
+
+import javax.sql.DataSource;
+import java.io.Closeable;
+
+/**
+ * Data source wrapper is for abstract standard jdbc and sharding jdbc.
+ */
+public interface DataSourceWrapper extends DataSource, Closeable {
+}
diff --git a/shardingsphere-scaling/shardingsphere-scaling-core/src/main/java/org/apache/shardingsphere/scaling/core/datasource/HikariDataSourceWrapper.java b/shardingsphere-scaling/shardingsphere-scaling-core/src/main/java/org/apache/shardingsphere/scaling/core/datasource/HikariDataSourceWrapper.java
new file mode 100644
index 0000000..5806175
--- /dev/null
+++ b/shardingsphere-scaling/shardingsphere-scaling-core/src/main/java/org/apache/shardingsphere/scaling/core/datasource/HikariDataSourceWrapper.java
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.scaling.core.datasource;
+
+import com.zaxxer.hikari.HikariDataSource;
+import lombok.AllArgsConstructor;
+
+import java.io.PrintWriter;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.SQLFeatureNotSupportedException;
+import java.util.logging.Logger;
+
+@AllArgsConstructor
+public final class HikariDataSourceWrapper implements DataSourceWrapper {
+
+ private final HikariDataSource dataSource;
+
+ @Override
+ public void close() {
+ if (!dataSource.isClosed()) {
+ dataSource.close();
+ }
+ }
+
+ @Override
+ public Connection getConnection() throws SQLException {
+ return dataSource.getConnection();
+ }
+
+ @Override
+ public Connection getConnection(final String username, final String password) throws SQLException {
+ return dataSource.getConnection(username, password);
+ }
+
+ @Override
+ public <T> T unwrap(final Class<T> iface) throws SQLException {
+ return dataSource.unwrap(iface);
+ }
+
+ @Override
+ public boolean isWrapperFor(final Class<?> iface) throws SQLException {
+ return dataSource.isWrapperFor(iface);
+ }
+
+ @Override
+ public PrintWriter getLogWriter() throws SQLException {
+ return dataSource.getLogWriter();
+ }
+
+ @Override
+ public void setLogWriter(final PrintWriter out) throws SQLException {
+ dataSource.setLogWriter(out);
+ }
+
+ @Override
+ public void setLoginTimeout(final int seconds) throws SQLException {
+ dataSource.setLoginTimeout(seconds);
+ }
+
+ @Override
+ public int getLoginTimeout() throws SQLException {
+ return dataSource.getLoginTimeout();
+ }
+
+ @Override
+ public Logger getParentLogger() throws SQLFeatureNotSupportedException {
+ return dataSource.getParentLogger();
+ }
+}
diff --git a/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/datasource/DataSourceManagerTest.java b/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/datasource/DataSourceManagerTest.java
index 3c37c58..78a403e 100644
--- a/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/datasource/DataSourceManagerTest.java
+++ b/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/datasource/DataSourceManagerTest.java
@@ -18,7 +18,6 @@
package org.apache.shardingsphere.scaling.core.datasource;
import com.google.gson.Gson;
-import com.zaxxer.hikari.HikariDataSource;
import org.apache.shardingsphere.scaling.core.config.ScalingConfiguration;
import org.apache.shardingsphere.scaling.core.config.SyncConfiguration;
import org.apache.shardingsphere.scaling.core.utils.SyncConfigurationUtil;
@@ -60,7 +59,7 @@ public final class DataSourceManagerTest {
public void assertGetDataSource() {
DataSourceManager dataSourceManager = new DataSourceManager();
DataSource actual = dataSourceManager.getDataSource(syncConfigurations.get(0).getDumperConfiguration().getDataSourceConfiguration());
- assertThat(actual, instanceOf(HikariDataSource.class));
+ assertThat(actual, instanceOf(HikariDataSourceWrapper.class));
}
@Test