Ejemplo n.º 1
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();
  }
Ejemplo n.º 2
0
  public void test_0() throws Exception {

    StringBuffer ddl = new StringBuffer();
    ddl.append("INSERT INTO t_big (");
    for (int i = 0; i < COUNT; ++i) {
      if (i != 0) {
        ddl.append(", ");
      }
      ddl.append("F" + i);
    }
    ddl.append(") VALUES (");
    for (int i = 0; i < COUNT; ++i) {
      if (i != 0) {
        ddl.append(", ");
      }
      ddl.append("?");
    }
    ddl.append(")");

    Connection conn = dataSource.getConnection();

    System.out.println(ddl.toString());

    PreparedStatement stmt = conn.prepareStatement(ddl.toString());

    for (int i = 0; i < COUNT; ++i) {
      stmt.setInt(i + 1, i);
    }
    stmt.execute();
    stmt.close();

    conn.close();
  }
Ejemplo n.º 3
0
  public void test_change_minIdle() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class);

    conn.close();

    dataSource.setMinIdle(1);
  }
Ejemplo n.º 4
0
  public void test_error_1() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class);

    conn.close();

    dataSource.setPoolPreparedStatements(false);
  }
Ejemplo n.º 5
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();
  }
Ejemplo n.º 6
0
  public void test_prepCall() 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,
              ResultSet.HOLD_CURSORS_OVER_COMMIT);
      raw = stmt.unwrap(MockPreparedStatement.class);
      stmt.close();
    }
    {
      PreparedStatement stmt =
          conn.prepareCall(
              "SELECT 1",
              ResultSet.TYPE_FORWARD_ONLY,
              ResultSet.CONCUR_READ_ONLY,
              ResultSet.HOLD_CURSORS_OVER_COMMIT);
      Assert.assertEquals(raw, stmt.unwrap(MockPreparedStatement.class));
      stmt.close();
    }

    conn.close();
  }
Ejemplo n.º 7
0
  public void test_setConnectionInitSqls() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class);

    conn.close();

    dataSource.setConnectionInitSqls(null);
  }
Ejemplo n.º 8
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();
  }
  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());
  }
Ejemplo n.º 10
0
  public void test_getValidConnectionCheckerClassName() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class);

    conn.close();

    dataSource.getValidConnectionCheckerClassName();
  }
Ejemplo n.º 11
0
  public void test_change_connectProperties_2() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class);

    conn.close();

    dataSource.setConnectionProperties("x=12;;");
  }
Ejemplo n.º 12
0
  public void test_error_6() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class);

    conn.close();

    dataSource.setMaxActive(1);
  }
  public void test_connect() throws Exception {
    {
      Connection conn = dataSource.getConnection();
      conn.close();
    }

    MySqlValidConnectionChecker checker =
        (MySqlValidConnectionChecker) dataSource.getValidConnectionChecker();
    Assert.assertFalse(checker.isUsePingMethod());

    dataSource.setConnectionProperties("druid.mysql.usePingMethod=true");

    Assert.assertTrue(checker.isUsePingMethod());

    Connection conn = dataSource.getConnection();
    conn.close();
  }
Ejemplo n.º 14
0
  public void test_stmtCache() throws Exception {
    for (int j = 0; j < 10; ++j) {
      for (int i = 0; i < 10; ++i) {
        Connection conn = dataSource.getConnection();
        String sql = "SELECT" + i;
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.execute();
        stmt.close();
        conn.close();
      }
    }

    dataSource.setPoolPreparedStatements(true);

    for (int j = 0; j < 10; ++j) {
      for (int i = 0; i < 10; ++i) {
        Connection conn = dataSource.getConnection();
        String sql = "SELECT" + i;
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.execute();
        stmt.close();
        conn.close();
      }
    }

    for (int i = 0; i < 1000 * 1; ++i) {
      Connection conn = dataSource.getConnection();
      PreparedStatement stmt = conn.prepareStatement("SELECT " + i);
      stmt.execute();
      stmt.close();
      conn.close();
    }

    Connection conn = dataSource.getConnection();
    DruidPooledConnection poolableConn = conn.unwrap(DruidPooledConnection.class);
    Assert.assertNotNull(poolableConn);

    Assert.assertEquals(
        dataSource.getMaxPoolPreparedStatementPerConnectionSize(),
        poolableConn.getConnectionHolder().getStatementPool().getMap().size());

    conn.close();

    Assert.assertEquals(0, dataSource.getActiveCount());
    Assert.assertEquals(1, dataSource.getPoolingCount());
  }
