Search in sources :

Example 6 with JdbcDataSource

use of io.mycat.datasource.jdbc.datasource.JdbcDataSource in project Mycat2 by MyCATApache.

the class JdbcDatasourcePoolImpl method getAvailableNumber.

@Override
public Integer getAvailableNumber() {
    JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
    JdbcDataSource jdbcDataSource = jdbcConnectionManager.getDatasourceInfo().get(targetName);
    return jdbcDataSource.getMaxCon() - jdbcDataSource.getUsedCount();
}
Also used : JdbcDataSource(io.mycat.datasource.jdbc.datasource.JdbcDataSource) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)

Example 7 with JdbcDataSource

use of io.mycat.datasource.jdbc.datasource.JdbcDataSource in project Mycat2 by MyCATApache.

the class TestMySQLManagerImpl method getWriteableConnectionMap.

@Override
@SneakyThrows
public Map<String, java.sql.Connection> getWriteableConnectionMap() {
    ConcurrentHashMap.KeySetView<String, DruidDataSource> strings = nameMap.keySet();
    JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
    Map<String, JdbcDataSource> datasourceInfo = jdbcConnectionManager.getDatasourceInfo();
    HashMap<String, Connection> map = new HashMap<>();
    for (String string : strings) {
        JdbcDataSource jdbcDataSource = datasourceInfo.get(string);
        DatasourceConfig config = jdbcDataSource.getConfig();
        if (jdbcDataSource.isMySQLType()) {
            if (InstanceType.valueOf(Optional.ofNullable(config.getInstanceType()).orElse("READ_WRITE").toUpperCase()).isWriteType()) {
                Connection connection = jdbcDataSource.getDataSource().getConnection();
                map.put(string, connection);
            }
        }
    }
    return map;
}
Also used : DatasourceConfig(io.mycat.config.DatasourceConfig) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) JdbcDataSource(io.mycat.datasource.jdbc.datasource.JdbcDataSource) Connection(java.sql.Connection) NewMycatConnection(io.mycat.newquery.NewMycatConnection) SqlConnection(io.vertx.sqlclient.SqlConnection) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) SneakyThrows(lombok.SneakyThrows)

Example 8 with JdbcDataSource

use of io.mycat.datasource.jdbc.datasource.JdbcDataSource in project Mycat2 by MyCATApache.

the class SeataMySQLXADatasourceProvider method createDataSource.

@Override
public JdbcDataSource createDataSource(DatasourceConfig config) {
    String username = config.getUser();
    String password = config.getPassword();
    String url = Objects.requireNonNull(config.getUrl());
    String dbType = config.getDbType();
    int maxRetryCount = config.getMaxRetryCount();
    List<String> initSQLs = config.getInitSqls();
    int maxCon = config.getMaxCon();
    int minCon = config.getMinCon();
    MysqlXADataSource datasource = new MysqlXADataSource();
    datasource.setPassword(password);
    datasource.setUser(username);
    datasource.setUrl(url);
    return new JdbcDataSource(config, new DataSourceProxyXA(datasource));
}
Also used : DataSourceProxyXA(io.seata.rm.datasource.xa.DataSourceProxyXA) MysqlXADataSource(com.mysql.cj.jdbc.MysqlXADataSource) JdbcDataSource(io.mycat.datasource.jdbc.datasource.JdbcDataSource)

Example 9 with JdbcDataSource

use of io.mycat.datasource.jdbc.datasource.JdbcDataSource in project Mycat2 by MyCATApache.

the class DruidDatasourceProvider method createDataSource.

@Override
public JdbcDataSource createDataSource(DatasourceConfig config) {
    if (System.getProperty("druid.mysql.usePingMethod") == null) {
        System.setProperty("druid.mysql.usePingMethod", "false");
    }
    String username = config.getUser();
    String password = config.getPassword();
    String url = Objects.requireNonNull(config.getUrl());
    String dbType = config.getDbType();
    int maxRetryCount = config.getMaxRetryCount();
    List<String> initSQLs = config.getInitSqls();
    int maxCon = config.getMaxCon();
    int minCon = config.getMinCon();
    DruidDataSource datasource = new DruidDataSource();
    datasource.setPassword(password);
    datasource.setUsername(username);
    datasource.setUrl(url);
    datasource.setMaxWait(config.getMaxConnectTimeout());
    datasource.setMaxActive(maxCon);
    datasource.setMinIdle(minCon);
    datasource.setKeepAlive(true);
    // datasource.setTestOnReturn(true);
    // datasource.setTestOnBorrow(true);
    datasource.setValidationQuery("select 'x'");
    datasource.setTestWhileIdle(true);
    datasource.setQueryTimeout(config.getQueryTimeout());
    if (maxRetryCount > 0) {
        datasource.setConnectionErrorRetryAttempts(maxRetryCount);
    }
    if (dbType != null) {
        datasource.setDbType(dbType);
    }
    if (initSQLs != null) {
        datasource.setConnectionInitSqls(initSQLs);
    }
    return new JdbcDataSource(config, datasource);
}
Also used : JdbcDataSource(io.mycat.datasource.jdbc.datasource.JdbcDataSource) DruidDataSource(com.alibaba.druid.pool.DruidDataSource)

