public void test_error_1() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class);

    conn.close();

    dataSource.setPoolPreparedStatements(false);
  }
  /**
   * @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;
  }
  public void test_error_6() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class);

    conn.close();

    dataSource.setMaxActive(1);
  }
  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();
  }
 public void test_ValidConnectionChecker() throws Exception {
   dataSource.getValidConnectionCheckerClassName();
   dataSource.setValidConnectionChecker(new MySqlValidConnectionChecker());
   Assert.assertEquals(
       MySqlValidConnectionChecker.class.getName(),
       dataSource.getValidConnectionCheckerClassName());
 }
  public void test_setConnectionInitSqls() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class);

    conn.close();

    dataSource.setConnectionInitSqls(null);
  }
  public void test_stat_1() throws Exception {
    Connection conn = dataSource.getConnection();

    String sql = "select 'x'";
    PreparedStatement stmt = conn.prepareStatement("select 'x'");

    JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql);

    Assert.assertEquals(0, sqlStat.getInputStreamOpenCount());

    ResultSet rs = stmt.executeQuery();
    rs.next();
    rs.getAsciiStream("1");
    rs.getAsciiStream("2");
    rs.getAsciiStream("3");
    rs.close();
    stmt.close();

    conn.close();

    Assert.assertEquals(3, sqlStat.getInputStreamOpenCount());

    sqlStat.reset();
    Assert.assertEquals(0, sqlStat.getInputStreamOpenCount());
  }
  public void test_getValidConnectionCheckerClassName() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class);

    conn.close();

    dataSource.getValidConnectionCheckerClassName();
  }
  public void test_change_connectProperties_2() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class);

    conn.close();

    dataSource.setConnectionProperties("x=12;;");
  }
Пример #10
0
  public void test_change_minIdle() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class);

    conn.close();

    dataSource.setMinIdle(1);
  }
Пример #11
0
  public void test_o() throws Exception {
    {
      Connection[] connections = new Connection[3];
      for (int i = 0; i < connections.length; ++i) {
        connections[i] = dataSource.getConnection();
      }

      for (int i = 0; i < connections.length; ++i) {
        Statement stmt = connections[i].createStatement();
        ResultSet rs = stmt.executeQuery("select now()");
        JdbcUtils.printResultSet(rs);
        rs.close();
        stmt.close();
      }

      for (int i = 0; i < connections.length; ++i) {
        connections[i].close();
      }
    }

    Thread.sleep(1000 * 60 * 60 * 6); // 6 hours
    Connection conn = dataSource.getConnection();

    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("select now()");
    JdbcUtils.printResultSet(rs);
    rs.close();
    stmt.close();

    conn.close();
  }
Пример #12
0
  public void test_error_7() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class);

    conn.close();

    dataSource.setPassword(dataSource.getPassword());
    dataSource.setPassword("xx");
  }
Пример #13
0
  @SuppressWarnings("deprecation")
  public void test_error_4() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class);

    conn.close();

    dataSource.setMaxIdle(1);
  }
 protected void setUp() throws Exception {
   dataSource = new DruidDataSource();
   dataSource.setUrl("jdbc:mock:xxx");
   dataSource.setMaxActive(1);
   dataSource.setInitialSize(1);
   dataSource.setTestOnBorrow(false);
   dataSource.init();
 }
Пример #15
0
  public void test_setConnectionProperties() throws Exception {
    dataSource.setConnectionProperties(null);
    dataSource.setLogWriter(null);
    dataSource.getLogWriter();

    DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class);

    conn.close();
  }
  @Bean
  @Primary
  @ConfigurationProperties(prefix = "spring.druid")
  public DataSource MySqlDataSource(Properties properties) {
    DruidDataSource druidDataSource = new DruidDataSource();
    druidDataSource.configFromPropety(properties);

    return druidDataSource;
  }
Пример #17
0
 /** 关闭所有连接池 */
 public static synchronized void closeAll() {
   Collection<DruidDataSource> values = dsMap.values();
   for (DruidDataSource dds : values) {
     if (dds != null) {
       dds.close();
     }
   }
   dsMap.clear();
   SSHUtil.closeAll();
 }
