-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #214 from datafuselabs/feat/connection-pool-example
feat: add DatabendConnection pool factory examples
- Loading branch information
Showing
3 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
databend-jdbc/src/main/java/com/databend/jdbc/examples/DatabendConnectionFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} | ||
} | ||
|
41 changes: 41 additions & 0 deletions
41
databend-jdbc/src/main/java/com/databend/jdbc/examples/DatabendConnectionPool.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
||
|