/**
   * @param server
   * @param map
   * @return
   * @throws SQLException
   */
  public static DruidDataSource newDataSoruce(MergeServerConfig server, Map<String, String> map)
      throws SQLException {
    DruidDataSource druid = new DruidDataSource();

    if (logger.isInfoEnabled()) {
      logger.info("create druid datasource, param: " + map);
    }

    for (DruidConfig config : DruidConfig.values()) {
      String value = null;
      if (config.name().equals("url")) {
        value = Helper.getMySqlConURL(server.getIp(), server.getPort());
      } else if (config.name().equals("name")) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM_dd HH_mm_ss");

        value =
            String.format("%s_%s[%s]", server.getIp(), server.getPort(), sdf.format(new Date()));
      } else {
        value = map.get(config.name());
      }
      config.setValue(druid, value);
    }
    druid.init();

    return druid;
  }
  protected void setUp() throws Exception {
    dataSource = new DruidDataSource();

    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setFilters("stat");
    dataSource.setTestOnBorrow(false);
    dataSource
        .getProxyFilters()
        .add(
            new FilterAdapter() {

              @Override
              public java.io.InputStream resultSet_getAsciiStream(
                  FilterChain chain, ResultSetProxy result, int columnIndex) throws SQLException {
                return new ByteArrayInputStream(new byte[0]);
              }

              @Override
              public java.io.InputStream resultSet_getAsciiStream(
                  FilterChain chain, ResultSetProxy result, String columnLabel)
                  throws SQLException {
                return new ByteArrayInputStream(new byte[0]);
              }
            });
    dataSource.init();
  }
 protected void setUp() throws Exception {
   dataSource = new DruidDataSource();
   dataSource.setUrl("jdbc:mock:xxx");
   dataSource.setMaxActive(1);
   dataSource.setInitialSize(1);
   dataSource.setTestOnBorrow(false);
   dataSource.init();
 }
 @Override
 public DataSource getObject() throws Exception {
   if (CommonConstants.USE_TDDL) {
     return null;
   } else {
     DruidDataSource druidDataSource = new DruidDataSource();
     druidDataSource.setUrl(CommonConstants.JDBC_URL);
     druidDataSource.setUsername(CommonConstants.JDBC_USERNAME);
     druidDataSource.setPassword(CommonConstants.JDBC_PASSWORD);
     druidDataSource.init();
     return druidDataSource;
   }
 }
Ejemplo n.º 5
0
  public void test_config() throws Exception {
    Assert.assertEquals(true, dataSource.isTestOnBorrow()); // default
    Assert.assertEquals(8, dataSource.getMaxActive());

    Assert.assertEquals(1, dataSource.getProxyFilters().size());

    dataSource.init();

    Assert.assertEquals(false, dataSource.isTestOnBorrow());
    Assert.assertEquals(11, dataSource.getMaxActive());
    Assert.assertEquals("jdbc:mock:config-1", dataSource.getUrl());
    Assert.assertEquals(MockDriver.instance, dataSource.getDriver());
    Assert.assertEquals(3, dataSource.getProxyFilters().size());

    Connection conn = dataSource.getConnection();
    conn.close();
  }
 @Override
 public DataSource createAinitDataSource(Map<String, String> properties) {
   DataSource ds = null;
   try {
     ds = DruidDataSourceFactory.createDataSource(properties);
   } catch (Exception e) {
     log.error("create druid datasource error", e);
     try {
       ds = DruidDataSourceFactory.createDataSource(properties);
     } catch (Exception ex) {
       log.error("create druid datasource error", ex);
       throw new IllegalArgumentException("Cannot Create Datasource", ex);
     }
   }
   try {
     ((DruidDataSource) ds).init();
   } catch (SQLException e) {
     log.error("Init druid datasource errror", e);
     throw new IllegalArgumentException("Init Datasource Failure", e);
   }
   return ds;
 }
Ejemplo n.º 7
0
 public void init() throws SQLException {
   druidDataSource = new DruidDataSource();
   //		druidDataSource.setDriverClassName(driver);
   druidDataSource.setOracle(false);
   druidDataSource.setUrl(url);
   druidDataSource.setUsername(username);
   druidDataSource.setPassword(password);
   druidDataSource.setMaxActive(maxActive);
   druidDataSource.setMinIdle(minIdle);
   druidDataSource.setInitialSize(initialSize);
   //		 配置获取连接等待超时的时间。 单位是毫秒
   druidDataSource.setMaxWait(3000);
   //		配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
   druidDataSource.setTimeBetweenEvictionRunsMillis(60000);
   //		 配置一个连接在池中最小生存的时间,单位是毫秒
   druidDataSource.setMinEvictableIdleTimeMillis(300000);
   //		打开PSCache,并且指定每个连接上PSCache的大小
   druidDataSource.setPoolPreparedStatements(false);
   druidDataSource.setTestOnBorrow(false);
   druidDataSource.setTestWhileIdle(true);
   druidDataSource.setValidationQuery("SELECT 'x'");
   druidDataSource.init();
 }