Skip to content

Commit

Permalink
Merge pull request #214 from datafuselabs/feat/connection-pool-example
Browse files Browse the repository at this point in the history
feat: add DatabendConnection pool factory examples
  • Loading branch information
hantmac authored May 8, 2024
2 parents f421590 + 0c7e499 commit 9b8f908
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
5 changes: 5 additions & 0 deletions databend-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@
<artifactId>fastcsv</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.11.1</version>
</dependency>

<dependency>
<groupId>org.testng</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.databend.jdbc.examples;
import com.databend.jdbc.DatabendConnection;
import com.fasterxml.jackson.databind.annotation.JsonAppend;
import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.apache.commons.pool2.impl.GenericObjectPool;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class DatabendConnectionFactory implements PooledObjectFactory<DatabendConnection> {

private String url;
private Properties properties;

public DatabendConnectionFactory(String url, Properties properties) {
this.url = url;
this.properties = properties;
}
private Connection createConnection(String url, Properties p) throws SQLException {
return DriverManager.getConnection(url, p);
}

@Override
public PooledObject<DatabendConnection> makeObject() throws Exception {
DatabendConnection connection = (DatabendConnection) createConnection(url, properties);
return new DefaultPooledObject<>(connection);
}

@Override
public void destroyObject(PooledObject<DatabendConnection> p) throws Exception {
p.getObject().close();
}

@Override
public boolean validateObject(PooledObject<DatabendConnection> p) {
try {
return !p.getObject().isClosed();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

@Override
public void activateObject(PooledObject<DatabendConnection> p) throws Exception {
}

@Override
public void passivateObject(PooledObject<DatabendConnection> p) throws Exception {
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.databend.jdbc.examples;

import com.databend.jdbc.DatabendConnection;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

import java.util.Properties;

public class DatabendConnectionPool extends GenericObjectPool<DatabendConnection> {
public DatabendConnectionPool(DatabendConnectionFactory factory, GenericObjectPoolConfig<DatabendConnection> config) {
super(factory,config);
}

public void testDemo() throws Exception {
GenericObjectPoolConfig<DatabendConnection> config = new GenericObjectPoolConfig<>();
config.setMaxTotal(10); // set max total connection
config.setMinIdle(2); // set min idle connection

Properties props = new Properties();
props.setProperty("database", "db3");
props.setProperty("SSL", "false");
// Create a Databend connection pool
DatabendConnectionFactory factory = new DatabendConnectionFactory("jdbc:databend://localhost:8000", props);
DatabendConnectionPool pool = new DatabendConnectionPool(factory,config);

// Get a connection from the pool
DatabendConnection connection = pool.borrowObject();
// connection.uploadStream();

// Use the connection
connection.createStatement().executeQuery("SELECT version()");

// Return the connection to the pool
pool.returnObject(connection);

// Close the pool when done
pool.close();
}
}


0 comments on commit 9b8f908

Please sign in to comment.