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/12/29 01:01:10 UTC

[shardingsphere] branch master updated: Fix issue 14175 resultSet.getBoolean() throw ClassCastException (#14337)

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 5920c07  Fix issue 14175 resultSet.getBoolean() throw ClassCastException (#14337)
5920c07 is described below

commit 5920c07c845004ad77d16fb7dcb50312182a39db
Author: Bowen <ch...@gmail.com>
AuthorDate: Wed Dec 29 09:00:25 2021 +0800

    Fix issue 14175 resultSet.getBoolean() throw ClassCastException (#14337)
    
    * fix issue 14175 resultSet.getBoolean() throw ClassCastException
    
    * Fixed Discovery heartbeat job not work. (#14339)
    
    * fix issue 14175 resultSet.getBoolean() throw ClassCastException
    
    * Revert "Fixed Discovery heartbeat job not work. (#14339)"
    
    This reverts commit f2151421374e3e5a856f82a7283de4d3bb0ad154.
    
    Co-authored-by: zhaojinchao <33...@users.noreply.github.com>
---
 .../dbdiscovery/rule/DatabaseDiscoveryRule.java    |  2 +-
 .../driver/jdbc/core/resultset/ResultSetUtil.java  | 23 +++++++++++++++++++++-
 .../jdbc/core/resultset/ResultSetUtilTest.java     |  8 ++++++++
 .../cluster/ClusterContextManagerBuilder.java      |  2 +-
 4 files changed, 32 insertions(+), 3 deletions(-)

diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryRule.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryRule.java
index 2dd3736..203ea2e 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryRule.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryRule.java
@@ -190,7 +190,7 @@ public final class DatabaseDiscoveryRule implements SchemaRule, DataSourceContai
         Optional<ModeScheduleContext> modeScheduleContext = ModeScheduleContextFactory.getInstance().get();
         if (modeScheduleContext.isPresent()) {
             for (Entry<String, DatabaseDiscoveryDataSourceRule> entry : dataSourceRules.entrySet()) {
-                Map<String, DataSource> dataSources = dataSourceMap.entrySet().stream().filter(dataSource -> !entry.getValue().getDisabledDataSourceNames().contains(dataSource.getKey()))
+                Map<String, DataSource> dataSources = dataSourceMap.entrySet().stream().filter(dataSource -> entry.getValue().getDisabledDataSourceNames().contains(dataSource.getKey()))
                         .collect(Collectors.toMap(Entry::getKey, Entry::getValue));
                 CronJob job = new CronJob(entry.getValue().getDatabaseDiscoveryType().getType() + "-" + entry.getValue().getName(),
                     each -> new HeartbeatJob(schemaName, dataSources, entry.getValue().getName(), entry.getValue().getDatabaseDiscoveryType(), entry.getValue().getDisabledDataSourceNames())
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/resultset/ResultSetUtil.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/resultset/ResultSetUtil.java
index 2b41220..fe637e3 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/resultset/ResultSetUtil.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/resultset/ResultSetUtil.java
@@ -73,6 +73,9 @@ public final class ResultSetUtil {
         if (value instanceof byte[]) {
             return convertByteArrayValue(value, convertType);
         }
+        if (boolean.class.equals(convertType)) {
+            return convertBooleanValue(value);
+        }
         if (String.class.equals(convertType)) {
             return value.toString();
         } else {
@@ -169,7 +172,7 @@ public final class ResultSetUtil {
         Number number = (Number) value;
         switch (convertType.getName()) {
             case "boolean":
-                return 0 != number.longValue();
+                return longToBoolean(number.longValue());
             case "byte":
                 return number.byteValue();
             case "short":
@@ -224,4 +227,22 @@ public final class ResultSetUtil {
                 return value;
         }
     }
+
+    private static Object convertBooleanValue(final Object value) {
+        if (value instanceof Boolean) {
+            return value;
+        }
+        String stringVal = value.toString();
+        if (stringVal.length() > 0) {
+            int firstChar = Character.toLowerCase(stringVal.charAt(0));
+            return 't' == firstChar || 'y' == firstChar || '1' == firstChar || "-1".equals(stringVal);
+        } else {
+            return false;
+        }
+    }
+
+    private static Boolean longToBoolean(final long longVal) {
+        return -1 == longVal || longVal > 0;
+    }
+    
 }
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/resultset/ResultSetUtilTest.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/resultset/ResultSetUtilTest.java
index fc9254e..44ef674 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/resultset/ResultSetUtilTest.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/resultset/ResultSetUtilTest.java
@@ -64,6 +64,14 @@ public final class ResultSetUtilTest {
         assertThat(ResultSetUtil.convertValue(timestamp, LocalDate.class), is(LocalDate.of(2021, Month.DECEMBER, 23)));
         assertThat(ResultSetUtil.convertValue(timestamp, LocalTime.class), is(LocalTime.of(19, 30)));
     }
+
+    @Test
+    public void assertConvertBooleanValue() {
+        String dbFalse = "-2";
+        String dbTrue = "1";
+        assertThat(ResultSetUtil.convertValue(dbFalse, boolean.class), is(false));
+        assertThat(ResultSetUtil.convertValue(dbTrue, boolean.class), is(true));
+    }
     
     @Test
     public void assertConvertNumberValueSuccess() {
diff --git a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
index 34fbb5e..01e42939 100644
--- a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
+++ b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
@@ -83,6 +83,7 @@ public final class ClusterContextManagerBuilder implements ContextManagerBuilder
         beforeBuildContextManager(modeConfig, dataSourcesMap, schemaRuleConfigs, globalRuleConfigs, props, isOverwrite, port, schemaName);
         contextManager = new ContextManager();
         contextManager.init(metaDataContexts, transactionContexts, null);
+        ModeScheduleContextFactory.getInstance().init(modeConfig);
         afterBuildContextManager();
         return contextManager;
     }
@@ -92,7 +93,6 @@ public final class ClusterContextManagerBuilder implements ContextManagerBuilder
                                            final Properties props, final boolean isOverwrite, final Integer port, final String schemaName) throws SQLException {
         ClusterPersistRepository repository = createClusterPersistRepository((ClusterPersistRepositoryConfiguration) modeConfig.getRepository());
         registryCenter = new RegistryCenter(repository, port);
-        ModeScheduleContextFactory.getInstance().init(modeConfig);
         metaDataPersistService = new MetaDataPersistService(repository);
         persistConfigurations(metaDataPersistService, dataSourcesMap, schemaRuleConfigs, globalRuleConfigs, props, isOverwrite);
         Collection<String> schemaNames = Strings.isNullOrEmpty(schemaName) ? metaDataPersistService.getSchemaMetaDataService().loadAllNames() : Arrays.asList(schemaName);