Ejemplo n.º 15
0
  public void test_error_7() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class);

    conn.close();

    dataSource.setPassword(dataSource.getPassword());
    dataSource.setPassword("xx");
  }
Ejemplo n.º 16
0
  @SuppressWarnings("deprecation")
  public void test_error_4() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class);

    conn.close();

    dataSource.setMaxIdle(1);
  }
Ejemplo n.º 17
0
  public void test_setConnectionProperties() throws Exception {
    dataSource.setConnectionProperties(null);
    dataSource.setLogWriter(null);
    dataSource.getLogWriter();

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

    conn.close();
  }
Ejemplo n.º 18
0
 public static Connection getConnection() {
   Connection conn = null;
   try {
     conn = dataSource.getConnection();
   } catch (SQLException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
   return conn;
 }
Ejemplo n.º 19
0
  private void dropTable() throws SQLException {

    Connection conn = dataSource.getConnection();

    Statement stmt = conn.createStatement();
    stmt.execute("DROP TABLE t_big");
    stmt.close();

    conn.close();
  }
  public void test_connect() throws Exception {
    String sql = "SELECT 1";
    {
      DruidPooledConnection conn = dataSource.getConnection();

      PreparedStatement pstmt = conn.prepareStatement(sql);
      pstmt.execute();
      pstmt.close();
      conn.close();
    }

    DruidPooledConnection conn = dataSource.getConnection();
    MockConnection mockConn = conn.unwrap(MockConnection.class);
    Assert.assertNotNull(mockConn);

    Statement stmt = conn.createStatement();
    stmt.addBatch(sql);

    SQLException exception = new SQLException("xx", "xxx", 28);
    mockConn.setError(exception);

    SQLException stmtErrror = null;
    try {
      stmt.executeBatch();
    } catch (SQLException ex) {
      stmtErrror = ex;
    }
    Assert.assertNotNull(stmtErrror);
    Assert.assertSame(exception, stmtErrror);

    SQLException commitError = null;
    try {
      conn.commit();
    } catch (SQLException ex) {
      commitError = ex;
    }

    Assert.assertNotNull(commitError);
    Assert.assertSame(exception, commitError.getCause());

    conn.close();
  }
Ejemplo n.º 21
0
  public void test_handleException() throws Exception {
    DruidPooledConnection conn = (DruidPooledConnection) dataSource.getConnection();
    conn.close();

    SQLException error = new SQLException();
    try {
      conn.handleException(error);
    } catch (SQLException ex) {
      Assert.assertEquals(error, ex);
    }
  }
Ejemplo n.º 22
0
  public void test_activeTrace() throws Exception {
    for (int i = 0; i < 1000 * 1000; ++i) {
      dataSource.shrink();

      Connection conn = dataSource.getConnection();
      conn.close();
      // Assert.assertEquals(1, dataSource.getPoolingCount());
      dataSource.shrink();
      Assert.assertEquals(
          "createCount : " + dataSource.getCreateCount(), 0, dataSource.getPoolingCount());
      Assert.assertEquals(0, dataSource.getActiveConnections().size());
    }
  }
Ejemplo n.º 23
0
  public void test_checkOpen_error() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class);

    conn.close();
    {
      Exception error = null;
      try {
        conn.checkState();
      } catch (SQLException ex) {
        error = ex;
      }
      Assert.assertNotNull(error);
    }
  }
Ejemplo n.º 24
0
  public void test_removeStatementEventListener() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class);

    conn.close();
    {
      Exception error = null;
      try {
        conn.removeStatementEventListener(null);
      } catch (IllegalStateException ex) {
        error = ex;
      }
      Assert.assertNotNull(error);
    }
  }
Ejemplo n.º 25
0
  public void test_setClientInfo_1() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class);
    conn.close();

    {
      SQLException error = null;
      try {
        conn.setClientInfo(new Properties());
      } catch (SQLException ex) {
        error = ex;
      }
      Assert.assertNotNull(error);
    }
  }
Ejemplo n.º 26
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());
  }
Ejemplo n.º 27
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);
    }
  }
Ejemplo n.º 28
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);
    }
  }
Ejemplo n.º 29
0
  public void test_prepareStatement_error5() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class);

    {
      SQLException error = null;
      try {
        conn.prepareStatement(null, new int[0]);
      } catch (SQLException ex) {
        error = ex;
      }
      Assert.assertNotNull(error);
    }

    conn.close();
  }
Ejemplo n.º 30
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());
  }