-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
124 changed files
with
535 additions
and
270 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...c-core/src/main/java/com/dangdang/ddframe/rdb/sharding/api/ShardingDataSourceFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
...ng-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/jdbc/ShardingDataSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.