OrientDB connection pooling using spring boot
Hi,
Recently I gone through some assignment where I need to use OrientDB and need to configure it with my application where I am using SpringBoot.
I decided to use connection pooling to communicate with OrientDB.
Here is the code which helps you to create connection pool.
First need to add required dependencies in pom.xml file.
<!-- Spring boot version 2.2.4.RELEASE -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.orientechnologies</groupId>
<artifactId>orientdb-client</artifactId>
<version>3.0.31</version>
</dependency>
Then need to write a class where we can configure the connection pool.
@Configuration
public class DBCofig {
private static OrientDB orientDB;
private static ODatabasePool pool;
@PostConstruct
private void init() {
createPool();
}
private static void createPool() {
orientDB = new OrientDB("remote:localhost", "root", "atul123", OrientDBConfig.defaultConfig());
OrientDBConfigBuilder poolCfg = OrientDBConfig.builder();
poolCfg.addConfig(OGlobalConfiguration.DB_POOL_MIN, 1);
poolCfg.addConfig(OGlobalConfiguration.DB_POOL_MAX, 10);
pool = new ODatabasePool(orientDB, "practicedb", "admin", "admin", poolCfg.build());
}
@PreDestroy
private void destroyPool() {
if (pool != null) {
pool.close();
}
if (orientDB != null) {
orientDB.close();
}
}
public static ODatabaseSession getConnection() {
return pool.acquire();
}
public static void closeAll(ODatabaseSession dbConn, OResultSet rs) {
System.out.println("closeAll ..............");
if (rs != null)
rs.close();
if (dbConn != null)
dbConn.close();
}
}
This will create a connection pool and using the getConnection() method.
Here we will manage to shut down the connection pool while shut down the applicaiton.
Hope this helps.
Thanks,
Atul