All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.shardingsphere.shardingjdbc.jdbc.adapter.AbstractDataSourceAdapter Maven / Gradle / Ivy

There is a newer version: 3.1.0
Show newest version
/*
 * Copyright 2016-2018 shardingsphere.io.
 * 

* 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. *

*/ package io.shardingsphere.shardingjdbc.jdbc.adapter; import com.google.common.base.Preconditions; import io.shardingsphere.core.bootstrap.ShardingBootstrap; import io.shardingsphere.core.constant.DatabaseType; import io.shardingsphere.shardingjdbc.jdbc.unsupported.AbstractUnsupportedOperationDataSource; import lombok.Getter; import lombok.Setter; import javax.sql.DataSource; import java.io.PrintWriter; import java.sql.Connection; import java.sql.SQLException; import java.util.Collection; import java.util.logging.Logger; /** * Adapter for {@code Datasource}. * * @author zhangliang * @author panjuan */ @Getter @Setter public abstract class AbstractDataSourceAdapter extends AbstractUnsupportedOperationDataSource { static { ShardingBootstrap.init(); } private final DatabaseType databaseType; private PrintWriter logWriter = new PrintWriter(System.out); public AbstractDataSourceAdapter(final Collection dataSources) throws SQLException { databaseType = getDatabaseType(dataSources); } protected final DatabaseType getDatabaseType(final Collection dataSources) throws SQLException { DatabaseType result = null; for (DataSource each : dataSources) { DatabaseType databaseType = getDatabaseType(each); Preconditions.checkState(null == result || result.equals(databaseType), String.format("Database type inconsistent with '%s' and '%s'", result, databaseType)); result = databaseType; } return result; } private DatabaseType getDatabaseType(final DataSource dataSource) throws SQLException { if (dataSource instanceof AbstractDataSourceAdapter) { return ((AbstractDataSourceAdapter) dataSource).databaseType; } try (Connection connection = dataSource.getConnection()) { return DatabaseType.valueFrom(connection.getMetaData().getDatabaseProductName()); } } @Override public final Logger getParentLogger() { return Logger.getLogger(Logger.GLOBAL_LOGGER_NAME); } @Override public final Connection getConnection(final String username, final String password) throws SQLException { return getConnection(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy