未验证 提交 c414cb81 编写于 作者: Z zhyee 提交者: GitHub

Fix orchestration create datasource error when datasource map is empty (#5605)

* fix orchestration create datasource error when datasource map is empty

* fix
上级 fe364eaf
......@@ -100,7 +100,7 @@ public final class YamlOrchestrationShardingSphereDataSourceFactory {
private static DataSource createDataSource(final Map<String, DataSource> dataSourceMap, final YamlOrchestrationRootRuleConfigurations configurations,
final Properties props, final Map<String, YamlCenterRepositoryConfiguration> yamlInstanceConfigurationMap) throws SQLException {
if (null == configurations) {
if (configurations.getRules().isEmpty() || dataSourceMap.isEmpty()) {
return new OrchestrationShardingSphereDataSource(new OrchestrationConfiguration(YamlCenterRepositoryConfigurationSwapperUtil.marshal(yamlInstanceConfigurationMap)));
} else {
ShardingSphereDataSource shardingSphereDataSource = new ShardingSphereDataSource(dataSourceMap, SWAPPER.swap(configurations), props);
......
/*
* 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.shardingjdbc.orchestration.api.yaml;
import org.apache.shardingsphere.shardingjdbc.orchestration.internal.datasource.OrchestrationShardingSphereDataSource;
import org.apache.shardingsphere.underlying.common.exception.ShardingSphereException;
import org.junit.AfterClass;
import org.junit.Test;
import javax.sql.DataSource;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
import java.util.List;
public class YamlOrchestrationShardingSphereDataSourceFactoryTest extends AbstractYamlDataSourceTest {
private static final List<String> CONFIG_FILES = Arrays.asList("/yaml/integrate/sharding_ms/configWithDataSourceWithProps.yaml",
"/yaml/integrate/sharding_ms/configWithoutDataSourceWithProps.yaml", "/yaml/integrate/sharding_ms/configWithDataSourceWithoutRules.yaml");
private static DataSource dataSource;
@Test
public void assertCreateDataSource() {
CONFIG_FILES.forEach(each -> {
try {
File yamlFile = new File(YamlOrchestrationShardingSphereDataSourceFactoryTest.class.getResource(each).toURI());
exeuteSQL(yamlFile);
} catch (URISyntaxException | SQLException | IOException e) {
throw new ShardingSphereException(e);
}
});
}
private void exeuteSQL(final File yamlFile) throws SQLException, IOException {
dataSource = YamlOrchestrationShardingSphereDataSourceFactory.createDataSource(yamlFile);
try (Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement()) {
statement.execute(String.format("INSERT INTO t_order(user_id,status) values(%d, %s)", 10, "'insert'"));
statement.executeQuery("SELECT * FROM t_order");
statement.executeQuery("SELECT * FROM t_order_item");
statement.executeQuery("SELECT * FROM config");
}
}
@AfterClass
public static void close() throws Exception {
((OrchestrationShardingSphereDataSource) dataSource).close();
}
}
#
# 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.
#
dataSources:
db0_master: !!org.apache.commons.dbcp2.BasicDataSource
driverClassName: org.h2.Driver
url: jdbc:h2:mem:db0_master;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
username: sa
password:
maxTotal: 100
db1_master: !!org.apache.commons.dbcp2.BasicDataSource
driverClassName: org.h2.Driver
url: jdbc:h2:mem:db1_master;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
username: sa
password:
maxTotal: 100
db0_slave: !!org.apache.commons.dbcp2.BasicDataSource
driverClassName: org.h2.Driver
url: jdbc:h2:mem:db0_slave;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
username: sa
password:
maxTotal: 100
db1_slave: !!org.apache.commons.dbcp2.BasicDataSource
driverClassName: org.h2.Driver
url: jdbc:h2:mem:db1_slave;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
username: sa
password:
maxTotal: 100
orchestration:
test_sharding_ms_registry_name:
orchestrationType: registry_center
instanceType: FirstTestRegistryCenter
serverLists: localhost:3181
namespace: test_sharding_ms_registry
props:
overwrite: true
retryIntervalMilliseconds: 10
timeToLiveSeconds: 10
maxRetries: 10
operationTimeoutMilliseconds: 10
test_sharding_ms_config_name:
orchestrationType: config_center
instanceType: FirstTestConfigCenter
serverLists: localhost:3181
namespace: test_sharding_ms_config
props:
overwrite: true
retryIntervalMilliseconds: 10
timeToLiveSeconds: 10
maxRetries: 10
operationTimeoutMilliseconds: 10
test_sharding_ms_metadata_name:
orchestrationType: metadata_center
instanceType: FirstTestMetaDataCenter
serverLists: localhost:3181
namespace: test_sharding_ms_metadata
props:
overwrite: true
retryIntervalMilliseconds: 10
timeToLiveSeconds: 10
maxRetries: 10
operationTimeoutMilliseconds: 10
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册