/*
 * Copyright 2016-2018 shardingsphere.io.
 * <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 io.shardingsphere.proxy.backend.jdbc.datasource;

import io.shardingsphere.core.constant.transaction.TransactionType;
import io.shardingsphere.core.exception.ShardingException;
import io.shardingsphere.core.rule.DataSourceParameter;
import io.shardingsphere.proxy.backend.BackendDataSource;
import lombok.Getter;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Backend data source for JDBC.
 *
 * @author zhaojun
 * @author zhangliang
 */
@Getter
public final class JDBCBackendDataSource implements BackendDataSource {
    
    private final Map<String, DataSource> dataSourceMap;
    
    public JDBCBackendDataSource(final TransactionType transactionType, final Map<String, DataSourceParameter> dataSourceParameters) {
        dataSourceMap = createDataSourceMap(transactionType, dataSourceParameters);
    }
    
    private Map<String, DataSource> createDataSourceMap(final TransactionType transactionType, final Map<String, DataSourceParameter> dataSourceParameters) {
        Map<String, DataSource> result = new LinkedHashMap<>(dataSourceParameters.size());
        for (Entry<String, DataSourceParameter> entry : dataSourceParameters.entrySet()) {
            try {
                result.put(entry.getKey(), getBackendDataSourceFactory(transactionType).build(entry.getKey(), entry.getValue()));
            // CHECKSTYLE:OFF
            } catch (final Exception ex) {
                // CHECKSTYLE:ON
                throw new ShardingException(String.format("Can not build data source, name is `%s`.", entry.getKey()), ex);
            }
        }
        return result;
    }
    
    private JDBCBackendDataSourceFactory getBackendDataSourceFactory(final TransactionType transactionType) {
        switch (transactionType) {
            case XA:
                return new JDBCXABackendDataSourceFactory();
            default:
                return new JDBCRawBackendDataSourceFactory();
        }
    }
    
    /**
     * Get connection.
     *
     * @param dataSourceName data source name
     * @return connection
     * @throws SQLException SQL exception
     */
    public Connection getConnection(final String dataSourceName) throws SQLException {
        return dataSourceMap.get(dataSourceName).getConnection();
    }
}

@@ -883,39 +883,186 @@
 ere.
-proxy.backend.BackendDataSource
+jdbc.orchestration.internal.jdbc.datasource.CircuitBreakerDataSource;%0Aimport io.shardingsphere.proxy.backend.BackendDataSource;%0Aimport io.shardingsphere.proxy.config.RuleRegistry
 ;%0Aim
@@ -1165,24 +1165,53 @@
 LException;%0A
+import java.util.Collection;%0A
 import java.
@@ -1362,16 +1362,35 @@
 ngliang%0A
+ * @author panjuan%0A
  */%0A@Get
@@ -1569,140 +1569,639 @@
 rce(
-final TransactionType transactionType, final Map%3CString, DataSourceParameter%3E dataSourceParameters) %7B%0A        dataSourceMap = create
+) %7B%0A        dataSourceMap = createDataSourceMap();%0A    %7D%0A    %0A    private Map%3CString, DataSource%3E createDataSourceMap() %7B%0A        TransactionType transactionType = RuleRegistry.getInstance().getTransactionType();%0A        Map%3CString, DataSourceParameter%3E dataSourceParameters = RuleRegistry.getInstance().getDataSourceConfigurationMap();%0A        Collection%3CString%3E circuitBreakerDataSourceNames = RuleRegistry.getInstance().getCircuitBreakerDataSourceNames();%0A        if (!circuitBreakerDataSourceNames.isEmpty()) %7B%0A            return getCircuitBreakerDataSourceMap(circuitBreakerDataSourceNames);%0A        %7D%0A        return getNormal
 Data
@@ -2289,38 +2289,41 @@
 ng, DataSource%3E 
-create
+getNormal
 DataSourceMap(fi
@@ -3032,32 +3032,387 @@
 ult;%0A    %7D%0A    %0A
+    private Map%3CString, DataSource%3E getCircuitBreakerDataSourceMap(final Collection%3CString%3E circuitBreakerDataSourceNames) %7B%0A        Map%3CString, DataSource%3E result = new LinkedHashMap%3C%3E();%0A        for (String each : circuitBreakerDataSourceNames) %7B%0A            result.put(each, new CircuitBreakerDataSource());%0A        %7D%0A        return result;%0A    %7D%0A    %0A
     private JDBC

add getNormalDataSourceMap