UltipaDocs
Try Playground
  • Introduction
  • RESTful API
    • Installation
    • Connection
      • Request Configuration
      • Querying Methods
      • UQL Execution
      • Graphset Management
      • Schema and Property Management
      • Data Insertion and Deletion
      • Query Acceleration
      • Algorithm Management
      • Downloads and Exports
      • Process and Task Management
      • Access Management
      • Others
    • Result Processing
    • Types Mapping
    • Installation
    • Connection
      • Request Configuration
      • Querying Methods
      • UQL Execution
      • Graphset Management
      • Schema and Property Management
      • Data Insertion and Deletion
      • Query Acceleration
      • Algorithm Management
      • Downloads and Exports
      • Process and Task Management
      • Access Management
      • Others
    • Result Processing
    • Types Mapping
    • Installation
    • Connection
      • Request Configuration
      • Querying Methods
      • UQL Execution
      • Graphset Management
      • Schema and Property Management
      • Data Insertion and Deletion
      • Query Acceleration
      • Algorithm Management
      • Downloads and Exports
      • Process and Task Management
      • Access Management
      • Others
    • Result Processing
    • Types Mapping
    • Installation
    • Connection
      • Request Configuration
      • Querying Methods
      • UQL Execution
      • Graphset Management
      • Schema and Property Management
      • Data Insertion and Deletion
      • Query Acceleration
      • Algorithm Management
      • Downloads and Exports
      • Process and Task Management
      • Access Management
      • Others
    • Result Processing
    • Types Mapping
    • Installation
    • Connection
      • Request Configuration
      • Querying Methods
      • UQL Execution
      • Graphset Management
      • Schema and Property Management
      • Data Insertion and Deletion
      • Query Acceleration
      • Algorithm Management
      • Downloads and Exports
      • Process and Task Management
      • Access Management
      • Others
    • Result Processing
    • Types Mapping
  1. Docs
  2. /
  3. Ultipa Drivers
  4. /
  5. Java

Connection

After installing the Ultipa Java SDK and setting up a running Ultipa instance, you should be able to connect your application to the Ultipa graph database.

Connection to Ultipa can be established by creating a driver with configurations specified using either or both of the following methods:

  • Code Configuration Connection: through the UltipaConfiguration class
  • File Configuration Connection: through the ultipa.properties file

The values of configuration items are preferentially determined by UltipaConfiguration, followed by ultipa.properties. If an item is not found in either configuration, the default value is used.

Code Configuration Connection

Main.java
package com.ultipa.www.sdk.api;

import com.ultipa.sdk.connect.Connection;
import com.ultipa.sdk.connect.conf.UltipaConfiguration;
import com.ultipa.sdk.connect.driver.UltipaClientDriver;

public class Main {
    public static void main(String[] args) {
        UltipaConfiguration myConfig = UltipaConfiguration.config()
            // URI example: .hosts("mqj4zouys.us-east-1.cloud.ultipa.com:60010")
            .hosts("192.168.1.85:60061,192.168.1.86:60061,192.168.1.87:60061")
            .username("<username>")
            .password("<password>")
            .defaultGraph("default");

        UltipaClientDriver driver = null;
        try {
            driver = new UltipaClientDriver(myConfig);
            Connection client = driver.getConnection("amz");

            String reply = client.sayHello("Hi");
            System.out.println(reply);
        } finally {
            if (driver != null) {
                driver.close();
            }
        }
    }
}
Output
Hi Welcome To Ultipa!

A driver is created with the configurations specified using UltipaConfiguration. Please refer to Configuration Items for all items available for configuring connection details with UltipaConfiguration.

The getConnection() method obtains a connection to Ultipa, allowing you to optionally specify a graphset as the current graphset (in this case, amz). If no graph is specified, the graphset identified by the configuration item defaultGraph will be used.

File Configuration Connection

Main.java
package com.ultipa.www.sdk.api;

import com.ultipa.sdk.connect.Connection;
import com.ultipa.sdk.connect.conf.UltipaConfiguration;
import com.ultipa.sdk.connect.driver.UltipaClientDriver;

public class Main {
    public static void main(String[] args) {
        UltipaClientDriver driver = null;
        try {
            driver = new UltipaClientDriver();
            Connection client = driver.getConnection();

            String reply = client.sayHello("Hi");
            System.out.println(reply);
        } finally {
            if (driver != null) {
                driver.close();
            }
        }
    }
}

A driver is created with the configurations specified using the ultipa.properties file. The ultipa.properties file should be placed under the classpath of Java project, which is by default ./src/main/resources.

Example of the ultipa.properties file:

ultipa.properties
ultipa.hosts=192.168.1.85:60061,192.168.1.86:60061,192.168.1.87:60061
#ultipa.hosts=mqj4zouys.us-east-1.cloud.ultipa.com:60010
ultipa.username=<username>
ultipa.password=<password>
ultipa.passwordEncrypt=NOTHING
ultipa.defaultGraph=amz
# ultipa.crt=F:\\ultipa.crt
# ultipa.overrideAuthority=ultipa
# ultipa.crt=https
# ultipa.crt=
ultipa.keepAlive=180
ultipa.keepAliveWithoutCalls=true
ultipa.pool.maxIdle=50
ultipa.pool.minIdle=20
ultipa.pool.maxTotal=200
ultipa.pool.timeBetweenEvictionRunsMillis=3600000
ultipa.pool.testOnBorrow=true

Please refer to Configuration Items for all items available for configuring connection details with the ultipa.properties file.

