Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- interface Connection {
- int read(); // reads an int from the connection
- void close(); // closes the connection
- }
- class StreamConnection implements Connection {
- private final InputStream input;
- int read(){ return input.read(); }
- void close(){ input.close(); }
- }
- class StreamConnectionPool {
- List<StreamConnection> freeConnections = openSomeConnectionsSomehow();
- StreamConnection borrowConnection(){
- if (freeConnections.isEmpty()) throw new IllegalStateException("No free connections");
- return freeConnections.remove(0);
- }
- void returnConnection(StreamConnection conn){
- freeConnections.add(conn);
- }
- }
- class ConnectionPool {
- private final StreamConnectionPool streamPool = ...;
- Connection getConnection() {
- final StreamConnection realConnection = streamPool.borrowConnection();
- return new Connection(){
- private boolean closed = false;
- int read () {
- if (closed) throw new IllegalStateException("Connection closed");
- return realConnection.read();
- }
- void close() {
- if (!closed) {
- closed = true;
- streamPool.returnConnection(realConnection);
- }
- }
- protected void finalize() throws Throwable {
- try {
- close();
- } finally {
- super.finalize();
- }
- }
- };
- }
- }
- 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();
- }
- }
Add Comment
Please, Sign In to add comment