Search in sources :

Example 66 with DbType

use of com.alibaba.druid.DbType in project druid by alibaba.

the class DruidAbstractDataSource method initPhysicalConnection.

public void initPhysicalConnection(Connection conn, Map<String, Object> variables, Map<String, Object> globalVariables) throws SQLException {
    if (conn.getAutoCommit() != defaultAutoCommit) {
        conn.setAutoCommit(defaultAutoCommit);
    }
    if (defaultReadOnly != null) {
        if (conn.isReadOnly() != defaultReadOnly) {
            conn.setReadOnly(defaultReadOnly);
        }
    }
    if (getDefaultTransactionIsolation() != null) {
        if (conn.getTransactionIsolation() != getDefaultTransactionIsolation().intValue()) {
            conn.setTransactionIsolation(getDefaultTransactionIsolation());
        }
    }
    if (getDefaultCatalog() != null && getDefaultCatalog().length() != 0) {
        conn.setCatalog(getDefaultCatalog());
    }
    Collection<String> initSqls = getConnectionInitSqls();
    if (initSqls.size() == 0 && variables == null && globalVariables == null) {
        return;
    }
    Statement stmt = null;
    try {
        stmt = conn.createStatement();
        for (String sql : initSqls) {
            if (sql == null) {
                continue;
            }
            stmt.execute(sql);
        }
        DbType dbType = DbType.of(this.dbTypeName);
        if (JdbcUtils.isMysqlDbType(dbType)) {
            if (variables != null) {
                ResultSet rs = null;
                try {
                    rs = stmt.executeQuery("show variables");
                    while (rs.next()) {
                        String name = rs.getString(1);
                        Object value = rs.getObject(2);
                        variables.put(name, value);
                    }
                } finally {
                    JdbcUtils.close(rs);
                }
            }
            if (globalVariables != null) {
                ResultSet rs = null;
                try {
                    rs = stmt.executeQuery("show global variables");
                    while (rs.next()) {
                        String name = rs.getString(1);
                        Object value = rs.getObject(2);
                        globalVariables.put(name, value);
                    }
                } finally {
                    JdbcUtils.close(rs);
                }
            }
        }
    } finally {
        JdbcUtils.close(stmt);
    }
}
Also used : Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) DbType(com.alibaba.druid.DbType)

Example 67 with DbType

use of com.alibaba.druid.DbType in project druid by alibaba.

the class DruidDataSource method init.

