示例#1
0
  /** Tests that new connections are created on the fly. */
  @Test
  public void testConnectionCreate() throws InterruptedException, SQLException {
    CommonTestUtils.logTestInfo("Tests that new connections are created on the fly");
    config.setMinConnectionsPerPartition(10);
    config.setMaxConnectionsPerPartition(20);
    config.setAcquireIncrement(5);
    config.setPartitionCount(1);
    config.setReleaseHelperThreads(0);

    BoneCP dsb = new BoneCP(config);

    assertEquals(10, dsb.getTotalCreatedConnections());
    assertEquals(0, dsb.getTotalLeased());

    for (int i = 0; i < 10; i++) {
      dsb.getConnection();
    }
    assertEquals(10, dsb.getTotalLeased());

    for (int i = 0; i < 60; i++) {
      Thread.yield();
      Thread.sleep(2000); // give time for pool watch thread to fire up
      if (dsb.getTotalCreatedConnections() == 15) {
        break;
      }
    }
    assertEquals(15, dsb.getTotalCreatedConnections());
    assertEquals(10, dsb.getTotalLeased());
    assertEquals(5, dsb.getTotalFree());

    dsb.shutdown();
    CommonTestUtils.logPass();
  }
示例#2
0
  /**
   * Test case for cache put.
   *
   * @throws SQLException
   * @throws SecurityException
   * @throws NoSuchFieldException
   * @throws IllegalArgumentException
   * @throws IllegalAccessException
   */
  @Test
  public void testStatementCachePut()
      throws SQLException, SecurityException, NoSuchFieldException, IllegalArgumentException,
          IllegalAccessException {
    CommonTestUtils.logTestInfo("Tests statement close (put in cache).");
    String sql = CommonTestUtils.TEST_QUERY;
    BoneCP dsb = null;
    config.setMinConnectionsPerPartition(1);
    config.setMaxConnectionsPerPartition(5);
    config.setAcquireIncrement(1);
    config.setPartitionCount(1);
    config.setStatementsCacheSize(5);
    config.setStatementReleaseHelperThreads(0);
    config.setStatisticsEnabled(true);
    dsb = new BoneCP(config);
    Connection conn = dsb.getConnection();
    Statement statement = conn.prepareStatement(sql);
    statement.close();
    Field statementCache = conn.getClass().getDeclaredField("preparedStatementCache");
    statementCache.setAccessible(true);
    IStatementCache cache = (IStatementCache) statementCache.get(conn);

    statement = cache.get(sql);
    assertNotNull(statement);
    // Calling again should not provide the same object
    assertNull(cache.get(sql));

    // now pretend we have 1 connection being asked for the same statement
    // twice
    statement = conn.prepareStatement(sql);
    Statement statement2 = conn.prepareStatement(sql);

    statement.close(); // release it again
    statement2.close(); // release the other one

    statement2.close();
    statement.close();
    conn.close();
    dsb.shutdown();

    CommonTestUtils.logPass();
  }
示例#3
0
  @Test
  public void testGetReleaseSingleThread() throws InterruptedException, SQLException {
    CommonTestUtils.logTestInfo("Test simple get/release connection from 1 partition");

    config.setMinConnectionsPerPartition(30);
    config.setMaxConnectionsPerPartition(100);
    config.setAcquireIncrement(5);
    config.setPartitionCount(1);
    BoneCP dsb = new BoneCP(config);

    for (int i = 0; i < 60; i++) {
      Connection conn = dsb.getConnection();
      conn.close();
    }
    assertEquals(0, dsb.getTotalLeased());
    assertEquals(30, dsb.getTotalFree());

    dsb.shutdown();
    CommonTestUtils.logPass();
  }
示例#4
0
  /**
   * Test limits.
   *
   * @throws SQLException
   * @throws SecurityException
   * @throws NoSuchFieldException
   * @throws IllegalArgumentException
   * @throws IllegalAccessException
   */
  @Test
  public void testStatementCacheLimits()
      throws SQLException, SecurityException, NoSuchFieldException, IllegalArgumentException,
          IllegalAccessException {
    CommonTestUtils.logTestInfo("Tests statement caching module.");
    String sql = CommonTestUtils.TEST_QUERY;
    BoneCP dsb = null;
    config.setMinConnectionsPerPartition(2);
    config.setMaxConnectionsPerPartition(5);
    config.setAcquireIncrement(1);
    config.setPartitionCount(1);
    config.setStatementsCacheSize(5);
    dsb = new BoneCP(config);
    Connection conn = dsb.getConnection();
    StatementHandle statement = (StatementHandle) conn.prepareStatement(sql);

    StatementCache cache = new StatementCache(5, true, new Statistics(dsb));
    cache.putIfAbsent("test1", statement);
    cache.putIfAbsent("test2", statement);
    cache.putIfAbsent("test3", statement);
    cache.putIfAbsent("test4", statement);
    cache.putIfAbsent("test5", statement);

    conn.close();

    for (int i = 0; i < 5000000; i++) {
      cache.putIfAbsent("test" + i, statement);
      if ((i % 10000) == 0) {
        System.gc();
      }
      if (cache.size() != i) {
        break;
      }
    }
    // some elements should have been dropped in the cache
    assertFalse(cache.size() == 5000000);

    dsb.shutdown();
    CommonTestUtils.logPass();
  }
