Skip to content

Commit

Permalink
Merge pull request #2177 from cherrylzhao/dev-bug-fix
Browse files Browse the repository at this point in the history
fix broadcast table configuration error for spring-boot
  • Loading branch information
terrymanu authored Apr 10, 2019
2 parents 0f6b25e + 3013a7f commit d7a7489
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ public DataSource dataSource() throws SQLException {
if (null != masterSlaveProperties.getMasterDataSourceName()) {
return MasterSlaveDataSourceFactory.createDataSource(dataSourceMap, masterSlaveSwapper.swap(masterSlaveProperties), propMapProperties.getProps());
}
if (!shardingProperties.getTables().isEmpty()) {
return ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingSwapper.swap(shardingProperties), propMapProperties.getProps());
if (!encryptProperties.getTables().isEmpty()) {
return EncryptDataSourceFactory.createDataSource(dataSourceMap.values().iterator().next(), encryptSwapper.swap(encryptProperties));
}
return EncryptDataSourceFactory.createDataSource(dataSourceMap.values().iterator().next(), encryptSwapper.swap(encryptProperties));
return ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingSwapper.swap(shardingProperties), propMapProperties.getProps());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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.spring.boot.type;

import org.apache.shardingsphere.core.rule.ShardingRule;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.ShardingDataSource;
import org.hamcrest.CoreMatchers;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.Collection;
import java.util.Collections;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SpringBootBroadcastTableTest.class)
@SpringBootApplication
@ActiveProfiles("broadcast-table")
public class SpringBootBroadcastTableTest {

@Resource
private DataSource dataSource;

@Test
public void assertBroadcastTable() {
assertThat(dataSource, instanceOf(ShardingDataSource.class));
ShardingDataSource shardingDataSource = (ShardingDataSource) dataSource;
ShardingRule shardingRule = shardingDataSource.getShardingContext().getShardingRule();
assertThat(shardingRule.getBroadcastTables(), CoreMatchers.<Collection<String>>is(Collections.singletonList("t_config")));
assertThat(shardingRule.getShardingDataSourceNames().getDataSourceNames().size(), is(3));
assertThat(shardingRule.getShardingRuleConfig().getDefaultDataSourceName(), is("ds"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#
# 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.
#

sharding.jdbc.datasource.names=ds,ds_${0..1}
sharding.jdbc.datasource.ds.type=org.apache.commons.dbcp2.BasicDataSource
sharding.jdbc.datasource.ds.driver-class-name=org.h2.Driver
sharding.jdbc.datasource.ds.url=jdbc:h2:mem:ds;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
sharding.jdbc.datasource.ds.username=sa
sharding.jdbc.datasource.ds.password=
sharding.jdbc.datasource.ds.max-total=100

sharding.jdbc.datasource.ds_0.type=org.apache.commons.dbcp2.BasicDataSource
sharding.jdbc.datasource.ds_0.driver-class-name=org.h2.Driver
sharding.jdbc.datasource.ds_0.url=jdbc:h2:mem:ds_0;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
sharding.jdbc.datasource.ds_0.username=sa
sharding.jdbc.datasource.ds_0.password=
sharding.jdbc.datasource.ds_0.max-total=100

sharding.jdbc.datasource.ds_1.type=org.apache.commons.dbcp2.BasicDataSource
sharding.jdbc.datasource.ds_1.driver-class-name=org.h2.Driver
sharding.jdbc.datasource.ds_1.url=jdbc:h2:mem:ds_1;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
sharding.jdbc.datasource.ds_1.username=sa
sharding.jdbc.datasource.ds_1.password=
sharding.jdbc.datasource.ds_1.max-total=100

sharding.jdbc.config.sharding.default-data-source-name=ds
sharding.jdbc.config.sharding.broadcast-tables=t_config

sharding.jdbc.config.props.sql.show=true
sharding.jdbc.config.props.executor.size=100
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="master-slave-rules" minOccurs="0" />
<xsd:element ref="table-rules" />
<xsd:element ref="table-rules" minOccurs="0" />
<xsd:element ref="binding-table-rules" minOccurs="0" />
<xsd:element ref="broadcast-table-rules" minOccurs="0" />
</xsd:sequence>
Expand Down

0 comments on commit d7a7489

Please sign in to comment.