ShardingConnection.java 6.9 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 21 22 23 24 25 26 27
/**
 * 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 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;
G
fix #16  
gaohongtao 已提交
28
import javax.sql.DataSource;
T
terrymanu 已提交
29 30

import com.codahale.metrics.Timer.Context;
G
fix #16  
gaohongtao 已提交
31
import com.dangdang.ddframe.rdb.sharding.exception.ShardingJdbcException;
T
terrymanu 已提交
32 33
import com.dangdang.ddframe.rdb.sharding.jdbc.adapter.AbstractConnectionAdapter;
import com.dangdang.ddframe.rdb.sharding.metrics.MetricsContext;
G
fix #16  
gaohongtao 已提交
34 35 36 37 38 39
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.Collections2;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
T
terrymanu 已提交
40 41 42 43

/**
 * 支持分片的数据库连接.
 * 
T
terrymanu 已提交
44 45
 * @author zhangliang
 * @author gaohongtao
T
terrymanu 已提交
46
 */
G
fix #16  
gaohongtao 已提交
47
@RequiredArgsConstructor
T
terrymanu 已提交
48 49
public final class ShardingConnection extends AbstractConnectionAdapter {
    
G
fix #16  
gaohongtao 已提交
50
    @Getter(AccessLevel.PACKAGE)
T
terrymanu 已提交
51
    private final ShardingContext shardingContext;
T
terrymanu 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64
    
    private Map<String, Connection> connectionMap = new HashMap<>();
    
    /**
     * 根据数据源名称获取相应的数据库连接.
     * 
     * @param dataSourceName 数据源名称
     * @return 数据库连接
     */
    public Connection getConnection(final String dataSourceName) throws SQLException {
        if (connectionMap.containsKey(dataSourceName)) {
            return connectionMap.get(dataSourceName);
        }
T
terrymanu 已提交
65 66 67
        Context metricsContext = MetricsContext.start("ShardingConnection-getConnection", dataSourceName);
        Connection connection = shardingContext.getShardingRule().getDataSourceRule().getDataSource(dataSourceName).getConnection();
        MetricsContext.stop(metricsContext);
T
terrymanu 已提交
68 69 70 71 72 73 74
        replayMethodsInvovation(connection);
        connectionMap.put(dataSourceName, connection);
        return connection;
    }
    
    @Override
    public DatabaseMetaData getMetaData() throws SQLException {
G
fix #16  
gaohongtao 已提交
75
        if (connectionMap.isEmpty()) {
T
terrymanu 已提交
76
            return getDatabaseMetaDataFromDataSource(shardingContext.getShardingRule().getDataSourceRule().getDataSources());
G
fix #16  
gaohongtao 已提交
77
        }
T
terrymanu 已提交
78
        return getDatabaseMetaDataFromConnection(connectionMap.values());
G
fix #16  
gaohongtao 已提交
79 80 81
    }
    
    public static DatabaseMetaData getDatabaseMetaDataFromDataSource(final Collection<DataSource> dataSources) {
T
terrymanu 已提交
82
        Collection<Connection> connections = null;
G
fix #16  
gaohongtao 已提交
83
        try {
T
terrymanu 已提交
84
            connections = Collections2.transform(dataSources, new Function<DataSource, Connection>() {
G
fix #16  
gaohongtao 已提交
85 86 87 88 89
                
                @Override
                public Connection apply(final DataSource input) {
                    try {
                        return input.getConnection();
T
terrymanu 已提交
90 91
                    } catch (final SQLException ex) {
                        throw new ShardingJdbcException(ex);
G
fix #16  
gaohongtao 已提交
92 93 94
                    }
                }
            });
T
terrymanu 已提交
95
            return getDatabaseMetaDataFromConnection(connections);
G
fix #16  
gaohongtao 已提交
96
        } finally {
T
terrymanu 已提交
97 98
            if (null != connections) {
                for (Connection each : connections) {
G
fix #16  
gaohongtao 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
                    try {
                        each.close();
                    } catch (final SQLException ignored) {
                    }
                }
            }
        }
    }
    
    private static DatabaseMetaData getDatabaseMetaDataFromConnection(final Collection<Connection> connections) {
        String databaseProductName = null;
        DatabaseMetaData result = null;
        for (Connection each : connections) {
            String databaseProductNameInEach;
            DatabaseMetaData metaDataInEach;
            try {
                metaDataInEach = each.getMetaData();
                databaseProductNameInEach = metaDataInEach.getDatabaseProductName();
            } catch (final SQLException ex) {
                throw new ShardingJdbcException("Can not get data source DatabaseProductName", ex);
            }
            Preconditions.checkState(null == databaseProductName || databaseProductName.equals(databaseProductNameInEach),
                    String.format("Database type inconsistent with '%s' and '%s'", databaseProductName, databaseProductNameInEach));
            databaseProductName = databaseProductNameInEach;
            result = metaDataInEach;
        }
        return result;
T
terrymanu 已提交
126 127 128 129
    }
    
    @Override
    public PreparedStatement prepareStatement(final String sql) throws SQLException {
G
fix #16  
gaohongtao 已提交
130
        return new ShardingPreparedStatement(this, sql);
T
terrymanu 已提交
131 132 133 134
    }
    
    @Override
    public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency) throws SQLException {
G
fix #16  
gaohongtao 已提交
135
        return new ShardingPreparedStatement(this, sql, resultSetType, resultSetConcurrency);
T
terrymanu 已提交
136 137 138 139
    }
    
    @Override
    public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) throws SQLException {
G
fix #16  
gaohongtao 已提交
140
        return new ShardingPreparedStatement(this, sql, resultSetType, resultSetConcurrency, resultSetHoldability);
T
terrymanu 已提交
141 142 143 144
    }
    
    @Override
    public PreparedStatement prepareStatement(final String sql, final int autoGeneratedKeys) throws SQLException {
G
fix #16  
gaohongtao 已提交
145
        return new ShardingPreparedStatement(this, sql, autoGeneratedKeys);
T
terrymanu 已提交
146 147 148 149
    }
    
    @Override
    public PreparedStatement prepareStatement(final String sql, final int[] columnIndexes) throws SQLException {
G
fix #16  
gaohongtao 已提交
150
        return new ShardingPreparedStatement(this, sql, columnIndexes);
T
terrymanu 已提交
151 152 153 154
    }
    
    @Override
    public PreparedStatement prepareStatement(final String sql, final String[] columnNames) throws SQLException {
G
fix #16  
gaohongtao 已提交
155
        return new ShardingPreparedStatement(this, sql, columnNames);
T
terrymanu 已提交
156 157 158 159
    }
    
    @Override
    public Statement createStatement() throws SQLException {
G
fix #16  
gaohongtao 已提交
160
        return new ShardingStatement(this);
T
terrymanu 已提交
161 162 163 164
    }
    
    @Override
    public Statement createStatement(final int resultSetType, final int resultSetConcurrency) throws SQLException {
G
fix #16  
gaohongtao 已提交
165
        return new ShardingStatement(this, resultSetType, resultSetConcurrency);
T
terrymanu 已提交
166 167 168 169
    }
    
    @Override
    public Statement createStatement(final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) throws SQLException {
G
fix #16  
gaohongtao 已提交
170
        return new ShardingStatement(this, resultSetType, resultSetConcurrency, resultSetHoldability);
T
terrymanu 已提交
171 172 173 174 175 176 177
    }
    
    @Override
    public Collection<Connection> getConnections() {
        return connectionMap.values();
    }
}