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(); }
static { dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUsername("flower"); dataSource.setPassword("flower"); dataSource.setUrl("jdbc:mysql://localhost:3306/flower"); dataSource.setInitialSize(5); dataSource.setMinIdle(1); dataSource.setPoolPreparedStatements(false); }
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 { jdbcUrl = "jdbc:mysql://a.b.c.d:3306/dragoon_v25_masterdb"; user = "******"; password = "******"; driverClass = "com.mysql.jdbc.Driver"; dataSource = new DruidDataSource(); dataSource.setDriverClassName(driverClass); dataSource.setUrl(jdbcUrl); dataSource.setPoolPreparedStatements(true); dataSource.setUsername(user); dataSource.setPassword(password); createTable(); }
public void test_error_12() throws Exception { DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class); conn.close(); { Exception error = null; try { dataSource.setDriverClassName(""); } catch (Exception ex) { error = ex; } Assert.assertNotNull(error); } }
/** * 获得一个数据源 * * @param dsName 数据源名称,若null使用配置文件中的默认连接,此名称在配置文件中定义 * @param sshName 跳板机名称 * @throws ConnException */ public static synchronized DataSource getDataSource(String dsName, String sshName) throws ConnException { if (dbSetting == null) { throw new ConnException("No setting found, please init it!"); } DsSetting dsSetting = null; try { dsSetting = new DsSetting(dsName, sshName, dbSetting); } catch (SettingException e) { throw new ConnException("Init datasource setting fail!", e); } // 如果已经存在已有数据源(连接池)直接返回 DruidDataSource existedDataSource = dsMap.get(dsSetting.getName()); if (existedDataSource != null) { return existedDataSource; } // 基本连接信息 String remoteHost = dsSetting.getString(DsSetting.KEY_DS_HOST); int port = dsSetting.getInt(DsSetting.KEY_DS_PORT); String dbName = dsSetting.getString(DsSetting.KEY_DS_DB); // 验证连接信息的有效性 if (StrUtil.isBlank(remoteHost) || !SocketUtil.isValidPort(port) || StrUtil.isBlank(dbName)) { throw new ConnException( "Invalid connection info=>host:" + remoteHost + ", port:" + port + ", database:" + dbName + "】"); } DruidDataSource dds = new DruidDataSource(); if (druidSetting != null) { try { // 连接池参数注入 druidSetting.toObject(dds); } catch (SettingException e) { throw new ConnException("Read Druid setting error!", e); } } // 基本连接信息 dds.setName(dsSetting.getName()); // 数据源名称为连接名称 dds.setDriverClassName(dsSetting.getJdbcDriver()); dds.setUsername(dsSetting.getDsUser()); dds.setPassword(dsSetting.getDsPass()); if (dsSetting.isEnableSSH()) { port = SSHUtil.openAndBindPortToLocal(dsSetting.getSSHConnector(), remoteHost, port); remoteHost = SocketUtil.LOCAL_IP; } String jdbcUrl = DbUtil.buildJdbcUrl( dsSetting.getProtocol(), remoteHost, port, dbName, dsSetting.getJdbcUrlParam()); dds.setUrl(jdbcUrl); logger.info("【{}】{}@{}", dsSetting.getName(), dds.getUsername(), jdbcUrl); // 添加到数据源池中,以备下次使用 dsMap.put(dsSetting.getName(), dds); return dds; }