With JDBC, a database is represented by a URL (Uniform Resource Locator). With LightDB, this takes one of the following forms:

The parameters have the following meanings:

To connect, you need to get a Connection instance from JDBC. To do this, you use the DriverManager.getConnection() method:

Connection db = DriverManager.getConnection(url, username, password);

## Connection Parameters

In addition to the standard connection parameters the driver supports a number of additional properties which can be used to specify additional driver behaviour specific to LightDB™. These properties may be specified in either the connection URL or an additional Properties object parameter to DriverManager.getConnection. The following examples illustrate the use of both methods to establish a SSL connection.

If a property is specified both in URL and in Properties object, the value from Properties object is ignored.

String url = "jdbc:lightdb://localhost/test";
Properties props = new Properties();
props.setProperty("user","fred");
props.setProperty("password","secret");
props.setProperty("ssl","true");
Connection conn = DriverManager.getConnection(url, props);

String url = "jdbc:lightdb://localhost/test?user=fred&password=secret&ssl=true";
Connection conn = DriverManager.getConnection(url);

## Unix sockets

Aleksander Blomskøld has forked junixsocket and added a Unix SocketFactory that works with the driver. His code can be found at https://github.com/fiken/junixsocket.

Dependencies for junixsocket are :

<dependency>
  <groupId>no.fiken.oss.junixsocket</groupId>
  <artifactId>junixsocket-common</artifactId>
  <version>1.0.2</version>
</dependency>
<dependency>
  <groupId>no.fiken.oss.junixsocket</groupId>
  <artifactId>junixsocket-native-common</artifactId>
  <version>1.0.2</version>
</dependency>

Simply add ?socketFactory=org.newsclub.net.unix.socketfactory.PostgresqlAFUNIXSocketFactory&socketFactoryArg=[path-to-the-unix-socket] to the connection URL.

For many distros the default path is /var/run/postgresql/.s.PGSQL.5432

## Connection Fail-over

To support simple connection fail-over it is possible to define multiple endpoints (host and port pairs) in the connection url separated by commas. The driver will try to once connect to each of them in order until the connection succeeds. If none succeed, a normal connection exception is thrown.

The syntax for the connection url is:

jdbc:lightdb://host1:port1,host2:port2/database

The simple connection fail-over is useful when running against a high availability postgres installation that has identical data on each node. For example streaming replication postgres or postgres-xc cluster.

For example an application can create two connection pools. One data source is for writes, another for reads. The write pool limits connections only to a primary node:

jdbc:lightdb://node1,node2,node3/accounting?targetServerType=primary.

And read pool balances connections between secondary nodes, but allows connections also to a primary if no secondaries are available:

jdbc:lightdb://node1,node2,node3/accounting?targetServerType=preferSecondary&loadBalanceHosts=true

If a secondary fails, all secondaries in the list will be tried first. In the case that there are no available secondaries the primary will be tried. If all of the servers are marked as “can’t connect” in the cache then an attempt will be made to connect to all of the hosts in the URL in order.