Example 10 with JdbcDataSource

use of io.mycat.datasource.jdbc.datasource.JdbcDataSource in project Mycat2 by MyCATApache.

the class PrototypeHandlerImpl method onJdbc.

private Optional<List<Object[]>> onJdbc(String statement) {
    String datasourceDs = null;
    if (MetaClusterCurrent.exist(ReplicaSelectorManager.class)) {
        ReplicaSelectorManager replicaSelectorManager = MetaClusterCurrent.wrapper(ReplicaSelectorManager.class);
        datasourceDs = replicaSelectorManager.getDatasourceNameByReplicaName(MetadataManager.getPrototype(), true, null);
    } else {
        datasourceDs = "prototypeDs";
    }
    JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
    Map<String, JdbcDataSource> datasourceInfo = jdbcConnectionManager.getDatasourceInfo();
    if (datasourceInfo.containsKey(datasourceDs)) {
        datasourceDs = null;
    } else {
        List<DatasourceConfig> configAsList = jdbcConnectionManager.getConfigAsList();
        if (!configAsList.isEmpty()) {
            datasourceDs = configAsList.get(0).getName();
        } else {
            datasourceDs = null;
        }
    }
    if (datasourceDs == null) {
        datasourceDs = datasourceInfo.values().stream().filter(i -> i.isMySQLType()).map(i -> i.getName()).findFirst().orElse(null);
    }
    if (datasourceDs == null) {
        return Optional.empty();
    }
    try (DefaultConnection connection = jdbcConnectionManager.getConnection(datasourceDs)) {
        Connection rawConnection = connection.getRawConnection();
        Statement jdbcStatement1 = rawConnection.createStatement();
        ResultSet resultSet = jdbcStatement1.executeQuery(statement);
        int columnCount = resultSet.getMetaData().getColumnCount();
        List<Object[]> res = new ArrayList<>();
        while (resultSet.next()) {
            Object[] objects = new Object[columnCount];
            for (int i = 0; i < columnCount; i++) {
                objects[i] = resultSet.getObject(i + 1);
            }
            res.add(objects);
        }
        return Optional.of(res);
    } catch (Exception e) {
        LOGGER.warn("", e);
    }
    return Optional.empty();
}
Also used : ReplicaSelectorManager(io.mycat.replica.ReplicaSelectorManager) MetadataManager(io.mycat.MetadataManager) java.util(java.util) Connection(java.sql.Connection) TableHandler(io.mycat.TableHandler) MetaClusterCurrent(io.mycat.MetaClusterCurrent) LoggerFactory(org.slf4j.LoggerFactory) JdbcDataSource(io.mycat.datasource.jdbc.datasource.JdbcDataSource) SQLLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLLiteralExpr) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) Supplier(java.util.function.Supplier) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) PrepareExecutor(io.mycat.calcite.PrepareExecutor) SchemaHandler(io.mycat.calcite.table.SchemaHandler) ResultSet(java.sql.ResultSet) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) DatasourceConfig(io.mycat.config.DatasourceConfig) com.alibaba.druid.sql.dialect.mysql.ast.statement(com.alibaba.druid.sql.dialect.mysql.ast.statement) SQLUtils(com.alibaba.druid.sql.SQLUtils) Logger(org.slf4j.Logger) PatternMatcher(org.apache.doris.common.PatternMatcher) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) Collectors(java.util.stream.Collectors) ReplicaSelectorManager(io.mycat.replica.ReplicaSelectorManager) NameMap(io.mycat.util.NameMap) Statement(java.sql.Statement) SQLTextLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLTextLiteralExpr) MysqlVariableService(io.mycat.MysqlVariableService) com.alibaba.druid.sql.ast.statement(com.alibaba.druid.sql.ast.statement) Statement(java.sql.Statement) JdbcDataSource(io.mycat.datasource.jdbc.datasource.JdbcDataSource) Connection(java.sql.Connection) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) DatasourceConfig(io.mycat.config.DatasourceConfig) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) ResultSet(java.sql.ResultSet) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)

Aggregations

JdbcDataSource (io.mycat.datasource.jdbc.datasource.JdbcDataSource)10 JdbcConnectionManager (io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)7 DatasourceConfig (io.mycat.config.DatasourceConfig)3 SneakyThrows (lombok.SneakyThrows)3 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)2 SQLUtils (com.alibaba.druid.sql.SQLUtils)2 PrepareExecutor (io.mycat.calcite.PrepareExecutor)2 SchemaHandler (io.mycat.calcite.table.SchemaHandler)2 Connection (java.sql.Connection)2 XaLog (cn.mycat.vertx.xa.XaLog)1 DruidXADataSource (com.alibaba.druid.pool.xa.DruidXADataSource)1 SQLCommentHint (com.alibaba.druid.sql.ast.SQLCommentHint)1 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)1 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)1 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)1 SQLLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLLiteralExpr)1 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)1 SQLTextLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLTextLiteralExpr)1 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)1