ShardingConnection.java 5.0 KB
Newer Older
T
terrymanu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/**
 * 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.codahale.metrics.Timer.Context;
21
import com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule;
T
terrymanu 已提交
22 23
import com.dangdang.ddframe.rdb.sharding.jdbc.adapter.AbstractConnectionAdapter;
import com.dangdang.ddframe.rdb.sharding.metrics.MetricsContext;
T
terrymanu 已提交
24
import com.google.common.base.Joiner;
G
fix #16  
gaohongtao 已提交
25 26 27
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
T
terrymanu 已提交
28

T
terrymanu 已提交
29 30 31 32 33 34 35 36 37
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

T
terrymanu 已提交
38 39 40
/**
 * 支持分片的数据库连接.
 * 
T
terrymanu 已提交
41 42
 * @author zhangliang
 * @author gaohongtao
T
terrymanu 已提交
43
 */
G
fix #16  
gaohongtao 已提交
44
@RequiredArgsConstructor
T
terrymanu 已提交
45 46
public final class ShardingConnection extends AbstractConnectionAdapter {
    
G
fix #16  
gaohongtao 已提交
47
    @Getter(AccessLevel.PACKAGE)
T
terrymanu 已提交
48
    private final ShardingContext shardingContext;
T
terrymanu 已提交
49
    
T
terrymanu 已提交
50
    private final Map<String, Connection> connectionMap = new HashMap<>();
T
terrymanu 已提交
51 52 53 54 55 56 57 58 59 60 61
    
    /**
     * 根据数据源名称获取相应的数据库连接.
     * 
     * @param dataSourceName 数据源名称
     * @return 数据库连接
     */
    public Connection getConnection(final String dataSourceName) throws SQLException {
        if (connectionMap.containsKey(dataSourceName)) {
            return connectionMap.get(dataSourceName);
        }
T
terrymanu 已提交
62
        Context metricsContext = MetricsContext.start(Joiner.on("-").join("ShardingConnection-getConnection", dataSourceName));
T
terrymanu 已提交
63 64
        Connection connection = shardingContext.getShardingRule().getDataSourceRule().getDataSource(dataSourceName).getConnection();
        MetricsContext.stop(metricsContext);
T
terrymanu 已提交
65
        replayMethodsInvocation(connection);
T
terrymanu 已提交
66 67 68 69 70 71
        connectionMap.put(dataSourceName, connection);
        return connection;
    }
    
    @Override
    public DatabaseMetaData getMetaData() throws SQLException {
G
fix #16  
gaohongtao 已提交
72
        if (connectionMap.isEmpty()) {
73
            DataSourceRule dataSourceRule = shardingContext.getShardingRule().getDataSourceRule();
T
terrymanu 已提交
74 75
            String dataSourceName = dataSourceRule.getDataSourceNames().iterator().next();
            connectionMap.put(dataSourceName, dataSourceRule.getDataSource(dataSourceName).getConnection());
G
fix #16  
gaohongtao 已提交
76
        }
T
terrymanu 已提交
77
        return connectionMap.values().iterator().next().getMetaData();
T
terrymanu 已提交
78 79 80 81
    }
    
    @Override
    public PreparedStatement prepareStatement(final String sql) throws SQLException {
G
fix #16  
gaohongtao 已提交
82
        return new ShardingPreparedStatement(this, sql);
T
terrymanu 已提交
83 84 85 86
    }
    
    @Override
    public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency) throws SQLException {
G
fix #16  
gaohongtao 已提交
87
        return new ShardingPreparedStatement(this, sql, resultSetType, resultSetConcurrency);
T
terrymanu 已提交
88 89 90 91
    }
    
    @Override
    public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) throws SQLException {
G
fix #16  
gaohongtao 已提交
92
        return new ShardingPreparedStatement(this, sql, resultSetType, resultSetConcurrency, resultSetHoldability);
T
terrymanu 已提交
93 94 95 96
    }
    
    @Override
    public PreparedStatement prepareStatement(final String sql, final int autoGeneratedKeys) throws SQLException {
G
fix #16  
gaohongtao 已提交
97
        return new ShardingPreparedStatement(this, sql, autoGeneratedKeys);
T
terrymanu 已提交
98 99 100 101
    }
    
    @Override
    public PreparedStatement prepareStatement(final String sql, final int[] columnIndexes) throws SQLException {
G
fix #16  
gaohongtao 已提交
102
        return new ShardingPreparedStatement(this, sql, columnIndexes);
T
terrymanu 已提交
103 104 105 106
    }
    
    @Override
    public PreparedStatement prepareStatement(final String sql, final String[] columnNames) throws SQLException {
G
fix #16  
gaohongtao 已提交
107
        return new ShardingPreparedStatement(this, sql, columnNames);
T
terrymanu 已提交
108 109 110 111
    }
    
    @Override
    public Statement createStatement() throws SQLException {
G
fix #16  
gaohongtao 已提交
112
        return new ShardingStatement(this);
T
terrymanu 已提交
113 114 115 116
    }
    
    @Override
    public Statement createStatement(final int resultSetType, final int resultSetConcurrency) throws SQLException {
G
fix #16  
gaohongtao 已提交
117
        return new ShardingStatement(this, resultSetType, resultSetConcurrency);
T
terrymanu 已提交
118 119 120 121
    }
    
    @Override
    public Statement createStatement(final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) throws SQLException {
G
fix #16  
gaohongtao 已提交
122
        return new ShardingStatement(this, resultSetType, resultSetConcurrency, resultSetHoldability);
T
terrymanu 已提交
123 124 125 126 127 128 129
    }
    
    @Override
    public Collection<Connection> getConnections() {
        return connectionMap.values();
    }
}