Menu

Connection pool maximum connection check

RPC
2014-03-24
2014-03-25
  • RPC

    RPC - 2014-03-24

    I have created a Connection pool with the maximum size of 2 and initialized more than 2 connections from the pool. I'm not getting any error even if it reaches beyond the maximum limit.

    After establishing every connection I have checked the available number of connections that is reducing from 2 - 1 - 0. Once it reached 0, still it is allowing to establish the connection. Even I perform some operations on the pool for every connection, I don't know how it allows to create more than 2 connections. I want to test the failing scenario for the maximum connection reached. If possible share any code same to check.

     
  • Neil Wilson

    Neil Wilson - 2014-03-24

    The maximum connection pool size specifies the maximum number of idle connections that the pool will retain at any given time. If the pool is busy, then there may be more connections in use than this maximum because, by default, the LDAPConnectionPool will automatically create a new connection if one is requested but none is available. However, there are a couple of methods that can be used to customize this behavior:

    . setMaxWaitTimeMillis -- This specifies the maximum length of time that the connection pool should wait for a connection to become available if one is needed but none are available. The default value is that it will not wait at all.

    . setCreateIfNecessary -- This indicates whether the connection pool should create a new connection if one is needed but none are available even after waiting for the maximum wait time. The default is to create a new connection, but if this is set to false, then an unsuccessful attempt to create a connection will result in an LDAPException.

    If you want the connection pool to immediately throw an exception if all connections are already in use, use:

     pool.setMaxWaitTimeMillis(0L);
     pool.setCreateIfNecessary(false);
    

    Neil

     
  • RPC

    RPC - 2014-03-25

    Thanks Neil for your prompt response.
    1. I have created the Connection pool with the Min Value of 1 and Max Value of 2
    2. As per your adv, I have assigned both setmaxWaitTimeMillis & setCreateIfNecessary
    3. Using the pool, I have created 5 connections, after creating each connection I performed some operation. But still I have not received any exception even If I try to exceed the connection pool's max limit.
    My expectation is to throw the error once the user try to get the connection beyond the maximum limit.
    Note: Even I have commented the pool health check for to test the above scenario

     
  • Neil Wilson

    Neil Wilson - 2014-03-25

    Could you provide an example that demonstrates this? I just ran a quick test and it threw an exception as I expected it would. The code I used was:

    // Create a connection to use as the basis for the pool.
    final LDAPConnection conn = new LDAPConnection("ds.example.com", 389);
    
    // Create a connection pool with a single initial connection and a
    // maximum of two connections.
    final LDAPConnectionPool pool = new LDAPConnectionPool(conn, 1, 2);
    
    // Configure the pool  to immediately throw an exception on an
    // attempt to check out more connections than that.
    pool.setMaxWaitTimeMillis(0L);
    pool.setCreateIfNecessary(false);
    
    // Check out both of the pool's allowed connections, and verify
    // that we can retrieve the server root DSE over each one.
    final LDAPConnection[] checkedOutConns = new LDAPConnection[5];
    for (int i=0; i < 2; i++)
    {
      checkedOutConns[i] = pool.getConnection();
      checkedOutConns[i].getRootDSE();
    }
    
    // Try to check out one more connection.  This should throw an
    // exception.
    final LDAPConnection extraConn = pool.getConnection();
    

    When I run this code, the last line throws an LDAPException with a result code of 91 (connect error) and a message of "No connections are currently available in the connection pool."

     

Log in to post a comment.