Skip to content

Commit

Permalink
fix #60
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed May 6, 2016
1 parent dfb3c5c commit c66f845
Show file tree
Hide file tree
Showing 124 changed files with 535 additions and 270 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@
>详细的规则配置请参考[用户指南](http://dangdangdotcom.github.io/sharding-jdbc/post/user_guide/)
## 使用原生JDBC接口
通过规则配置对象获取`ShardingDataSource``ShardingDataSource`实现自`JDBC`的标准接口`DataSource`。然后可通过`DataSource`选择使用原生`JDBC`开发,或者使用`JPA`, `MyBatis``ORM`工具。
通过`ShardingDataSourceFactory`工厂和规则配置对象获取`ShardingDataSource``ShardingDataSource`实现自`JDBC`的标准接口`DataSource`。然后可通过`DataSource`选择使用原生`JDBC`开发,或者使用`JPA`, `MyBatis``ORM`工具。
`JDBC`原生实现为例:
```java
DataSource dataSource = new ShardingDataSource(shardingRule);
DataSource dataSource = ShardingDataSourceFactory.createDataSource(shardingRule);
String sql = "SELECT i.* FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id WHERE o.user_id=? AND o.order_id=?";
try (
Connection conn = dataSource.getConnection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

package com.dangdang.ddframe.rdb.sharding.spring.datasource;

import com.dangdang.ddframe.rdb.sharding.api.ShardingDataSource;
import com.dangdang.ddframe.rdb.sharding.config.common.api.ShardingRuleBuilder;
import com.dangdang.ddframe.rdb.sharding.config.common.api.config.ShardingRuleConfig;
import com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource;

import java.util.Properties;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@
import javax.annotation.Resource;
import javax.sql.DataSource;

import com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource;
import org.apache.commons.dbcp.BasicDataSource;
import org.h2.tools.RunScript;
import org.junit.Before;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
import com.dangdang.ddframe.rdb.sharding.api.ShardingDataSource;

import lombok.Getter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
</constructor-arg>
</bean>

<bean id="shardingDataSource" class="com.dangdang.ddframe.rdb.sharding.api.ShardingDataSource">
<bean id="shardingDataSource" class="com.dangdang.ddframe.rdb.sharding.api.ShardingDataSourceFactory" factory-method="createDataSource">
<constructor-arg ref="shardingRule"/>
<constructor-arg>
<props>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@
<constructor-arg index="2" ref="databaseShardingStrategy"/>
<constructor-arg index="3" ref="tableShardingStrategy"/>
</bean>

<bean id="shardingDataSource" class="com.dangdang.ddframe.rdb.sharding.api.ShardingDataSource">
<bean id="shardingDataSource" class="com.dangdang.ddframe.rdb.sharding.api.ShardingDataSourceFactory" factory-method="createDataSource">
<constructor-arg ref="shardingRule"/>
<constructor-arg>
<props>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

package com.dangdang.ddframe.rdb.sharding.config.yaml.api;

import com.dangdang.ddframe.rdb.sharding.api.ShardingDataSource;
import com.dangdang.ddframe.rdb.sharding.config.common.api.ShardingRuleBuilder;
import com.dangdang.ddframe.rdb.sharding.config.yaml.internel.YamlConfig;
import com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,75 +17,27 @@

package com.dangdang.ddframe.rdb.sharding.api;

import com.dangdang.ddframe.rdb.sharding.api.props.ShardingProperties;
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
import com.dangdang.ddframe.rdb.sharding.constants.DatabaseType;
import com.dangdang.ddframe.rdb.sharding.exception.ShardingJdbcException;
import com.dangdang.ddframe.rdb.sharding.executor.ExecutorEngine;
import com.dangdang.ddframe.rdb.sharding.jdbc.ShardingConnection;
import com.dangdang.ddframe.rdb.sharding.jdbc.ShardingContext;
import com.dangdang.ddframe.rdb.sharding.jdbc.adapter.AbstractDataSourceAdapter;
import com.dangdang.ddframe.rdb.sharding.metrics.MetricsContext;
import com.dangdang.ddframe.rdb.sharding.router.SQLRouteEngine;
import com.google.common.base.Preconditions;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Properties;

/**
* 支持分片的数据源.
*
* <p>
* 已废弃, 请使用ShardingDataSourceFactory创建数据源. 未来版本中将删除此类.
* </p>
*
* @author zhangliang
*/
public class ShardingDataSource extends AbstractDataSourceAdapter {

private final ShardingProperties shardingProperties;

private final ShardingContext shardingContext;
@Deprecated
public class ShardingDataSource extends com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource {

public ShardingDataSource(final ShardingRule shardingRule) {
this(shardingRule, new Properties());
super(shardingRule);
}

public ShardingDataSource(final ShardingRule shardingRule, final Properties props) {
Preconditions.checkNotNull(shardingRule);
Preconditions.checkNotNull(props);
shardingProperties = new ShardingProperties(props);
try {
shardingContext = new ShardingContext(shardingRule, new SQLRouteEngine(shardingRule, DatabaseType.valueFrom(getDatabaseProductName(shardingRule))), new ExecutorEngine(shardingProperties));
} catch (final SQLException ex) {
throw new ShardingJdbcException(ex);
}
}

private String getDatabaseProductName(final ShardingRule shardingRule) throws SQLException {
String result = null;
Collection<Connection> connections = new ArrayList<>(shardingRule.getDataSourceRule().getDataSources().size());
for (DataSource each : shardingRule.getDataSourceRule().getDataSources()) {
Connection connection = each.getConnection();
connections.add(connection);
String databaseProductName = connection.getMetaData().getDatabaseProductName();
Preconditions.checkState(null == result || result.equals(databaseProductName), String.format("Database type inconsistent with '%s' and '%s'", result, databaseProductName));
result = databaseProductName;
}
for (Connection each : connections) {
each.close();
}
return result;
}

@Override
public ShardingConnection getConnection() throws SQLException {
MetricsContext.init(shardingProperties);
return new ShardingConnection(shardingContext);
}

@Override
public final ShardingConnection getConnection(final String username, final String password) throws SQLException {
return getConnection();
super(shardingRule, props);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed 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.
* </p>
*/

package com.dangdang.ddframe.rdb.sharding.api;

import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
import com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import javax.sql.DataSource;
import java.util.Properties;

/**
* 分片数据源工厂.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ShardingDataSourceFactory {

/**
* 创建分片数据源.
*
* @param shardingRule 分片规则
* @return 分片数据源
*/
public static DataSource createDataSource(final ShardingRule shardingRule) {
return new ShardingDataSource(shardingRule);
}

/**
* 创建分片数据源.
*
* @param shardingRule 分片规则
* @param props 属性配置
* @return 分片数据源
*/
public static DataSource createDataSource(final ShardingRule shardingRule, final Properties props) {
return new ShardingDataSource(shardingRule, props);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* </p>
*/

package com.dangdang.ddframe.rdb.sharding.api.props;
package com.dangdang.ddframe.rdb.sharding.config;

import java.util.ArrayList;
import java.util.Collection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* </p>
*/

package com.dangdang.ddframe.rdb.sharding.api.props;
package com.dangdang.ddframe.rdb.sharding.config;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

package com.dangdang.ddframe.rdb.sharding.executor;

import com.dangdang.ddframe.rdb.sharding.api.props.ShardingProperties;
import com.dangdang.ddframe.rdb.sharding.api.props.ShardingPropertiesConstant;
import com.dangdang.ddframe.rdb.sharding.config.ShardingProperties;
import com.dangdang.ddframe.rdb.sharding.config.ShardingPropertiesConstant;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed 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.
* </p>
*/

package com.dangdang.ddframe.rdb.sharding.jdbc;

import com.dangdang.ddframe.rdb.sharding.config.ShardingProperties;
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
import com.dangdang.ddframe.rdb.sharding.constants.DatabaseType;
import com.dangdang.ddframe.rdb.sharding.exception.ShardingJdbcException;
import com.dangdang.ddframe.rdb.sharding.executor.ExecutorEngine;
import com.dangdang.ddframe.rdb.sharding.jdbc.adapter.AbstractDataSourceAdapter;
import com.dangdang.ddframe.rdb.sharding.metrics.MetricsContext;
import com.dangdang.ddframe.rdb.sharding.router.SQLRouteEngine;
import com.google.common.base.Preconditions;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Properties;

/**
* 支持分片的数据源.
*
* @author zhangliang
*/
public class ShardingDataSource extends AbstractDataSourceAdapter {

private final ShardingProperties shardingProperties;

private final ShardingContext shardingContext;

public ShardingDataSource(final ShardingRule shardingRule) {
this(shardingRule, new Properties());
}

public ShardingDataSource(final ShardingRule shardingRule, final Properties props) {
Preconditions.checkNotNull(shardingRule);
Preconditions.checkNotNull(props);
shardingProperties = new ShardingProperties(props);
try {
shardingContext = new ShardingContext(shardingRule, new SQLRouteEngine(shardingRule, DatabaseType.valueFrom(getDatabaseProductName(shardingRule))), new ExecutorEngine(shardingProperties));
} catch (final SQLException ex) {
throw new ShardingJdbcException(ex);
}
}

private String getDatabaseProductName(final ShardingRule shardingRule) throws SQLException {
String result = null;
Collection<Connection> connections = new ArrayList<>(shardingRule.getDataSourceRule().getDataSources().size());
for (DataSource each : shardingRule.getDataSourceRule().getDataSources()) {
Connection connection = each.getConnection();
connections.add(connection);
String databaseProductName = connection.getMetaData().getDatabaseProductName();
Preconditions.checkState(null == result || result.equals(databaseProductName), String.format("Database type inconsistent with '%s' and '%s'", result, databaseProductName));
result = databaseProductName;
}
for (Connection each : connections) {
each.close();
}
return result;
}

@Override
public ShardingConnection getConnection() throws SQLException {
MetricsContext.init(shardingProperties);
return new ShardingConnection(shardingContext);
}

@Override
public final ShardingConnection getConnection(final String username, final String password) throws SQLException {
return getConnection();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Slf4jReporter;
import com.codahale.metrics.Timer;
import com.dangdang.ddframe.rdb.sharding.api.props.ShardingProperties;
import com.dangdang.ddframe.rdb.sharding.api.props.ShardingPropertiesConstant;
import com.dangdang.ddframe.rdb.sharding.config.ShardingProperties;
import com.dangdang.ddframe.rdb.sharding.config.ShardingPropertiesConstant;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.slf4j.LoggerFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.dangdang.ddframe.rdb.integrate.AllIntegrateTests;
import com.dangdang.ddframe.rdb.sharding.api.AllApiTests;
import com.dangdang.ddframe.rdb.sharding.config.AllConfigTests;
import com.dangdang.ddframe.rdb.sharding.constants.AllConstantsTests;
import com.dangdang.ddframe.rdb.sharding.executor.AllExecutorTests;
import com.dangdang.ddframe.rdb.sharding.hint.AllHintTests;
Expand All @@ -35,7 +36,8 @@
@RunWith(Suite.class)
@SuiteClasses({
AllConstantsTests.class,
AllApiTests.class,
AllApiTests.class,
AllConfigTests.class,
AllParserTests.class,
AllRouterTests.class,
AllMergerTests.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import com.dangdang.ddframe.rdb.integrate.AbstractDBUnitTest;
import com.dangdang.ddframe.rdb.integrate.fixture.MultipleKeysModuloDatabaseShardingAlgorithm;
import com.dangdang.ddframe.rdb.sharding.api.ShardingDataSource;
import com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource;
import com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import java.sql.SQLException;

import com.dangdang.ddframe.rdb.integrate.db.AbstractShardingDataBasesOnlyDBUnitTest;
import com.dangdang.ddframe.rdb.sharding.api.ShardingDataSource;
import com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource;
import org.dbunit.DatabaseUnitException;
import org.hamcrest.core.Is;
import org.junit.Before;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.sql.SQLException;

import com.dangdang.ddframe.rdb.integrate.db.AbstractShardingDataBasesOnlyDBUnitTest;
import com.dangdang.ddframe.rdb.sharding.api.ShardingDataSource;
import com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource;
import org.dbunit.DatabaseUnitException;
import org.junit.Before;
import org.junit.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package com.dangdang.ddframe.rdb.integrate.db.pstatement;

import com.dangdang.ddframe.rdb.integrate.db.AbstractShardingDataBasesOnlyDBUnitTest;
import com.dangdang.ddframe.rdb.sharding.api.ShardingDataSource;
import com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource;
import org.dbunit.DatabaseUnitException;
import org.junit.Before;
import org.junit.Test;
Expand Down
Loading

0 comments on commit c66f845

Please sign in to comment.