Enabling Native Connection Load Balancing in JDBC

Native connection load balancing helps spread the overhead caused by client connections on the hosts in the HP Vertica database. Both the server and the client must enable native connection load balancing in order for it to have an effect. If both have enabled it, then when the client initially connects to a host in the database, the host picks a host to handle the client connection from a list of the currently up hosts in the database, and informs the client which host it has chosen. If the initially-contacted host did not choose itself to handle the connection, the client disconnects, then opens a second connection to the host selected by the first host. The connection process to this second host proceeds as usual—if SSL is enabled, then SSL negotiations begin, otherwise the client begins the authentication process. See About Native Connection Load Balancing in the Administrator's Guide for details.

To enable native load balancing on your client, set the ConnectionLoadBalance connection parameter to true. The following example demonstrates connecting to the database several times with native connection load balancing enabled, and fetching the name of the node handling the connection from the V_MONITOR.CURRENT_SESSION system table.

import java.sql.*;
import java.util.Properties;

import com.vertica.jdbc.DataSource;

public class ConnectionLoadBalancingExample {
    public static void main(String[] args) {
        /*
         * If your client needs to run under a Java 5 JVM, It will use the older
         * JDBC 3.0-compliant driver, which requires you manually load the
         * driver using Class.forname
         */

        Properties myProp = new Properties();
        myProp.put("user", "dbadmin");
        myProp.put("password", "vertica");
        // Enable connection load balancing on this client. Only works if it is set on the
        // server as well.
        myProp.put("ConnectionLoadBalance", 1);
        Connection conn;
        // Connect 4 times. See if we connect to a different node each time.
        for (int x=1; x <= 4; x++) {
            try {
                System.out.print("Connect attempt #" + x + "...");
                conn = DriverManager.getConnection(
                        "jdbc:vertica://docd03.verticacorp.com:5433/vmart", myProp);
                Statement stmt = conn.createStatement();

                // Query system to table to see what node we are connected to. Assume a single row
                // in response set.
                ResultSet rs = stmt.executeQuery("SELECT node_name FROM v_monitor.current_session;");      
                rs.next();
                System.out.println("Connected to node " + rs.getString(1).trim());
                conn.close();
            } catch (SQLException e) {
                // Catch-all for other exceptions
                System.out.println("Error!");
                e.printStackTrace();
            }
        }
    }
}

Running the above example produces the following output:

Connect attempt #1...Connected to node v_vmart_node0001
Status of load balance policy on server: roundrobin
Connect attempt #2...Connected to node v_vmart_node0002
Connect attempt #3...Connected to node v_vmart_node0003
Connect attempt #4...Connected to node v_vmart_node0001

Was this topic helpful?

© 2006 - 2015 Hewlett-Packard Development Company, L.P.