Skip to content

Instantly share code, notes, and snippets.

@yssharma
Created November 3, 2012 14:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save yssharma/4007460 to your computer and use it in GitHub Desktop.
Save yssharma/4007460 to your computer and use it in GitHub Desktop.
An implementation of a Java connection pool (Confused Coders)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/** A Connection Pool with 5 Available Connections **/
class ConnectionPool {
private List<Connection>availableConnections =
new ArrayList<Connection>();
private List<Connection>usedConnections = new ArrayList<Connection>();
private final int MAX_CONNECTIONS = 5;
private String URL;
private String USERID;
private String PASSWORD;
/** Initialize all 5 Connections and put them in the Pool **/
public ConnectionPool(String Url, String UserId, String password)
throws SQLException {
this.URL = Url;
this.USERID = UserId;
this.PASSWORD = password;
for (int count = 0; count <MAX_CONNECTIONS; count++) {
availableConnections.add(this.createConnection());
}
}
/** Private function,
used by the Pool to create new connection internally **/
private Connection createConnection() throws SQLException {
return DriverManager
.getConnection(this.URL, this.USERID, this.PASSWORD);
}
/** Public function, used by us to get connection from Pool **/
public Connection getConnection() {
if (availableConnections.size() == 0) {
System.out.println("All connections are Used !!");
return null;
} else {
Connection con =
availableConnections.remove(
availableConnections.size() - 1);
usedConnections.add(con);
return con;
}
}
/** Public function, to return connection back to the Pool **/
public boolean releaseConnection(Connection con) {
if (null != con) {
usedConnections.remove(con);
availableConnections.add(con);
return true;
}
return false;
}
/** Utility function to check the number of Available Connections **/
public int getFreeConnectionCount() {
return availableConnections.size();
}
}
/** Test Class for testing Connection Pool **/
public class ConnectionPoolTest {
public static void main(String[] args) throws SQLException {
ConnectionPool pool
= new ConnectionPool("jdbc:mysql://localhost/mydb",
"testusr", "somepwd");
Connection con1 = pool.getConnection();
Connection con2 = pool.getConnection();
System.out.println(pool.getFreeConnectionCount());
Connection con3 = pool.getConnection();
Connection con4 = pool.getConnection();
Connection con5 = pool.getConnection();
Connection con6 = pool.getConnection();
System.out.println(pool.getFreeConnectionCount());
pool.releaseConnection(con1);
pool.releaseConnection(con2);
pool.releaseConnection(con4);
System.out.println(pool.getFreeConnectionCount());
}
}
@LagineniSailendra
Copy link

Below statement has a time complexity of O(n)
usedConnections.remove(con);

We can do this by O(1) by using Double linked list and hashmap.

Please note both releaseConnection() and getConnection() must be of O(1).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment