This is an automated email from the ASF dual-hosted git repository.
jianglongtao 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 735e8220772 Fix NumberFormatException when loadng persisted datasource
pool config (#27956)
735e8220772 is described below
commit 735e822077242a92aa8ce7c36b96996bb8a0ff59
Author: Feng Zhang <[email protected]>
AuthorDate: Sun Aug 6 21:07:18 2023 -0700
Fix NumberFormatException when loadng persisted datasource pool config
(#27956)
* Fix NumberFormatException when loadng persisted datasource pool
configuration
* remove line comment
---
.../props/DataSourcePropertiesCreator.java | 47 ++++++++++++++++++----
.../props/DataSourcePropertiesCreatorTest.java | 42 +++++++++++++++++++
2 files changed, 81 insertions(+), 8 deletions(-)
diff --git
a/infra/datasource/core/src/main/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesCreator.java
b/infra/datasource/core/src/main/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesCreator.java
index 3bfcfe1b499..9016ac7199c 100644
---
a/infra/datasource/core/src/main/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesCreator.java
+++
b/infra/datasource/core/src/main/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesCreator.java
@@ -143,16 +143,47 @@ public final class DataSourcePropertiesCreator {
private static PoolConfiguration getPoolConfiguration(final
PoolPropertySynonyms poolPropertySynonyms, final CustomDataSourceProperties
customDataSourceProperties) {
Map<String, Object> standardProperties =
poolPropertySynonyms.getStandardProperties();
- Long connectionTimeoutMilliseconds =
standardProperties.containsKey("connectionTimeoutMilliseconds")
- ?
Long.valueOf(String.valueOf(standardProperties.get("connectionTimeoutMilliseconds")))
- : null;
- Long idleTimeoutMilliseconds =
standardProperties.containsKey("idleTimeoutMilliseconds") ?
Long.valueOf(String.valueOf(standardProperties.get("idleTimeoutMilliseconds")))
: null;
- Long maxLifetimeMilliseconds =
standardProperties.containsKey("maxLifetimeMilliseconds") ?
Long.valueOf(String.valueOf(standardProperties.get("maxLifetimeMilliseconds")))
: null;
- Integer maxPoolSize = standardProperties.containsKey("maxPoolSize") ?
Integer.valueOf(String.valueOf(standardProperties.get("maxPoolSize"))) : null;
- Integer minPoolSize = standardProperties.containsKey("minPoolSize") ?
Integer.valueOf(String.valueOf(standardProperties.get("minPoolSize"))) : null;
- Boolean readOnly = standardProperties.containsKey("readOnly") ?
Boolean.valueOf(String.valueOf(standardProperties.get("readOnly"))) : null;
+ Long connectionTimeoutMilliseconds = toLong(standardProperties,
"connectionTimeoutMilliseconds", null);
+ Long idleTimeoutMilliseconds = toLong(standardProperties,
"idleTimeoutMilliseconds", null);
+ Long maxLifetimeMilliseconds = toLong(standardProperties,
"maxLifetimeMilliseconds", null);
+ Integer maxPoolSize = toInt(standardProperties, "maxPoolSize", null);
+ Integer minPoolSize = toInt(standardProperties, "minPoolSize", null);
+ Boolean readOnly = toBoolean(standardProperties, "readOnly", null);
Properties customProperties = new Properties();
customProperties.putAll(customDataSourceProperties.getProperties());
return new PoolConfiguration(connectionTimeoutMilliseconds,
idleTimeoutMilliseconds, maxLifetimeMilliseconds, maxPoolSize, minPoolSize,
readOnly, customProperties);
}
+
+ private static Long toLong(final Map<String, Object> properties, final
String name, final Long defaultValue) {
+ if (!properties.containsKey(name)) {
+ return defaultValue;
+ }
+ try {
+ return Long.parseLong(String.valueOf(properties.get(name)));
+ } catch (final NumberFormatException ex) {
+ return defaultValue;
+ }
+ }
+
+ private static Integer toInt(final Map<String, Object> properties, final
String name, final Integer defaultValue) {
+ if (!properties.containsKey(name)) {
+ return defaultValue;
+ }
+ try {
+ return Integer.parseInt(String.valueOf(properties.get(name)));
+ } catch (final NumberFormatException ex) {
+ return defaultValue;
+ }
+ }
+
+ private static Boolean toBoolean(final Map<String, Object> properties,
final String name, final Boolean defaultValue) {
+ if (!properties.containsKey(name)) {
+ return defaultValue;
+ }
+ try {
+ return Boolean.parseBoolean(String.valueOf(properties.get(name)));
+ } catch (final NumberFormatException ex) {
+ return defaultValue;
+ }
+ }
}
diff --git
a/infra/datasource/core/src/test/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesCreatorTest.java
b/infra/datasource/core/src/test/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesCreatorTest.java
index 24cd7eade57..26c9a433271 100644
---
a/infra/datasource/core/src/test/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesCreatorTest.java
+++
b/infra/datasource/core/src/test/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesCreatorTest.java
@@ -20,16 +20,24 @@ package org.apache.shardingsphere.infra.datasource.props;
import
org.apache.shardingsphere.infra.datasource.config.ConnectionConfiguration;
import
org.apache.shardingsphere.infra.datasource.config.DataSourceConfiguration;
import org.apache.shardingsphere.infra.datasource.config.PoolConfiguration;
+import
org.apache.shardingsphere.infra.datasource.props.custom.CustomDataSourceProperties;
+import
org.apache.shardingsphere.infra.datasource.props.synonym.ConnectionPropertySynonyms;
+import
org.apache.shardingsphere.infra.datasource.props.synonym.PoolPropertySynonyms;
import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource;
import org.junit.jupiter.api.Test;
import javax.sql.DataSource;
+import java.util.Arrays;
+import java.util.Collections;
import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.Map;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
class DataSourcePropertiesCreatorTest {
@@ -63,6 +71,19 @@ class DataSourcePropertiesCreatorTest {
assertThat(DataSourcePropertiesCreator.create(createDataSource()),
is(new DataSourceProperties(MockedDataSource.class.getName(),
createProperties())));
}
+ @Test
+ void assertCreateConfiguration() {
+ DataSourceProperties dataSourceProperties =
mock(DataSourceProperties.class);
+ ConnectionPropertySynonyms connectionPropertySynonyms = new
ConnectionPropertySynonyms(createStandardProperties(),
createPropertySynonyms());
+ PoolPropertySynonyms poolPropertySynonyms = new
PoolPropertySynonyms(createStandardProperties(), createPropertySynonyms());
+ CustomDataSourceProperties customDataSourceProperties = new
CustomDataSourceProperties(createProperties(),
+ Arrays.asList("username", "password", "closed"),
Collections.singletonList("closed"), Collections.singletonMap("username",
"user"));
+
when(dataSourceProperties.getConnectionPropertySynonyms()).thenReturn(connectionPropertySynonyms);
+
when(dataSourceProperties.getPoolPropertySynonyms()).thenReturn(poolPropertySynonyms);
+
when(dataSourceProperties.getCustomDataSourceProperties()).thenReturn(customDataSourceProperties);
+ DataSourcePropertiesCreator.createConfiguration(dataSourceProperties);
+ }
+
private DataSource createDataSource() {
MockedDataSource result = new MockedDataSource();
result.setDriverClassName(MockedDataSource.class.getName());
@@ -81,4 +102,25 @@ class DataSourcePropertiesCreatorTest {
result.put("maximumPoolSize", "-1");
return result;
}
+
+ private Map<String, Object> createStandardProperties() {
+ Map<String, Object> result = new LinkedHashMap<>(6, 1F);
+ result.put("connectionTimeoutMilliseconds", "null");
+ result.put("idleTimeoutMilliseconds", 180000);
+ result.put("maxLifetimeMilliseconds", 180000);
+ result.put("maxPoolSize", 30);
+ result.put("minPoolSize", 10);
+ result.put("readOnly", false);
+ return result;
+ }
+
+ private Map<String, String> createPropertySynonyms() {
+ Map<String, String> result = new LinkedHashMap<>(5, 1F);
+ result.put("connectionTimeoutMilliseconds", "connectionTimeout");
+ result.put("idleTimeoutMilliseconds", "idleTimeout");
+ result.put("maxLifetimeMilliseconds", "maxLifetime");
+ result.put("maxPoolSize", "maximumPoolSize");
+ result.put("minPoolSize", "minimumIdle");
+ return result;
+ }
}