Пример #18
0
  protected void setUp() throws Exception {
    originalDataSourceCount = DruidDataSourceStatManager.getInstance().getDataSourceList().size();

    driver = new MockDriver();
    dataSource = new DruidDataSource();
    dataSource.setDriver(driver);
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setFilters("stat,trace,log4j,encoding");
    dataSource.setDefaultAutoCommit(false);
  }
Пример #19
0
  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();
  }
  protected void setUp() throws Exception {
    Assert.assertEquals(0, JdbcStatManager.getInstance().getSqlList().size());

    dataSource = new DruidDataSource();

    dataSource.setExceptionSorter(new OracleExceptionSorter());

    dataSource.setDriver(new OracleMockDriver());
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setPoolPreparedStatements(true);
    dataSource.setMaxOpenPreparedStatements(100);
  }
 @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;
   }
 }
Пример #22
0
  public void test_error_validateConnection_3() throws Exception {
    dataSource.setValidationQuery(null);
    dataSource.setValidConnectionChecker(new MySqlValidConnectionChecker());

    {
      Exception error = null;
      try {
        DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class);
        dataSource.validateConnection(conn);
      } catch (SQLException ex) {
        error = ex;
      }
      Assert.assertNotNull(error);
    }
  }
Пример #23
0
  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();
  }
Пример #24
0
  public void test_error_validateConnection_2() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class);

    conn.getConnection().close();

    {
      Exception error = null;
      try {
        dataSource.validateConnection(conn);
      } catch (SQLException ex) {
        error = ex;
      }
      Assert.assertNotNull(error);
    }
  }
Пример #25
0
  public void test_error_10() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class);

    conn.close();

    {
      Exception error = null;
      try {
        dataSource.addConnectionProperty("x", "11");
      } catch (Exception ex) {
        error = ex;
      }
      Assert.assertNotNull(error);
    }
  }
Пример #26
0
  public void test_prepareStatement_error() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class);

    conn.close();

    {
      Exception error = null;
      try {
        dataSource.setUsername("xxx");
      } catch (Exception ex) {
        error = ex;
      }
      Assert.assertNotNull(error);
    }
  }
Пример #27
0
  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);
    }
  }
Пример #28
0
  public void test_basic() throws Exception {
    DruidPooledConnection conn = (DruidPooledConnection) dataSource.getConnection();
    conn.close();

    Assert.assertEquals(true, dataSource.isResetStatEnable());
    dataSource.setResetStatEnable(false);
    Assert.assertEquals(false, dataSource.isResetStatEnable());
    Assert.assertEquals(1, dataSource.getConnectCount());
    dataSource.resetStat();
    Assert.assertEquals(1, dataSource.getConnectCount());

    dataSource.setResetStatEnable(true);
    dataSource.resetStat();
    Assert.assertEquals(0, dataSource.getConnectCount());
  }
Пример #29
0
  public void test_merge() throws Exception {
    for (int i = 1000; i < 2000; ++i) {
      String tableName = "t" + i;
      String sql = "select * from " + tableName + " where " + tableName + ".id = " + i;
      Connection conn = dataSource.getConnection();

      Statement stmt = conn.createStatement();
      stmt.execute(sql);
      stmt.close();

      conn.close();
    }

    Assert.assertEquals(1, dataSource.getDataSourceStat().getSqlStatMap().size());
  }
Пример #30
0
  public void test_prepCall_1() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class);

    MockPreparedStatement raw = null;
    {
      PreparedStatement stmt =
          conn.prepareCall("SELECT 1", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
      raw = stmt.unwrap(MockPreparedStatement.class);
      stmt.close();
    }
    {
      PreparedStatement stmt =
          conn.prepareCall("SELECT 1", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
      Assert.assertEquals(raw, stmt.unwrap(MockPreparedStatement.class));
      stmt.close();
    }

    conn.getConnectionHolder().toString();
    conn.getConnectionHolder().setLastActiveTimeMillis(0);
    conn.getConnectionHolder().toString();
    conn.getConnectionHolder().getUseCount();
    conn.getConnectionHolder().getTimeMillis();

    conn.close();
  }