Create and Get Connection
This article explains how to use the Create Connection and Get Connection APIs to create connections and fetch number of busy or available connections.
Note
- These APIs only apply when
Use_Persistent_Connectionsis set toyes.
Create connection API
This API creates connections for each Key Manager defined in the current load balancer for a session. Each Key Manager, whether specified by IP address or hostname, has its own connection pool, and connections will be created within each pool.
Code snippet
NAESession session = NAESession.getSession(<username>, <password>.toCharArray());
session.createConnections(n);
Here, n is the number of connections to be created.
Example
NAESession session = NAESession.getSession(<username>, <password>.toCharArray());
session.createConnections(3);
In this example, the number of connections to be created is 3. So, if one IP/Hostname is provided in the properties file, three additional connections will be created. If two IPs/Hostnames are provided in the properties file, six additional connections will be created.
The total number of connections will always be a multiple of the value specified in the load balancer (NAE_IP.1 field) in the properties file, regardless of whether the IP address is valid or the Key Manager is down. When a Key Manager is down, all the connections will be created with the valid Key Manager.
Note
This API does not work in failover scenarios (
NAE_IP.2andNAE_IP.3).Ensure to provide a valid Key Manager IP address or hostname in the properties file. Failure to do so may result in a
Max connectionserror as all connections would be created with valid Key Manager.If the
Load_Balancing_Algorithmis set to round-robin, connections will be evenly distributed across each specified IP address or hostname.If the
Load_Balancing_Algorithmis set to random, connections will be assigned randomly to the specified IP addresses or hostnames. However, the total number of connections will be a multiple of the count specified in theNAE_IP.1field in the properties file.
Get connection API
This API fetches total number of available and busy connections in a session. If multiple Key Managers are present, it will return the total number of connections available on each Key Manager.
Available connections: Connections that are free and can be used in the next operations.
Busy connections: Connections that are busy in some operations.
Code snippet
NAESession session = NAESession.getSession(<username>, <password>.toCharArray());
ConnectionDetails connectionDetails = session.getConnectionDetails();
connectionDetails.getTotalAvailableConnections();//will return available connections
connectionDetails.getTotalBusyConnections();//will return busy connections
Calling this API will return current and updated values.
Use cases
Following are some use cases of these APIs:
Create connections during the application's initialization phase to enhance performance, as creating connections can be an expensive operation.
Keep set of available connections ready at all the times. This way, when the application experiences increased load, the connections will already be established to handle the demand.
Reference
The compiled sample is available on Github.