public void druid() throws Exception { DruidDataSource dataSource = new DruidDataSource(); dataSource.setInitialSize(initialSize); dataSource.setMaxActive(maxActive); dataSource.setMaxIdle(maxIdle); dataSource.setMinIdle(minIdle); dataSource.setMaxWait(maxWait); dataSource.setPoolPreparedStatements(true); dataSource.setDriverClassName(driverClass); dataSource.setUrl(jdbcUrl); dataSource.setPoolPreparedStatements(true); dataSource.setUsername(user); dataSource.setPassword(password); dataSource.setValidationQuery(validationQuery); dataSource.setTestOnBorrow(testOnBorrow); dataSource.setTestOnBorrow(testWhileIdle); dataSource.setTestOnBorrow(testOnReturn); dataSource.setRemoveAbandoned(removeAbandoned); dataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout); dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); dataSource.setNumTestsPerEvictionRun(numTestsPerEvictionRun); for (int i = 0; i < TEST_COUNT; ++i) { p0(dataSource, "druid", threadCount); } System.out.println(); }
public void test_f() throws Exception { final DruidDataSource dataSource = new DruidDataSource(); dataSource.setTimeBetweenConnectErrorMillis(100); final long startTime = System.currentTimeMillis(); final long okTime = startTime + 1000 * 1; dataSource.setDriver( new MockDriver() { @Override public Connection connect(String url, Properties info) throws SQLException { if (System.currentTimeMillis() < okTime) { throw new SQLException(); } return super.connect(url, info); } }); dataSource.setUrl("jdbc:mock:"); dataSource.setMinIdle(0); dataSource.setMaxActive(2); dataSource.setMaxIdle(2); Connection conn = dataSource.getConnection(); conn.close(); dataSource.close(); }
@SuppressWarnings("deprecation") public void test_error_4() throws Exception { DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class); conn.close(); dataSource.setMaxIdle(1); }
public void test_concurrent_2() throws Exception { final DruidDataSource dataSource = new DruidDataSource(); Class.forName("com.alibaba.druid.mock.MockDriver"); dataSource.setInitialSize(10); dataSource.setMaxActive(maxPoolSize); dataSource.setMinIdle(minPoolSize); dataSource.setMaxIdle(maxPoolSize); dataSource.setPoolPreparedStatements(true); dataSource.setDriverClassName(driverClass); dataSource.setUrl(jdbcUrl); dataSource.setPoolPreparedStatements(true); final int THREAD_COUNT = 2; final int LOOP_COUNT = 1000 * 1000; final CountDownLatch startLatch = new CountDownLatch(1); final CountDownLatch endLatch = new CountDownLatch(THREAD_COUNT); for (int threadIndex = 0; threadIndex < THREAD_COUNT; ++threadIndex) { Thread thread = new Thread() { public void run() { try { startLatch.await(); for (int i = 0; i < LOOP_COUNT; ++i) { Connection conn = dataSource.getConnection(); conn.close(); } } catch (Throwable e) { e.printStackTrace(); } finally { endLatch.countDown(); } } }; thread.start(); } startLatch.countDown(); endLatch.await(); Assert.assertEquals(THREAD_COUNT * LOOP_COUNT, dataSource.getConnectCount()); Assert.assertEquals(THREAD_COUNT * LOOP_COUNT, dataSource.getCloseCount()); Assert.assertEquals(0, dataSource.getConnectErrorCount()); Assert.assertEquals(0, dataSource.getActiveCount()); }
protected void setUp() throws Exception { DruidDataSourceStatManager.clear(); driver = new MockDriver(); dataSource = new DruidDataSource(); dataSource.setUrl("jdbc:mock:xxx"); dataSource.setDriver(driver); dataSource.setInitialSize(1); dataSource.setMaxActive(2); dataSource.setMaxIdle(2); dataSource.setMinIdle(1); dataSource.setMinEvictableIdleTimeMillis(300 * 1000); // 300 / 10 dataSource.setTimeBetweenEvictionRunsMillis(10); // 180 / 10 dataSource.setTestWhileIdle(true); dataSource.setTestOnBorrow(false); dataSource.setValidationQuery("SELECT 1"); dataSource.setFilters("stat"); dataSource.setPoolPreparedStatements(false); dataSource.setMaxPoolPreparedStatementPerConnectionSize(20); // ((StatFilter) dataSource.getProxyFilters().get(0)).setMaxSqlStatCount(100); }
protected void setUp() throws Exception { DruidDataSourceStatManager.clear(); driver = new MockDriver(); dataSource = new DruidDataSource(); dataSource.setUrl("jdbc:mock:xxx"); dataSource.setDriver(driver); dataSource.setInitialSize(1); dataSource.setMaxActive(2); dataSource.setMaxIdle(2); dataSource.setMinIdle(1); dataSource.setMinEvictableIdleTimeMillis(300 * 1000); // 300 / 10 dataSource.setTimeBetweenEvictionRunsMillis(180 * 1000); // 180 / 10 dataSource.setTestWhileIdle(true); dataSource.setTestOnBorrow(false); dataSource.setValidationQuery("SELECT 1"); dataSource.setFilters("stat,trace"); JdbcStatContext context = new JdbcStatContext(); context.setTraceEnable(true); JdbcStatManager.getInstance().setStatContext(context); }