OceanBase JDBC enables you to create a database connection by using DriverManager or a connection pool.
DriverManager
We recommend that you use the DriverManager class to connect to OceanBase so that the OceanBase Client can be automatically loaded without configuration. Example:
Connection connection = DriverManager.getConnection("jdbc:oceanbase://lt:2883/user=root&password=***");
Connection pool
You can also connect to OceanBase by using a connection pool.
OceanBase Client supports two types of datasource pool:
OceanbaseDataSource: the basic implementation of a database connection. A connection is created every time you call thegetConnection()method.OceanbasePoolDataSource: the connection pool that accelerates database connections. 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 a JDBC driver provides a very fast pool implementation and solves the following problems found in the Java pool:
Two different connection states cleaning after release.
Automatic release of inactive connections. In a Java pool, connections are automatically released to avoid problems that may occur when the server closes connections after the
@wait_timeoutthreshold is reached. However, this is not always desired.
External pool
You must configure the com``.oceanbase.jdbc.Driver class when you use an external connection pool.
Example: Using 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);