ShardingDataSource.java 3.6 KB
Newer Older
T
terrymanu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/**
 * 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;

T
fix #49  
terrymanu 已提交
20
import com.dangdang.ddframe.rdb.sharding.api.props.ShardingProperties;
T
terrymanu 已提交
21 22
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
import com.dangdang.ddframe.rdb.sharding.exception.ShardingJdbcException;
G
fix #16  
gaohongtao 已提交
23
import com.dangdang.ddframe.rdb.sharding.executor.ExecutorEngine;
T
terrymanu 已提交
24
import com.dangdang.ddframe.rdb.sharding.jdbc.ShardingConnection;
G
fix #16  
gaohongtao 已提交
25
import com.dangdang.ddframe.rdb.sharding.jdbc.ShardingContext;
T
terrymanu 已提交
26 27
import com.dangdang.ddframe.rdb.sharding.jdbc.adapter.AbstractDataSourceAdapter;
import com.dangdang.ddframe.rdb.sharding.metrics.MetricsContext;
T
fix #49  
terrymanu 已提交
28
import com.dangdang.ddframe.rdb.sharding.router.SQLRouteEngine;
T
terrymanu 已提交
29 30
import com.google.common.base.Preconditions;

T
terrymanu 已提交
31
import javax.sql.DataSource;
T
fix #49  
terrymanu 已提交
32 33
import java.sql.Connection;
import java.sql.SQLException;
T
terrymanu 已提交
34 35
import java.util.ArrayList;
import java.util.Collection;
T
fix #49  
terrymanu 已提交
36 37
import java.util.Properties;

T
terrymanu 已提交
38 39 40 41 42 43 44
/**
 * 支持分片的数据源.
 * 
 * @author zhangliang
 */
public class ShardingDataSource extends AbstractDataSourceAdapter {
    
T
terrymanu 已提交
45
    private final ShardingProperties shardingProperties;
T
terrymanu 已提交
46
    
T
terrymanu 已提交
47 48
    private final ShardingContext shardingContext;
    
T
terrymanu 已提交
49 50 51 52 53
    public ShardingDataSource(final ShardingRule shardingRule) {
        this(shardingRule, new Properties());
    }
    
    public ShardingDataSource(final ShardingRule shardingRule, final Properties props) {
G
fix #16  
gaohongtao 已提交
54 55
        Preconditions.checkNotNull(shardingRule);
        Preconditions.checkNotNull(props);
T
terrymanu 已提交
56
        shardingProperties = new ShardingProperties(props);
G
fix #16  
gaohongtao 已提交
57
        try {
T
terrymanu 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
            shardingContext = new ShardingContext(shardingRule, new SQLRouteEngine(shardingRule, DatabaseType.valueFrom(getDatabaseProductName(shardingRule))), new ExecutorEngine(shardingProperties));
        } catch (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();
G
fix #16  
gaohongtao 已提交
76
        }
T
terrymanu 已提交
77
        return result;
T
terrymanu 已提交
78 79 80 81
    }
    
    @Override
    public ShardingConnection getConnection() throws SQLException {
T
terrymanu 已提交
82 83
        MetricsContext.init(shardingProperties);
        return new ShardingConnection(shardingContext);
T
terrymanu 已提交
84 85 86 87 88 89 90
    }
    
    @Override
    public final Connection getConnection(final String username, final String password) throws SQLException {
        return getConnection();
    }
}