示例#5
0
  /**
   * Prepared statement tests.
   *
   * @throws SQLException
   * @throws SecurityException
   * @throws NoSuchFieldException
   * @throws IllegalArgumentException
   * @throws IllegalAccessException
   */
  @Test
  public void testPreparedStatement()
      throws SQLException, SecurityException, NoSuchFieldException, IllegalArgumentException,
          IllegalAccessException {
    BoneCP dsb = null;
    CommonTestUtils.logTestInfo("Tests that prepared statements are obtained from cache when set.");
    config.setMinConnectionsPerPartition(10);
    config.setMaxConnectionsPerPartition(20);
    config.setAcquireIncrement(5);
    config.setPartitionCount(1);
    config.setStatementsCacheSize(1);
    config.setLogStatementsEnabled(true);
    config.setStatementReleaseHelperThreads(0);
    dsb = new BoneCP(config);

    ConnectionHandle con = (ConnectionHandle) dsb.getConnection();
    Field preparedStatement = con.getClass().getDeclaredField("preparedStatementCache");
    preparedStatement.setAccessible(true);
    // switch to our mock
    preparedStatement.set(con, mockCache);
    expect(mockCache.get(isA(String.class))).andReturn(null);
    //		mockCache.put(isA(String.class), isA(PreparedStatement.class));

    replay(mockCache);
    Statement statement = con.prepareStatement(CommonTestUtils.TEST_QUERY);
    statement.close();
    verify(mockCache);

    reset(mockCache);
    expect(mockCache.get(isA(String.class))).andReturn(null);
    replay(mockCache);

    con.prepareStatement(CommonTestUtils.TEST_QUERY);
    statement.close();
    verify(mockCache);
    dsb.shutdown();
    statement.close();
    con.close();
    CommonTestUtils.logPass();
  }
示例#6
0
  /** Tests that new connections are created on the fly. */
  @Test
  public void testConnectionCreate() throws InterruptedException, SQLException {
    CommonTestUtils.logTestInfo("Tests that new connections are created on the fly");
    config.setMinConnectionsPerPartition(10);
    config.setMaxConnectionsPerPartition(20);
    config.setAcquireIncrement(5);
    config.setPartitionCount(1);
    config.setReleaseHelperThreads(0);
    config.setPoolAvailabilityThreshold(0);

    BoneCP dsb = new BoneCP(config);

    assertEquals(10, dsb.getTotalCreatedConnections());
    assertEquals(0, dsb.getTotalLeased());

    Connection[] con = new Connection[10];
    for (int i = 0; i < 10; i++) {
      con[i] = dsb.getConnection(); // keep track of it to avoid finalizer
    }
    assertEquals(10, dsb.getTotalLeased());

    for (int i = 0; i < 10; i++) {
      Thread.yield();
      Thread.sleep(500); // give time for pool watch thread to fire up
      if (dsb.getTotalCreatedConnections() == 15) {
        break;
      }
    }
    assertEquals(15, dsb.getTotalCreatedConnections());
    assertEquals(10, dsb.getTotalLeased());
    assertEquals(5, dsb.getTotalFree());

    for (Connection c : con) {
      c.close();
    }

    dsb.shutdown();
    CommonTestUtils.logPass();
  }
示例#7
0
 @Test
 public void testClosedConnection() throws InterruptedException, SQLException {
   BoneCP dsb = null;
   CommonTestUtils.logTestInfo(
       "Tests that closed connections trigger exceptions if use is attempted.");
   config.setMinConnectionsPerPartition(10);
   config.setMaxConnectionsPerPartition(20);
   config.setAcquireIncrement(5);
   config.setPartitionCount(1);
   try {
     dsb = new BoneCP(config);
     Connection conn = dsb.getConnection();
     conn.prepareCall(CommonTestUtils.TEST_QUERY);
     conn.close();
     try {
       conn.prepareCall(CommonTestUtils.TEST_QUERY);
       fail("Should have thrown an exception");
     } catch (SQLException e) {
       CommonTestUtils.logPass();
     }
   } finally {
     dsb.shutdown();
   }
 }