/*
 * 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.rule.DataSourceParameter;
import io.shardingsphere.proxy.backend.BackendDataSource;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

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
@Slf4j
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
                log.warn("Can not build {} failed with {}", 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();
    }
}

@@ -739,16 +739,75 @@
 onType;%0A
+import io.shardingsphere.core.exception.ShardingException;%0A
 import i
@@ -937,42 +937,8 @@
 ter;
-%0Aimport lombok.extern.slf4j.Slf4j;
 %0A%0Aim
@@ -1202,15 +1202,8 @@
 ter%0A
-@Slf4j%0A
 publ
@@ -2181,49 +2181,91 @@
     
-log.warn(%22Can not build %7B%7D failed with %7B%7D
+throw new ShardingException(String.format(%22Can not build data source, name is %60%25s%60.
 %22, e
@@ -2277,16 +2277,17 @@
 getKey()
+)
 , ex);%0A 

throw exception when data source build failure