You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2021/10/01 13:37:16 UTC
[shardingsphere] branch master updated: Add
AbstractDataSourceAdapter (#12876)
This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang 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 fcf8b2e Add AbstractDataSourceAdapter (#12876)
fcf8b2e is described below
commit fcf8b2ef7de65753a79ae40f348487605f3347e8
Author: Liang Zhang <te...@163.com>
AuthorDate: Fri Oct 1 21:36:36 2021 +0800
Add AbstractDataSourceAdapter (#12876)
* Add AbstractDataSourceAdapter
* Fix test case
---
.../AbstractDataSourceAdapter.java} | 26 ++------
.../core/datasource/ShardingSphereDataSource.java | 28 ++++++--
.../datasource/CircuitBreakerDataSource.java | 19 ++----
.../UnsupportedOperationDataSourceTest.java | 76 ----------------------
.../datasource/CircuitBreakerDataSourceTest.java | 18 ++---
5 files changed, 40 insertions(+), 127 deletions(-)
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/unsupported/AbstractUnsupportedOperationDataSource.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/adapter/AbstractDataSourceAdapter.java
similarity index 56%
rename from shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/unsupported/AbstractUnsupportedOperationDataSource.java
rename to shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/adapter/AbstractDataSourceAdapter.java
index 9d7e5d2..be1072c 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/unsupported/AbstractUnsupportedOperationDataSource.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/adapter/AbstractDataSourceAdapter.java
@@ -15,40 +15,26 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.driver.jdbc.unsupported;
+package org.apache.shardingsphere.driver.jdbc.adapter;
import lombok.Getter;
import lombok.Setter;
-import org.apache.shardingsphere.driver.jdbc.adapter.WrapperAdapter;
import javax.sql.DataSource;
import java.io.PrintWriter;
-import java.sql.SQLException;
-import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;
/**
- * Unsupported {@code Datasource} methods.
+ * Adapter for {@code Datasource}.
*/
-public abstract class AbstractUnsupportedOperationDataSource extends WrapperAdapter implements DataSource {
+@Getter
+@Setter
+public abstract class AbstractDataSourceAdapter extends WrapperAdapter implements DataSource {
- @SuppressWarnings("UseOfSystemOutOrSystemErr")
- @Setter
- @Getter
private PrintWriter logWriter = new PrintWriter(System.out);
@Override
- public final int getLoginTimeout() throws SQLException {
- throw new SQLFeatureNotSupportedException("unsupported getLoginTimeout()");
- }
-
- @Override
- public final void setLoginTimeout(final int seconds) throws SQLException {
- throw new SQLFeatureNotSupportedException("unsupported setLoginTimeout(int seconds)");
- }
-
- @Override
- public Logger getParentLogger() {
+ public final Logger getParentLogger() {
return Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
}
}
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java
index 9f6d14d..51d22e4 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java
@@ -18,7 +18,7 @@
package org.apache.shardingsphere.driver.jdbc.core.datasource;
import lombok.Getter;
-import org.apache.shardingsphere.driver.jdbc.unsupported.AbstractUnsupportedOperationDataSource;
+import org.apache.shardingsphere.driver.jdbc.adapter.AbstractDataSourceAdapter;
import org.apache.shardingsphere.driver.state.DriverStateContext;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.infra.config.checker.RuleConfigurationCheckerFactory;
@@ -44,7 +44,7 @@ import java.util.stream.Collectors;
* ShardingSphere data source.
*/
@Getter
-public final class ShardingSphereDataSource extends AbstractUnsupportedOperationDataSource implements AutoCloseable {
+public final class ShardingSphereDataSource extends AbstractDataSourceAdapter implements AutoCloseable {
private final String schemaName;
@@ -86,10 +86,6 @@ public final class ShardingSphereDataSource extends AbstractUnsupportedOperation
return getConnection();
}
- private Map<String, DataSource> getDataSourceMap() {
- return contextManager.getMetaDataContexts().getMetaData(schemaName).getResource().getDataSources();
- }
-
/**
* Close data sources.
*
@@ -97,8 +93,9 @@ public final class ShardingSphereDataSource extends AbstractUnsupportedOperation
* @throws Exception exception
*/
public void close(final Collection<String> dataSourceNames) throws Exception {
+ Map<String, DataSource> dataSourceMap = getDataSourceMap();
for (String each : dataSourceNames) {
- close(getDataSourceMap().get(each));
+ close(dataSourceMap.get(each));
}
contextManager.close();
}
@@ -113,4 +110,21 @@ public final class ShardingSphereDataSource extends AbstractUnsupportedOperation
public void close() throws Exception {
close(getDataSourceMap().keySet());
}
+
+ @Override
+ public int getLoginTimeout() throws SQLException {
+ Map<String, DataSource> dataSourceMap = getDataSourceMap();
+ return dataSourceMap.isEmpty() ? 0 : dataSourceMap.values().iterator().next().getLoginTimeout();
+ }
+
+ @Override
+ public void setLoginTimeout(final int seconds) throws SQLException {
+ for (DataSource each : getDataSourceMap().values()) {
+ each.setLoginTimeout(seconds);
+ }
+ }
+
+ private Map<String, DataSource> getDataSourceMap() {
+ return contextManager.getMetaDataContexts().getMetaData(schemaName).getResource().getDataSources();
+ }
}
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/circuit/datasource/CircuitBreakerDataSource.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/circuit/datasource/CircuitBreakerDataSource.java
index 6ade171..a673cf0 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/circuit/datasource/CircuitBreakerDataSource.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/circuit/datasource/CircuitBreakerDataSource.java
@@ -17,21 +17,15 @@
package org.apache.shardingsphere.driver.state.circuit.datasource;
+import org.apache.shardingsphere.driver.jdbc.adapter.AbstractDataSourceAdapter;
import org.apache.shardingsphere.driver.state.circuit.connection.CircuitBreakerConnection;
-import org.apache.shardingsphere.driver.jdbc.unsupported.AbstractUnsupportedOperationDataSource;
-import java.io.PrintWriter;
import java.sql.Connection;
-import java.util.logging.Logger;
/**
* Circuit breaker datasource.
*/
-public final class CircuitBreakerDataSource extends AbstractUnsupportedOperationDataSource implements AutoCloseable {
-
- @Override
- public void close() {
- }
+public final class CircuitBreakerDataSource extends AbstractDataSourceAdapter implements AutoCloseable {
@Override
public Connection getConnection() {
@@ -44,16 +38,15 @@ public final class CircuitBreakerDataSource extends AbstractUnsupportedOperation
}
@Override
- public PrintWriter getLogWriter() {
- return null;
+ public int getLoginTimeout() {
+ return 0;
}
@Override
- public void setLogWriter(final PrintWriter out) {
+ public void setLoginTimeout(final int seconds) {
}
@Override
- public Logger getParentLogger() {
- return null;
+ public void close() {
}
}
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationDataSourceTest.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationDataSourceTest.java
deleted file mode 100644
index c6d3388..0000000
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationDataSourceTest.java
+++ /dev/null
@@ -1,76 +0,0 @@
-/*
- * 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.driver.jdbc.unsupported;
-
-import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
-import org.apache.shardingsphere.infra.config.RuleConfiguration;
-import org.apache.shardingsphere.infra.database.DefaultSchema;
-import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
-import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
-import org.junit.Before;
-import org.junit.Test;
-
-import javax.sql.DataSource;
-import java.sql.Connection;
-import java.sql.DatabaseMetaData;
-import java.sql.SQLException;
-import java.sql.SQLFeatureNotSupportedException;
-import java.util.Collections;
-import java.util.List;
-import java.util.Map;
-import java.util.Properties;
-
-import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-public final class UnsupportedOperationDataSourceTest {
-
- private ShardingSphereDataSource shardingSphereDataSource;
-
- @Before
- public void setUp() throws SQLException {
- shardingSphereDataSource = new ShardingSphereDataSource(DefaultSchema.LOGIC_NAME, null, getDataSource(), getRuleConfigurations(), new Properties());
- }
-
- private Map<String, DataSource> getDataSource() throws SQLException {
- DataSource dataSource = mock(DataSource.class);
- Connection connection = mock(Connection.class, RETURNS_DEEP_STUBS);
- when(dataSource.getConnection()).thenReturn(connection);
- when(connection.getMetaData()).thenReturn(mock(DatabaseMetaData.class, RETURNS_DEEP_STUBS));
- when(dataSource.getConnection().getMetaData().getURL()).thenReturn("jdbc:mysql://localhost:3306/test");
- return Collections.singletonMap("ds", dataSource);
- }
-
- private List<RuleConfiguration> getRuleConfigurations() {
- ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
- ShardingTableRuleConfiguration shardingTableRuleConfig = new ShardingTableRuleConfiguration("table", "ds" + "." + "table");
- shardingRuleConfig.setTables(Collections.singletonList(shardingTableRuleConfig));
- return Collections.singletonList(shardingRuleConfig);
- }
-
- @Test(expected = SQLFeatureNotSupportedException.class)
- public void assertGetLoginTimeout() throws SQLException {
- shardingSphereDataSource.getLoginTimeout();
- }
-
- @Test(expected = SQLFeatureNotSupportedException.class)
- public void assertSetLoginTimeout() throws SQLException {
- shardingSphereDataSource.setLoginTimeout(0);
- }
-}
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/state/circuit/datasource/CircuitBreakerDataSourceTest.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/state/circuit/datasource/CircuitBreakerDataSourceTest.java
index c083081..98127cb 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/state/circuit/datasource/CircuitBreakerDataSourceTest.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/state/circuit/datasource/CircuitBreakerDataSourceTest.java
@@ -20,7 +20,8 @@ package org.apache.shardingsphere.driver.state.circuit.datasource;
import org.apache.shardingsphere.driver.state.circuit.connection.CircuitBreakerConnection;
import org.junit.Test;
-import static org.junit.Assert.assertNull;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
public final class CircuitBreakerDataSourceTest {
@@ -39,18 +40,13 @@ public final class CircuitBreakerDataSourceTest {
}
@Test
- public void assertGetLogWriter() {
- assertNull(dataSource.getLogWriter());
+ public void assertGetLoginTimeout() {
+ assertThat(dataSource.getLoginTimeout(), is(0));
}
@Test
- public void assertSetLogWriter() {
- dataSource.setLogWriter(null);
- assertNull(dataSource.getLogWriter());
- }
-
- @Test
- public void assertGetParentLogger() {
- assertNull(dataSource.getParentLogger());
+ public void assertSetLoginTimeout() {
+ dataSource.setLoginTimeout(10);
+ assertThat(dataSource.getLoginTimeout(), is(0));
}
}