Search in sources :

Example 21 with JdbcDataSourceStat

use of com.alibaba.druid.stat.JdbcDataSourceStat in project druid by alibaba.

the class JdbcDataSourceStatTest method test_max_10_str.

public void test_max_10_str() throws Exception {
    Properties connectProperties = new Properties();
    connectProperties.put(Constants.DRUID_STAT_SQL_MAX_SIZE, "10");
    JdbcDataSourceStat stat = new JdbcDataSourceStat("", "", "mysql", connectProperties);
    for (int i = 0; i < 1000 * 1; ++i) {
        stat.createSqlStat("select " + i);
    }
    Assert.assertEquals(10, stat.getSqlStatMap().size());
}
Also used : JdbcDataSourceStat(com.alibaba.druid.stat.JdbcDataSourceStat) Properties(java.util.Properties)

Example 22 with JdbcDataSourceStat

use of com.alibaba.druid.stat.JdbcDataSourceStat in project druid by alibaba.

the class JdbcDataSourceStatTest method test_max.

public void test_max() throws Exception {
    JdbcDataSourceStat stat = new JdbcDataSourceStat("", "");
    for (int i = 0; i < 1000 * 10; ++i) {
        stat.createSqlStat("select " + i);
    }
    Assert.assertEquals(1000, stat.getSqlStatMap().size());
}
Also used : JdbcDataSourceStat(com.alibaba.druid.stat.JdbcDataSourceStat)

Example 23 with JdbcDataSourceStat

use of com.alibaba.druid.stat.JdbcDataSourceStat in project druid by alibaba.

the class ParamTest method test_1.

public void test_1() throws Exception {
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:");
    dataSource.setInitialSize(10);
    dataSource.setMaxActive(10);
    dataSource.setMinIdle(0);
    dataSource.setMaxIdle(10);
    dataSource.setFilters("stat");
    Assert.assertEquals(1, dataSource.getProxyFilters().size());
    JdbcStatManager.getInstance().reset();
    dataSource.init();
    JdbcDataSourceStat stat = dataSource.getDataSourceStat();
    Assert.assertEquals(10, stat.getConnectionStat().getConnectCount());
    for (int i = 0; i < 10; ++i) {
        Connection conn = dataSource.getConnection();
        conn.close();
    }
    Assert.assertEquals(10, stat.getConnectionStat().getConnectCount());
    dataSource.close();
}
Also used : Connection(java.sql.Connection) JdbcDataSourceStat(com.alibaba.druid.stat.JdbcDataSourceStat) DruidDataSource(com.alibaba.druid.pool.DruidDataSource)

Example 24 with JdbcDataSourceStat

use of com.alibaba.druid.stat.JdbcDataSourceStat in project druid by alibaba.

the class CounterFilterTest method test_countFilter.