Configuration Items

Below are all the configuration items available for UltipaConfiguration and ultipa.properties:

Items
Type
Default
Description
hostsStringDatabase host addresses or URI (excluding https:// or http://). For clusters, multiple addresses are separated by commas. Required.
usernameStringUsername of the host authentication. Required.
passwordStringPassword of the host authentication. Required.
passwordEncryptStringMD5Password encryption method of the driver. Supports MD5, LDAP and NOTHING. NOTHING is used when the content is blank.
timeoutInteger15Request timeout threshold in seconds.
connectTimeoutInteger2000Connection timeout threshold in milliseconds. By default, each host is attempted three times.
consistencyBooleanfalseWhether to use the leader node to ensure consistency read.
crtStringSets the local certificate file path. SSL will be used for connection, overrideAuthority must be appropriately configured. Alternatively, set to https for HTTPS protocol host. Non-SSL connection is used when it is ignored.
keepAliveInteger120Maximum period in seconds of inactivity before sending a keep-alive probe to the host to maintain connection responsiveness (may increase service load).
keepAliveWithoutCallsBooleanfalseActivates keep-alive mechanism even during connection inactivity.
overrideAuthorityStringOverrides hostname with ultipa to match server certificate authority.
maxRecvSizeInteger4Maximum size in megabytes when receiving data.
defaultGraphStringdefaultName of the graph in the database to use by default.
heartBeatInteger10000Heartbeat interval in milliseconds for all instances, set 0 to disable heartbeat.
poolConfigPoolConfigConfigures the connection pooling.

Connection Pooling

PoolConfig contains the configuration settings for using the Apache Commons Pool library. These settings are necessary when connecting to a database to efficiently handle and reuse connections.

Main.java
package com.ultipa.www.sdk.api;

import com.ultipa.sdk.connect.Connection;
import com.ultipa.sdk.connect.conf.UltipaConfiguration;
import com.ultipa.sdk.connect.driver.UltipaClientDriver;

public class Main {
    public static void main(String[] args) {
        UltipaConfiguration myConfig = UltipaConfiguration.config()
            // URI example: .hosts("mqj4zouys.us-east-1.cloud.ultipa.com:60010")
            .hosts("192.168.1.85:60061,192.168.1.86:60061,192.168.1.87:60061")
            .username("<username>")
            .password("<password>");

        PoolConfig poolConfig = myConfig.getPoolConfig();
        poolConfig.setMaxIdle(50);
        poolConfig.setMinIdle(2);
        poolConfig.setMaxTotal(200);

        UltipaClientDriver driver = null;
        try {
            driver = new UltipaClientDriver(myConfig);
            Connection client = driver.getConnection();

            String reply = client.sayHello("Hi");
            System.out.println(reply);
        } finally {
            if (driver != null) {
                driver.close();
            }
        }
    }
}

PoolConfig has the following fields:

Item
Type
Default
Description
maxIdleInteger8Maximum number of idle connections.
minIdleInteger0Minimum number of idle connections.
maxTotalInteger8Maximum number of total connections.
minEvictableIdleTimeMillisLong1800000Minimum idle time in milliseconds before a connection is evictable.
timeBetweenEvictionRunsMillisLong-1Interval in milliseconds between eviction tests for idle connections. Set to a negative value to disable scanning.
maxWaitMillisLong-1Maximum wait time in milliseconds for a connection. Set to a negative value to wait indefinitely.
testOnBorrowBooleanfalseWhether to test connections when borrowing.
testOnReturnBooleantrueWhether to test connections when returning.
testWhileIdleBooleantrueWhether to test connections while idle.
lifoBooleantrueWhether to use last-in-first-out (LIFO) order.
blockWhenExhaustedBooleantrueWhether to block new requests until a connection is available when connections are exhausted. If set to false, an error is thrown.
numTestsPerEvictionRunInteger3Maximum number of connections to test per eviction run.

Data Source

By setting the configurations and the target graph in a DataSource, you can easily pass all necessary connection information to the UltipaClientDriver when creating a connection.

NOTE

When UltipaClientDriver is instantiated using DataSource, the ultipa.properties file is ignored.

Main.java
package com.ultipa.www.sdk.api;

import com.ultipa.sdk.connect.Connection;
import com.ultipa.sdk.connect.conf.UltipaConfiguration;
import com.ultipa.sdk.connect.driver.UltipaClientDriver;

public class Main {
    public static void main(String[] args) {
        UltipaConfiguration myConfig = UltipaConfiguration.config()
            // URI example: .hosts("mqj4zouys.us-east-1.cloud.ultipa.com:60010")
            .hosts("192.168.1.85:60061,192.168.1.86:60061,192.168.1.87:60061")
            .username("<username>")
            .password("<password>");

        PoolConfig poolConfig = myConfig.getPoolConfig();
        poolConfig.setMaxIdle(50);
        poolConfig.setMinIdle(2);
        poolConfig.setMaxTotal(200);

        DataSource dataSource = new DataSource();
        dataSource.setUltipaConfiguration(myConfig);
        dataSource.setDefaultGraph("amz");

        UltipaClientDriver driver = null;
        try {
            driver = new UltipaClientDriver(dataSource);
            Connection client = driver.getConnection();

            String reply = client.sayHello("Hi");
            System.out.println(reply);
        } finally {
            if (driver != null) {
                driver.close();
            }
        }
    }
}

Close Connection

The driver object can be reused across multiple threads, and the connection can be properly closed by calling the driver.close() method.