public void init() throws SQLException {
    if (inited) {
        return;
    }
    // bug fixed for dead lock, for issue #2980
    DruidDriver.getInstance();
    final ReentrantLock lock = this.lock;
    try {
        lock.lockInterruptibly();
    } catch (InterruptedException e) {
        throw new SQLException("interrupt", e);
    }
    boolean init = false;
    try {
        if (inited) {
            return;
        }
        initStackTrace = Utils.toString(Thread.currentThread().getStackTrace());
        this.id = DruidDriver.createDataSourceId();
        if (this.id > 1) {
            long delta = (this.id - 1) * 100000;
            this.connectionIdSeedUpdater.addAndGet(this, delta);
            this.statementIdSeedUpdater.addAndGet(this, delta);
            this.resultSetIdSeedUpdater.addAndGet(this, delta);
            this.transactionIdSeedUpdater.addAndGet(this, delta);
        }
        if (this.jdbcUrl != null) {
            this.jdbcUrl = this.jdbcUrl.trim();
            initFromWrapDriverUrl();
        }
        for (Filter filter : filters) {
            filter.init(this);
        }
        if (this.dbTypeName == null || this.dbTypeName.length() == 0) {
            this.dbTypeName = JdbcUtils.getDbType(jdbcUrl, null);
        }
        DbType dbType = DbType.of(this.dbTypeName);
        if (JdbcUtils.isMysqlDbType(dbType)) {
            boolean cacheServerConfigurationSet = false;
            if (this.connectProperties.containsKey("cacheServerConfiguration")) {
                cacheServerConfigurationSet = true;
            } else if (this.jdbcUrl.indexOf("cacheServerConfiguration") != -1) {
                cacheServerConfigurationSet = true;
            }
            if (cacheServerConfigurationSet) {
                this.connectProperties.put("cacheServerConfiguration", "true");
            }
        }
        if (maxActive <= 0) {
            throw new IllegalArgumentException("illegal maxActive " + maxActive);
        }
        if (maxActive < minIdle) {
            throw new IllegalArgumentException("illegal maxActive " + maxActive);
        }
        if (getInitialSize() > maxActive) {
            throw new IllegalArgumentException("illegal initialSize " + this.initialSize + ", maxActive " + maxActive);
        }
        if (timeBetweenLogStatsMillis > 0 && useGlobalDataSourceStat) {
            throw new IllegalArgumentException("timeBetweenLogStatsMillis not support useGlobalDataSourceStat=true");
        }
        if (maxEvictableIdleTimeMillis < minEvictableIdleTimeMillis) {
            throw new SQLException("maxEvictableIdleTimeMillis must be grater than minEvictableIdleTimeMillis");
        }
        if (keepAlive && keepAliveBetweenTimeMillis <= timeBetweenEvictionRunsMillis) {
            throw new SQLException("keepAliveBetweenTimeMillis must be grater than timeBetweenEvictionRunsMillis");
        }
        if (this.driverClass != null) {
            this.driverClass = driverClass.trim();
        }
        initFromSPIServiceLoader();
        resolveDriver();
        initCheck();
        initExceptionSorter();
        initValidConnectionChecker();
        validationQueryCheck();
        if (isUseGlobalDataSourceStat()) {
            dataSourceStat = JdbcDataSourceStat.getGlobal();
            if (dataSourceStat == null) {
                dataSourceStat = new JdbcDataSourceStat("Global", "Global", this.dbTypeName);
                JdbcDataSourceStat.setGlobal(dataSourceStat);
            }
            if (dataSourceStat.getDbType() == null) {
                dataSourceStat.setDbType(this.dbTypeName);
            }
        } else {
            dataSourceStat = new JdbcDataSourceStat(this.name, this.jdbcUrl, this.dbTypeName, this.connectProperties);
        }
        dataSourceStat.setResetStatEnable(this.resetStatEnable);
        connections = new DruidConnectionHolder[maxActive];
        evictConnections = new DruidConnectionHolder[maxActive];
        keepAliveConnections = new DruidConnectionHolder[maxActive];
        SQLException connectError = null;
        if (createScheduler != null && asyncInit) {
            for (int i = 0; i < initialSize; ++i) {
                submitCreateTask(true);
            }
        } else if (!asyncInit) {
            // init connections
            while (poolingCount < initialSize) {
                try {
                    PhysicalConnectionInfo pyConnectInfo = createPhysicalConnection();
                    DruidConnectionHolder holder = new DruidConnectionHolder(this, pyConnectInfo);
                    connections[poolingCount++] = holder;
                } catch (SQLException ex) {
                    LOG.error("init datasource error, url: " + this.getUrl(), ex);
                    if (initExceptionThrow) {
                        connectError = ex;
                        break;
                    } else {
                        Thread.sleep(3000);
                    }
                }
            }
            if (poolingCount > 0) {
                poolingPeak = poolingCount;
                poolingPeakTime = System.currentTimeMillis();
            }
        }
        createAndLogThread();
        createAndStartCreatorThread();
        createAndStartDestroyThread();
        initedLatch.await();
        init = true;
        initedTime = new Date();
        registerMbean();
        if (connectError != null && poolingCount == 0) {
            throw connectError;
        }
        if (keepAlive) {
            // async fill to minIdle
            if (createScheduler != null) {
                for (int i = 0; i < minIdle; ++i) {
                    submitCreateTask(true);
                }
            } else {
                this.emptySignal();
            }
        }
    } catch (SQLException e) {
        LOG.error("{dataSource-" + this.getID() + "} init error", e);
        throw e;
    } catch (InterruptedException e) {
        throw new SQLException(e.getMessage(), e);
    } catch (RuntimeException e) {
        LOG.error("{dataSource-" + this.getID() + "} init error", e);
        throw e;
    } catch (Error e) {
        LOG.error("{dataSource-" + this.getID() + "} init error", e);
        throw e;
    } finally {
        inited = true;
        lock.unlock();
        if (init && LOG.isInfoEnabled()) {
            String msg = "{dataSource-" + this.getID();
            if (this.name != null && !this.name.isEmpty()) {
                msg += ",";
                msg += this.name;
            }
            msg += "} inited";
            LOG.info(msg);
        }
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) SQLException(java.sql.SQLException) JdbcDataSourceStat(com.alibaba.druid.stat.JdbcDataSourceStat) DbType(com.alibaba.druid.DbType) Date(java.util.Date) WallFilter(com.alibaba.druid.wall.WallFilter) Filter(com.alibaba.druid.filter.Filter)

Example 68 with DbType

use of com.alibaba.druid.DbType in project druid by alibaba.

the class DruidDataSource method initValidConnectionChecker.

private void initValidConnectionChecker() {
    if (this.validConnectionChecker != null) {
        return;
    }
    String realDriverClassName = driver.getClass().getName();
    if (JdbcUtils.isMySqlDriver(realDriverClassName)) {
        this.validConnectionChecker = new MySqlValidConnectionChecker();
    } else if (realDriverClassName.equals(JdbcConstants.ORACLE_DRIVER) || realDriverClassName.equals(JdbcConstants.ORACLE_DRIVER2)) {
        this.validConnectionChecker = new OracleValidConnectionChecker();
    } else if (realDriverClassName.equals(JdbcConstants.SQL_SERVER_DRIVER) || realDriverClassName.equals(JdbcConstants.SQL_SERVER_DRIVER_SQLJDBC4) || realDriverClassName.equals(JdbcConstants.SQL_SERVER_DRIVER_JTDS)) {
        this.validConnectionChecker = new MSSQLValidConnectionChecker();
    } else if (realDriverClassName.equals(JdbcConstants.POSTGRESQL_DRIVER) || realDriverClassName.equals(JdbcConstants.ENTERPRISEDB_DRIVER) || realDriverClassName.equals(JdbcConstants.POLARDB_DRIVER)) {
        this.validConnectionChecker = new PGValidConnectionChecker();
    } else if (realDriverClassName.equals(JdbcConstants.OCEANBASE_DRIVER) || (realDriverClassName.equals(JdbcConstants.OCEANBASE_DRIVER2))) {
        DbType dbType = DbType.of(this.dbTypeName);
        this.validConnectionChecker = new OceanBaseValidConnectionChecker(dbType);
    }
}
Also used : DbType(com.alibaba.druid.DbType)

Example 69 with DbType

use of com.alibaba.druid.DbType in project druid by alibaba.

the class StatFilter method createSqlStat.

public JdbcSqlStat createSqlStat(StatementProxy statement, String sql) {
    DataSourceProxy dataSource = statement.getConnectionProxy().getDirectDataSource();
    JdbcDataSourceStat dataSourceStat = dataSource.getDataSourceStat();
    JdbcStatContext context = JdbcStatManager.getInstance().getStatContext();
    String contextSql = context != null ? context.getSql() : null;
    if (contextSql != null && contextSql.length() > 0) {
        return dataSourceStat.createSqlStat(contextSql);
    } else {
        DbType dbType = this.dbType;
        if (dbType == null) {
            dbType = DbType.of(dataSource.getDbType());
        }
        sql = mergeSql(sql, dbType);
        return dataSourceStat.createSqlStat(sql);
    }
}
Also used : DataSourceProxy(com.alibaba.druid.proxy.jdbc.DataSourceProxy) JdbcDataSourceStat(com.alibaba.druid.stat.JdbcDataSourceStat) JdbcStatContext(com.alibaba.druid.stat.JdbcStatContext) DbType(com.alibaba.druid.DbType)

Example 70 with DbType

use of com.alibaba.druid.DbType in project druid by alibaba.

the class DruidXADataSource method initCheck.

protected void initCheck() throws SQLException {
    super.initCheck();
    DbType dbType = DbType.of(this.dbTypeName);
    if (JdbcUtils.H2.equals(dbType)) {
        h2Factory = H2Utils.createJdbcDataSourceFactory();
    }
}
Also used : DbType(com.alibaba.druid.DbType)

Aggregations

DbType (com.alibaba.druid.DbType)114 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)76 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)70 ArrayList (java.util.ArrayList)50 SQLASTOutputVisitor (com.alibaba.druid.sql.visitor.SQLASTOutputVisitor)45 SchemaStatVisitor (com.alibaba.druid.sql.visitor.SchemaStatVisitor)21 TableStat (com.alibaba.druid.stat.TableStat)10 JSONArray (com.alibaba.fastjson.JSONArray)10 MySqlExportParameterVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlExportParameterVisitor)5 ExportParameterVisitor (com.alibaba.druid.sql.visitor.ExportParameterVisitor)5 ParserException (com.alibaba.druid.sql.parser.ParserException)4 Map (java.util.Map)4 JdbcParameter (com.alibaba.druid.proxy.jdbc.JdbcParameter)2 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)2 SQLDeleteStatement (com.alibaba.druid.sql.ast.statement.SQLDeleteStatement)2 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)2 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)2 SQLUpdateStatement (com.alibaba.druid.sql.ast.statement.SQLUpdateStatement)2 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)2 OracleSQLObject (com.alibaba.druid.sql.dialect.oracle.ast.OracleSQLObject)2