Search in sources :

Example 36 with MockDriver

use of com.alibaba.druid.mock.MockDriver in project druid by alibaba.

the class TestIdel3 method test_idle2.

public void test_idle2() throws Exception {
    MockDriver driver = new MockDriver();
    final DruidDataSource dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setDriver(driver);
    dataSource.setInitialSize(1);
    dataSource.setMaxActive(14);
    dataSource.setMaxIdle(14);
    dataSource.setMinIdle(1);
    // 300 / 10
    dataSource.setMinEvictableIdleTimeMillis(3 * 100);
    // 180 / 10
    dataSource.setTimeBetweenEvictionRunsMillis(18 * 10);
    dataSource.setTestWhileIdle(true);
    dataSource.setTestOnBorrow(false);
    dataSource.setValidationQuery("SELECT 1");
    dataSource.setFilters("stat");
    ManagementFactory.getPlatformMBeanServer().registerMBean(dataSource, new ObjectName("com.alibaba:type=DataSource"));
    // 第一次创建连接
    {
        Assert.assertEquals(0, dataSource.getCreateCount());
        Assert.assertEquals(0, dataSource.getActiveCount());
        Connection conn = dataSource.getConnection();
        Assert.assertEquals(dataSource.getInitialSize(), dataSource.getCreateCount());
        Assert.assertEquals(dataSource.getInitialSize(), driver.getConnections().size());
        Assert.assertEquals(1, dataSource.getActiveCount());
        conn.close();
        Assert.assertEquals(0, dataSource.getDestroyCount());
        Assert.assertEquals(2, driver.getConnections().size());
        Assert.assertEquals(2, dataSource.getCreateCount());
        Assert.assertEquals(0, dataSource.getActiveCount());
    }
    {
        // 并发创建14个
        int count = 14;
        Connection[] connections = new Connection[count];
        for (int i = 0; i < count; ++i) {
            connections[i] = dataSource.getConnection();
            Assert.assertEquals(i + 1, dataSource.getActiveCount());
        }
        Assert.assertEquals(dataSource.getMaxActive(), dataSource.getCreateCount());
        Assert.assertEquals(count, driver.getConnections().size());
        // 全部关闭
        for (int i = 0; i < count; ++i) {
            connections[i].close();
            Assert.assertEquals(count - i - 1, dataSource.getActiveCount());
        }
        Assert.assertEquals(dataSource.getMaxActive(), dataSource.getCreateCount());
        Assert.assertEquals(0, dataSource.getActiveCount());
        Assert.assertEquals(14, driver.getConnections().size());
    }
    concurrent(dataSource, 100, 1000);
    concurrent(dataSource, 1, 1000 * 1000);
    Thread.sleep(1000 * 10);
    concurrent(dataSource, 1, 1000 * 1000);
    Thread.sleep(1000 * 10);
    concurrent(dataSource, 1000, 1000 * 1000);
    Assert.assertEquals(driver.getConnections().size(), dataSource.getPoolingCount());
    Assert.assertEquals(0, dataSource.getActiveCount());
    // 连续打开关闭单个连接
    for (int i = 0; i < 1000; ++i) {
        Assert.assertEquals(0, dataSource.getActiveCount());
        Connection conn = dataSource.getConnection();
        Assert.assertEquals(1, dataSource.getActiveCount());
        Thread.sleep(10);
        conn.close();
    }
    // Assert.assertEquals(2, dataSource.getPoolingCount());
    Thread.sleep(1000 * 100);
    dataSource.close();
}
Also used : MockDriver(com.alibaba.druid.mock.MockDriver) Connection(java.sql.Connection) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) ObjectName(javax.management.ObjectName)

Example 37 with MockDriver

use of com.alibaba.druid.mock.MockDriver in project druid by alibaba.

the class EncodingConvertFilterTest2 method setUp.

