Exemplo n.º 1
0
  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());
  }
  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());
  }