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"));
}
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();
}
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();
}
Aggregations