protected void setUp() throws Exception {
    dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setFilters("encoding");
    dataSource.setDriver(new MockDriver() {

        public ResultSet createResultSet(MockPreparedStatement stmt) {
            return new MyResultSet(stmt);
        }

        public ResultSet executeQuery(MockStatementBase stmt, String sql) throws SQLException {
            return new MyResultSet(stmt);
        }
    });
    dataSource.getConnectProperties().put("clientEncoding", CLIENT_ENCODING);
    dataSource.getConnectProperties().put("serverEncoding", SERVER_ENCODING);
    dataSource.init();
}
Also used : MockDriver(com.alibaba.druid.mock.MockDriver) SQLException(java.sql.SQLException) MockResultSet(com.alibaba.druid.mock.MockResultSet) ResultSet(java.sql.ResultSet) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) MockStatementBase(com.alibaba.druid.mock.MockStatementBase)

Example 38 with MockDriver

use of com.alibaba.druid.mock.MockDriver in project druid by alibaba.

the class StatFilterBuildSlowParameterTest method setUp.

protected void setUp() throws Exception {
    dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setFilters("stat");
    dataSource.setTestOnBorrow(false);
    dataSource.setConnectionProperties("druid.stat.slowSqlMillis=1");
    MockDriver driver = new MockDriver() {

        public ResultSet executeQuery(MockStatementBase stmt, String sql) throws SQLException {
            try {
                Thread.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return super.executeQuery(stmt, sql);
        }
    };
    dataSource.setDriver(driver);
    dataSource.init();
}
Also used : MockDriver(com.alibaba.druid.mock.MockDriver) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) MockStatementBase(com.alibaba.druid.mock.MockStatementBase)

Example 39 with MockDriver

use of com.alibaba.druid.mock.MockDriver in project druid by alibaba.

the class StatFilterExecuteTest method setUp.

protected void setUp() throws Exception {
    dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setFilters("stat");
    dataSource.setTestOnBorrow(false);
    MockDriver driver = new MockDriver() {

        public MockPreparedStatement createMockPreparedStatement(MockConnection conn, String sql) {
            return new MyMockPreparedStatement(conn, sql);
        }
    };
    dataSource.setDriver(driver);
    dataSource.init();
}
Also used : MockDriver(com.alibaba.druid.mock.MockDriver) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) MockConnection(com.alibaba.druid.mock.MockConnection)

Example 40 with MockDriver

use of com.alibaba.druid.mock.MockDriver in project druid by alibaba.

the class EncodingConvertFilterTest method setUp.

protected void setUp() throws Exception {
    dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setFilters("encoding");
    dataSource.setDriver(new MockDriver() {

        public ResultSet createResultSet(MockPreparedStatement stmt) {
            return new MyResultSet(stmt);
        }

        public ResultSet executeQuery(MockStatementBase stmt, String sql) throws SQLException {
            return new MyResultSet(stmt);
        }
    });
    dataSource.getConnectProperties().put("clientEncoding", CLIENT_ENCODING);
    dataSource.getConnectProperties().put("serverEncoding", SERVER_ENCODING);
    dataSource.init();
}
Also used : MockDriver(com.alibaba.druid.mock.MockDriver) SQLException(java.sql.SQLException) MockResultSet(com.alibaba.druid.mock.MockResultSet) ResultSet(java.sql.ResultSet) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) MockStatementBase(com.alibaba.druid.mock.MockStatementBase)

Aggregations

MockDriver (com.alibaba.druid.mock.MockDriver)60 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)49 Connection (java.sql.Connection)17 SQLException (java.sql.SQLException)13 ResultSet (java.sql.ResultSet)10 Properties (java.util.Properties)9 MockConnection (com.alibaba.druid.mock.MockConnection)8 MockStatementBase (com.alibaba.druid.mock.MockStatementBase)7 MockPreparedStatement (com.alibaba.druid.mock.MockPreparedStatement)5 DataSourceProxyConfig (com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig)5 DataSourceProxyImpl (com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl)5 JdbcStatContext (com.alibaba.druid.stat.JdbcStatContext)5 Log4jFilter (com.alibaba.druid.filter.logging.Log4jFilter)4 CommonsLogFilter (com.alibaba.druid.filter.logging.CommonsLogFilter)3 MockResultSet (com.alibaba.druid.mock.MockResultSet)3 MockStatement (com.alibaba.druid.mock.MockStatement)3 Statement (java.sql.Statement)3 ObjectName (javax.management.ObjectName)3 StatFilter (com.alibaba.druid.filter.stat.StatFilter)2 DruidPooledConnection (com.alibaba.druid.pool.DruidPooledConnection)2