View Javadoc
1 package com.sun.ebxml.registry.persistence.rdb; 2 3 import java.sql.*; 4 import java.util.*; 5 6 import java.io.*; 7 8 /* 9 * $Header: /cvsroot/ebxmlrr/ebxmlrr/src/share/com/sun/ebxml/registry/persistence/rdb/ConnectionPool.java,v 1.3 2002/11/09 04:47:08 jasilva Exp $ 10 * 11 */ 12 13 public class ConnectionPool 14 { 15 16 private String name; 17 private String URL; 18 private String user; 19 private String password; 20 private int initConns; 21 private int maxConns; 22 private int timeOut; 23 private SQLException sqlException; 24 private int checkedOut; 25 private Vector freeConnections = new Vector(); 26 27 /*** 28 @param timeOut is the time in seconds after this has been elasped but the 29 connection cannot be returned, getConnection() will return SQLException 30 */ 31 public ConnectionPool(String name, String URL, String user, 32 String password, int maxConns, int initConns, int timeOut) 33 { 34 this.name = name; 35 this.URL = URL; 36 this.user = user; 37 this.password = password; 38 this.initConns = initConns; 39 this.maxConns = maxConns; 40 this.timeOut = timeOut > 0 ? timeOut : 5; 41 42 // initialise the pool 43 for (int i = 0; i < initConns; i++) 44 { 45 try { 46 Connection pc = newConnection(); 47 freeConnections.addElement(pc); 48 } 49 catch (SQLException e) { 50 sqlException = e; 51 } 52 } 53 System.err.println("Database connection pooling enabled"); 54 System.err.println(getStats()); 55 } 56 57 public Connection getConnection() throws SQLException { 58 try 59 { 60 if (maxConns < initConns || maxConns <=0) { 61 throw new SQLException ("Invalid initial or maximum size of connection pool"); 62 } 63 if (freeConnections.size()==0 && initConns != 0) { 64 // for some reasons the pool cannot be initialised 65 throw sqlException; 66 } 67 68 Connection conn = getConnection(timeOut * 1000); 69 return conn; 70 } 71 catch (SQLException e) 72 { 73 throw e; 74 } 75 } 76 77 private synchronized Connection getConnection(long timeout) 78 throws SQLException 79 { 80 // Get a pooled Connection from the cache or a new one. 81 // Wait if all are checked out and the max limit has 82 // been reached. 83 long startTime = System.currentTimeMillis(); 84 long remaining = timeout; 85 Connection conn = null; 86 while ((conn = getPooledConnection()) == null) 87 { 88 try 89 { 90 wait(remaining); 91 } 92 catch (InterruptedException e) 93 { } 94 remaining = timeout - (System.currentTimeMillis() - startTime); 95 if (remaining <= 0) 96 { 97 // Timeout has expired 98 throw new SQLException("Database connection timed-out"); 99 } 100 } 101 102 // Check if the Connection is still OK 103 if (!isConnectionOK(conn)) 104 { 105 // It was bad. Try again with the remaining timeout 106 return getConnection(remaining); 107 } 108 checkedOut++; 109 return conn; 110 } 111 112 private boolean isConnectionOK(Connection conn) 113 { 114 Statement testStmt = null; 115 try 116 { 117 if (!conn.isClosed()) 118 { 119 // Try to createStatement to see if it's really alive 120 testStmt = conn.createStatement(); 121 testStmt.close(); 122 } 123 else 124 { 125 return false; 126 } 127 } 128 catch (SQLException e) 129 { 130 if (testStmt != null) 131 { 132 try 133 { 134 testStmt.close(); 135 } 136 catch (SQLException se) 137 { } 138 } 139 return false; 140 } 141 return true; 142 } 143 144 private Connection getPooledConnection() throws SQLException 145 { 146 Connection conn = null; 147 if (freeConnections.size() > 0) 148 { 149 // Pick the first Connection in the Vector 150 // to get round-robin usage 151 conn = (Connection) freeConnections.firstElement(); 152 freeConnections.removeElementAt(0); 153 } 154 else if (checkedOut < maxConns) 155 { 156 conn = newConnection(); 157 } 158 return conn; 159 } 160 161 private Connection newConnection() throws SQLException 162 { 163 Connection conn = null; 164 if (user == null) { 165 conn = DriverManager.getConnection(URL); 166 } 167 else { 168 conn = DriverManager.getConnection(URL, user, password); 169 } 170 return conn; 171 } 172 173 public synchronized void freeConnection(Connection conn) throws SQLException 174 { 175 if (freeConnections.size()==0 && checkedOut == 0) { 176 // for some reasons the pool cannot be initialised 177 throw sqlException; 178 } 179 // Put the connection at the end of the Vector 180 freeConnections.addElement(conn); 181 checkedOut--; 182 notifyAll(); 183 } 184 185 /*** 186 Close all connections in the pool 187 */ 188 public synchronized void release() throws SQLException 189 { 190 if (freeConnections.size()==0 && checkedOut == 0) { 191 // for some reasons the pool cannot be initialised 192 throw sqlException; 193 } 194 Enumeration allConnections = freeConnections.elements(); 195 while (allConnections.hasMoreElements()) 196 { 197 Connection con = (Connection) allConnections.nextElement(); 198 con.close(); 199 } 200 freeConnections.removeAllElements(); 201 } 202 203 private String getStats() { 204 return "Total connections: " + 205 (freeConnections.size() + checkedOut) + 206 " Available: " + freeConnections.size() + 207 " Checked-out: " + checkedOut; 208 } 209 }

This page was automatically generated by Maven