Create a database connection

2023-07-27 06:42:08  Updated

OceanBase Connector/J enables you to create a database connection by using DriverManager or a connection pool.

Create a database connection by using DriverManager

We recommend that you use the DriverManager class to connect to OceanBase Connector/J.

When you use the DriverManager class, set the parameters as follows to establish a connection to OceanBase Database:

Connection connection = DriverManager.getConnection("jdbc:oceanbase://host:port/user=root&password=***");

Before you use DriverManager, you need to use Class.forName to load the class, and then use the IP address and port number of OceanBase Database to connect to the database. In other word, enter the corresponding IP address, port number, and schema name in String url.

The following example shows complete sample code.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class HelloWorld {
   public static void main(String[] args) {
       try {
           String url = "jdbc:oceanbase://ipaddress:port/shemaname?pool=false";
           String  user = "username";
           String  password = "***";
           Class.forName("com.oceanbase.jdbc.Driver");
           Connection connection = DriverManager.getConnection(url, user, password);
       } catch (Throwable e) {
           e.printStackTrace();
       }
   }

After the database connection is established, you can perform steps in the following example to load the class.

javac -cp target/oceanbase-client-{version}.jar HelloWorld.java
java -cp .:target/oceanbase-client-{version}.jar HelloWorld

Create a database connection by using a connection pool

You can also connect to OceanBase Connector/J by using a connection pool.

OceanBase Connector/J supports two types of datasource pools:

  • OceanbaseDataSource: the basic implementation of a database connection. A connection is created each time you call the getConnection() method.

  • OceanbasePoolDataSource: a connection pool implementation. The connection pool maintains connection resources. When a connection request is received, a connection is borrowed from the connection pool.

Internal pool

The internal pool of OceanBase Connector/J provides a very fast pool implementation and solves the following problems found in the Java pool:

  • Two different connection state cleanup modes are available after a connection is released.

  • Inactive connections are automatically released. In a Java pool, connections are automatically released to avoid problems that may occur when the server closes connections after the @wait_timeout threshold is reached.

External pool

You must configure the com.oceanbase.jdbc.Driver class when you use an external connection pool.

Example: Use the HikariCP JDBC connection pool.

final HikariDataSource ds = new HikariDataSource();
ds.setMaximumPoolSize(10);
ds.setDriverClassName("com.oceanbase.jdbc.Driver");
ds.setJdbcUrl("jdbc:oceanbase://localhost:2883/db");
ds.addDataSourceProperty("user", "root");
ds.addDataSourceProperty("password", "OBClient");
ds.setAutoCommit(false);

Contact Us