3.5.2 releaseConnection()
This method is called by the query engine after a connection is no longer needed. This method should be coordinated with the getConnection() method so that if there is a getConnection() pending, this method will notify the waiting thread.
The following is a simple connection pool implementation. It creates ten connections at startup.
import java.sql.*;
import java.util.*;
import java.security.Principal;
import java.util.concurrent.*;
import inetsoft.uql.*;
import inetsoft.uql.jdbc.*;
public class TestConnectionPool implements ConnectionPool {
public TestConnectionPool() {
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
}
catch(ClassNotFoundException classx) {
}
String url = "jdbc:derby:classpath:orders";
for(int i = 0; i < SIZE; i++) {
try {
Connection connection = DriverManager.getConnection(url, "sa", "");
connections.push(connection);
}
catch(SQLException sqlx) {
}
}
}
public Connection getConnection(XDataSource xds, Principal user) {
System.out.println("getConnection("+xds+", "+user+")");
Connection conn = null;
if(xds.getName().equals("Orders")) {
try {
conn = connections.takeFirst();
}
catch (InterruptedException ie) {
}
}
return conn;
}
public void releaseConnection(XDataSource xds, Connection conn) {
System.out.println("releaseConnection("+xds+", "+conn+")");
connections.push(conn);
}
private final BlockingDeque<Connection> connections = new LinkedBlockingDeque<Connection>(SIZE);
private static final int SIZE = 10;
}
| << 3.5.1 getConnection() | © 1996-2013 InetSoft Technology Corporation (v11.5) | 3.5.3 Setting the Connection Pool >> |