Search in sources :

Example 56 with MockDriver

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

the class TestIdle3_Concurrent_Starvation_Longtime method setUp.

protected void setUp() throws Exception {
    driver = new MockDriver();
    dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setDriver(driver);
    dataSource.setInitialSize(1);
    dataSource.setMaxActive(1000);
    dataSource.setMaxIdle(1000);
    dataSource.setMinIdle(1);
    // 300 / 10
    dataSource.setMinEvictableIdleTimeMillis(300 * 1000);
    // 180 / 10
    dataSource.setTimeBetweenEvictionRunsMillis(180 * 1000);
    dataSource.setTestWhileIdle(true);
    dataSource.setTestOnBorrow(false);
    dataSource.setValidationQuery("SELECT 1");
    dataSource.setFilters("stat");
    ManagementFactory.getPlatformMBeanServer().registerMBean(dataSource, new ObjectName("com.alibaba:type=DataSource"));
}
Also used : MockDriver(com.alibaba.druid.mock.MockDriver) ObjectName(javax.management.ObjectName)

Example 57 with MockDriver

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

the class TestIdle3_Concurrent method test_idle2.

public void test_idle2() throws Exception {
    MockDriver driver = new MockDriver();
    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(30 * 10);
    // 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(1, driver.getConnections().size());
        Assert.assertEquals(1, dataSource.getCreateCount());
        Assert.assertEquals(0, dataSource.getActiveCount());
    }
    {
        // 并发创建14个
        concurrent(driver, dataSource, 30);
    }
    // 连续打开关闭单个连接
    for (int i = 0; i < 1000; ++i) {
        Assert.assertEquals(0, dataSource.getActiveCount());
        Connection conn = dataSource.getConnection();
        Assert.assertEquals(1, dataSource.getActiveCount());
        conn.close();
    }
    Thread.sleep(1000);
    Assert.assertEquals(1, dataSource.getPoolingCount());
    dataSource.close();
}
Also used : MockDriver(com.alibaba.druid.mock.MockDriver) Connection(java.sql.Connection) DruidDataSource(com.alibaba.druid.pool.DruidDataSource)

Example 58 with MockDriver

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

the class TestIdleForKylin method test_idle.

public void test_idle() throws Exception {
    MockDriver driver = MockDriver.instance;
    // BasicDataSource dataSource = new BasicDataSource();
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setDriverClassName("com.alibaba.druid.mock.MockDriver");
    dataSource.setInitialSize(1);
    dataSource.setMaxActive(10);
    dataSource.setMaxIdle(10);
    dataSource.setMinIdle(0);
    dataSource.setMinEvictableIdleTimeMillis(50000 * 1);
    dataSource.setTimeBetweenEvictionRunsMillis(500);
    dataSource.setTestWhileIdle(true);
    dataSource.setTestOnBorrow(false);
    dataSource.setValidationQuery("SELECT 1");
    {
        Connection conn = dataSource.getConnection();
        // Assert.assertEquals(dataSource.getInitialSize(), driver.getConnections().size());
        System.out.println("raw size : " + driver.getConnections().size());
        PreparedStatement stmt = conn.prepareStatement("SELECT 1");
        ResultSet rs = stmt.executeQuery();
        rs.close();
        stmt.close();
        conn.close();
        System.out.println("raw size : " + driver.getConnections().size());
    }
    {
        Connection conn = dataSource.getConnection();
        // Assert.assertEquals(dataSource.getInitialSize(), driver.getConnections().size());
        System.out.println("raw size : " + driver.getConnections().size());
        conn.close();
        System.out.println("raw size : " + driver.getConnections().size());
    }
    {
        int count = 4;
        Connection[] connections = new Connection[4];
        for (int i = 0; i < count; ++i) {
            connections[i] = dataSource.getConnection();
        }
        System.out.println("raw size : " + driver.getConnections().size());
        for (int i = 0; i < count; ++i) {
            connections[i].close();
        }
        System.out.println("raw size : " + driver.getConnections().size());
        System.out.println("----------sleep for evict");
        Thread.sleep(dataSource.getMinEvictableIdleTimeMillis() * 2);
        System.out.println("raw size : " + driver.getConnections().size());
    }
    System.out.println("----------raw close all connection");
    for (MockConnection rawConn : driver.getConnections()) {
        rawConn.close();
    }
    Thread.sleep(dataSource.getMinEvictableIdleTimeMillis() * 2);
    System.out.println("raw size : " + driver.getConnections().size());
    {
        Connection conn = dataSource.getConnection();
        System.out.println("raw size : " + driver.getConnections().size());
        conn.close();
        System.out.println("raw size : " + driver.getConnections().size());
    }
    dataSource.close();
}
Also used : MockDriver(com.alibaba.druid.mock.MockDriver) Connection(java.sql.Connection) MockConnection(com.alibaba.druid.mock.MockConnection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) MockConnection(com.alibaba.druid.mock.MockConnection)

Aggregations

MockDriver (com.alibaba.druid.mock.MockDriver)58 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)48 Connection (java.sql.Connection)15 SQLException (java.sql.SQLException)11 ResultSet (java.sql.ResultSet)10 MockConnection (com.alibaba.druid.mock.MockConnection)8 MockStatementBase (com.alibaba.druid.mock.MockStatementBase)7 Properties (java.util.Properties)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 NullExceptionSorter (com.alibaba.druid.pool.vendor.NullExceptionSorter)2