Forum: Building VoltDB Applications

Post: Dose VoltDB have a connection pool?

Dose VoltDB have a connection pool?
webbean
Oct 14, 2013
Does VoltDB have a connection pool like other database libraries. If there is none,
how should we connect to a database?

1) We should create a Client instance and create a connection for each invocation. Or
2) We should create a Client instance and create a connection for all invocations.

Which way is correct in VoltDB?
gharabed
Oct 24, 2013
That's a good question...I'd like to hear an answer to that as well. I'm actually talking with VoltDB reps and engineers today. I'll be sure to ask them about this. My gut reaction is (since this is the way I have always done things in the past) is to create a db class that connects when the class is instantiated. Then it's up to you if you want to keep that object (and thus connection) around...or I guess you could create your own connection class that handles pooling.
bballard
Oct 25, 2013
If you are using the VoltDB java client library, you do not necessarily need to use connection pooling. A "Client" object (org.voltdb.client.Client) is thread-safe and can be shared by all of the threads of a java application within the same JVM. There is an example of this in the "voter" example app (see examples/voter/src/voter/SyncBenchmark.java).

There are several advantages to the native java client library over the JDBC driver. It supports both synchronous and asynchronous database calls, and allows you to switch between these naturally within the same application. It also can enable you to offload more processing to the database because if you need to retrieve data from multiple tables the native driver can return an array of VoltTable objects, each representing the results from a different table. To get the same results with JDBC would require multiple round-trips.

If you prefer to use the JDBC driver, it is a wrapper around our java client library that implements the relevant portions of the JDBC interface, so it is also thread-safe so one Connection (java.sql.Connection) object can be shared by all the threads on a JVM. There is also an example of this in the "voter" example app (see examples/voter/src/voter/JDBCBenchmark.java). One difference with typical JDBC drivers is that when the connection is established, the JDBC url can contain a comma-separated list of servers (see https://github.com/VoltDB/voltdb/blob/master/examples/voter/src/voter/JDBCBenchmark.java#L191).