public void test_countFilter() throws Exception {
    DataSourceProxyConfig config = new DataSourceProxyConfig();
    config.setUrl("");
    DataSourceProxyImpl dataSource = new DataSourceProxyImpl(null, config);
    JdbcDataSourceStat dataSourceStat = dataSource.getDataSourceStat();
    StatFilter filter = new StatFilter();
    filter.init(dataSource);
    dataSourceStat.reset();
    Assert.assertNull(StatFilter.getStatFilter(dataSource));
    Assert.assertNull(dataSourceStat.getSqlStat(Integer.MAX_VALUE));
    Assert.assertNull(dataSourceStat.getConnectionStat().getConnectLastTime());
    FilterChain chain = new FilterChainImpl(dataSource) {

        public ConnectionProxy connection_connect(Properties info) throws SQLException {
            throw new SQLException();
        }
    };
    Exception error = null;
    try {
        filter.connection_connect(chain, new Properties());
    } catch (SQLException ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
    Assert.assertEquals(1, dataSourceStat.getConnectionStat().getConnectErrorCount());
    Assert.assertNotNull(dataSourceStat.getConnectionStat().getConnectLastTime());
}
Also used : FilterChainImpl(com.alibaba.druid.filter.FilterChainImpl) DataSourceProxyConfig(com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig) DataSourceProxyImpl(com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl) SQLException(java.sql.SQLException) FilterChain(com.alibaba.druid.filter.FilterChain) StatFilter(com.alibaba.druid.filter.stat.StatFilter) JdbcDataSourceStat(com.alibaba.druid.stat.JdbcDataSourceStat) Properties(java.util.Properties) SQLException(java.sql.SQLException)

Example 25 with JdbcDataSourceStat

use of com.alibaba.druid.stat.JdbcDataSourceStat in project druid by alibaba.

the class DruidDataSource method init.

public void init() throws SQLException {
    if (inited) {
        return;
    }
    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.connectionIdSeed.addAndGet(delta);
            this.statementIdSeed.addAndGet(delta);
            this.resultSetIdSeed.addAndGet(delta);
            this.transactionIdSeed.addAndGet(delta);
        }
        if (this.jdbcUrl != null) {
            this.jdbcUrl = this.jdbcUrl.trim();
            initFromWrapDriverUrl();
        }
        for (Filter filter : filters) {
            filter.init(this);
        }
        if (this.dbType == null || this.dbType.length() == 0) {
            this.dbType = JdbcUtils.getDbType(jdbcUrl, null);
        }
        if (//
        JdbcConstants.MYSQL.equals(this.dbType) || JdbcConstants.MARIADB.equals(this.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 (this.driverClass != null) {
            this.driverClass = driverClass.trim();
        }
        initFromSPIServiceLoader();
        if (this.driver == null) {
            if (this.driverClass == null || this.driverClass.isEmpty()) {
                this.driverClass = JdbcUtils.getDriverClassName(this.jdbcUrl);
            }
            if (MockDriver.class.getName().equals(driverClass)) {
                driver = MockDriver.instance;
            } else {
                driver = JdbcUtils.createDriver(driverClassLoader, driverClass);
            }
        } else {
            if (this.driverClass == null) {
                this.driverClass = driver.getClass().getName();
            }
        }
        initCheck();
        initExceptionSorter();
        initValidConnectionChecker();
        validationQueryCheck();
        if (isUseGlobalDataSourceStat()) {
            dataSourceStat = JdbcDataSourceStat.getGlobal();
            if (dataSourceStat == null) {
                dataSourceStat = new JdbcDataSourceStat("Global", "Global", this.dbType);
                JdbcDataSourceStat.setGlobal(dataSourceStat);
            }
            if (dataSourceStat.getDbType() == null) {
                dataSourceStat.setDbType(this.dbType);
            }
        } else {
            dataSourceStat = new JdbcDataSourceStat(this.name, this.jdbcUrl, this.dbType, this.connectProperties);
        }
        dataSourceStat.setResetStatEnable(this.resetStatEnable);
        connections = new DruidConnectionHolder[maxActive];
        evictConnections = new DruidConnectionHolder[maxActive];
        keepAliveConnections = new DruidConnectionHolder[maxActive];
        SQLException connectError = null;
        try {
            // init connections
            for (int i = 0, size = getInitialSize(); i < size; ++i) {
                PhysicalConnectionInfo pyConnectInfo = createPhysicalConnection();
                DruidConnectionHolder holder = new DruidConnectionHolder(this, pyConnectInfo);
                connections[poolingCount] = holder;
                incrementPoolingCount();
            }
            if (poolingCount > 0) {
                poolingPeak = poolingCount;
                poolingPeakTime = System.currentTimeMillis();
            }
        } catch (SQLException ex) {
            LOG.error("init datasource error, url: " + this.getUrl(), ex);
            connectError = ex;
        }
        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) {
                    createTaskCount++;
                    CreateConnectionTask task = new CreateConnectionTask();
                    createScheduler.submit(task);
                }
            } 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);
    } finally {
        inited = true;
        lock.unlock();
        if (init && LOG.isInfoEnabled()) {
            LOG.info("{dataSource-" + this.getID() + "} inited");
        }
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) MockDriver(com.alibaba.druid.mock.MockDriver) SQLException(java.sql.SQLException) JdbcDataSourceStat(com.alibaba.druid.stat.JdbcDataSourceStat) Date(java.util.Date) WallFilter(com.alibaba.druid.wall.WallFilter) Filter(com.alibaba.druid.filter.Filter)

Aggregations

JdbcDataSourceStat (com.alibaba.druid.stat.JdbcDataSourceStat)25 JdbcSqlStat (com.alibaba.druid.stat.JdbcSqlStat)6 SQLException (java.sql.SQLException)4 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)3 JdbcConnectionStat (com.alibaba.druid.stat.JdbcConnectionStat)3 JdbcStatContext (com.alibaba.druid.stat.JdbcStatContext)3 Connection (java.sql.Connection)3 Properties (java.util.Properties)3 ConnectionProxy (com.alibaba.druid.proxy.jdbc.ConnectionProxy)2 Savepoint (java.sql.Savepoint)2 HashMap (java.util.HashMap)2 CompositeDataSupport (javax.management.openmbean.CompositeDataSupport)2 Filter (com.alibaba.druid.filter.Filter)1 FilterChain (com.alibaba.druid.filter.FilterChain)1 FilterChainImpl (com.alibaba.druid.filter.FilterChainImpl)1 StatFilter (com.alibaba.druid.filter.stat.StatFilter)1 MockDriver (com.alibaba.druid.mock.MockDriver)1 DataSourceProxy (com.alibaba.druid.proxy.jdbc.DataSourceProxy)1 DataSourceProxyConfig (com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig)1 DataSourceProxyImpl (com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl)1