YugabyteDB JDBC Driver Beta

Overview

The YugabyteDB JDBC Driver is based on the open source PostgreSQL JDBC Driver (PgJDBC) and incorporates all of the functionality and behavior of that driver. The YugabyteDB JDBC driver extends PgJDBC to add support for distributed SQL databases created in YugabyteDB universes, including cluster awareness and load balancing.

Cluster awareness

The YugabyteDB JBDC driver supports distributed SQL databases on a YugabyteDB universe, or cluster, and adds cluster awareness. When you specify any one node in your YugabyteDB cluster as the initial contact point (YBClusterAwareDataSource), the driver discovers the rest of the nodes in the universe and automatically responds to nodes being started, stopped, added, or removed.

Connection pooling

Internally, the driver maintains a connection pool for each node and selects a live node to get a connection from. If a connection is available in the node’s connection pool, a connection is made and used until released back to the pool. If a connection is not available, a new connection is created.

Load balancing

When a connection is requested, the YugabyteDB JDBC driver uses a round-robin load balancing system to select a node to connect to. If that node has an available connection in the pool, a connection is opened. Upon releasing the connection, YugabyteDB returns the connection to the pool.

Resources

To get the latest source code, file issues, and track enhancements, see the Yugabyte JDBC Driver repository.

For details on functionality incorporated from the PostgreSQL JDBC driver, see Documentation (PostgreSQL JDBC Driver).

Download

Add the following lines to your Apache Maven project to access and download the YugabyteDB JDBC driver.

  1. <dependency>
  2. <groupId>com.yugabyte</groupId>
  3. <artifactId>jdbc-yugabytedb</artifactId>
  4. <version>42.2.7-yb-3</version>
  5. </dependency>

Use the driver

  • Create the data source by passing an initial contact point.
  1. String jdbcUrl = "jdbc:postgresql://127.0.0.1:5433/yugabyte";
  2. YBClusterAwareDataSource ds = new YBClusterAwareDataSource(jdbcUrl);
  • Use like a regular connection pooling data source.
  1. // Using try-with-resources to auto-close the connection when done.
  2. try (Connection connection = ds.getConnection()) {
  3. // Use the connection as usual.
  4. } catch (java.sql.SQLException e) {
  5. // Handle/Report error.
  6. }

Develop and test locally

  1. git clone https://github.com/yugabyte/jdbc-yugabytedb.git && cd jdbc-yugabytedb
  • Build and install into your local Maven directory.
  1. mvn clean install -DskipTests
  • Add the lines below to your project.
  1. <dependency>
  2. <groupId>com.yugabyte</groupId>
  3. <artifactId>jdbc-yugabytedb</artifactId>
  4. <version>42.2.7-yb-3-SNAPSHOT</version>
  5. </dependency>