1 34 package net.myvietnam.mvncore.db; 35 36 import java.sql.*; 37 import java.util.Enumeration ; 38 import java.util.Vector ; 39 40 import net.myvietnam.mvncore.MVNCoreConfig; 41 import org.apache.commons.logging.Log; 42 import org.apache.commons.logging.LogFactory; 43 44 52 class DBConnectionManager { 53 54 private static Log log = LogFactory.getLog(DBConnectionManager.class); 55 56 private static final int TIME_BETWEEN_RETRIES = 500; 58 static private DBConnectionManager instance = null; 61 private DBConnectionPool pool = null; 64 69 private DBConnectionManager() { 70 String driverClassName = MVNCoreConfig.getDriverClassName(); 71 try { 72 Class.forName(driverClassName).newInstance(); 73 } catch (Exception e) { 74 log.fatal("DBConnectionManager: Unable to load driver = " + driverClassName, e); 75 } 76 77 String url = MVNCoreConfig.getDatabaseURL(); 78 String user = MVNCoreConfig.getDatabaseUser(); 79 String password = MVNCoreConfig.getDatabasePassword(); 80 int maxConnection = MVNCoreConfig.getMaxConnection(); 81 82 pool = new DBConnectionPool(url, user, password, maxConnection); 84 } 85 86 92 100 101 107 117 118 public static synchronized DBConnectionManager getInstance(boolean useConfig) { 119 if (instance == null) { 120 instance = new DBConnectionManager(); 121 } 122 return instance; 123 } 124 125 130 void freeConnection(Connection con) { 131 pool.freeConnection(con); 132 } 133 134 141 Connection getConnection() { 142 return getConnection(0); 143 } 144 145 154 Connection getConnection(long time) { 155 Connection connection = pool.getConnection(time); 156 if (connection == null) { 157 return null; 158 } 159 160 try { 161 connection.setAutoCommit(true); 163 } catch (SQLException e) { 164 log.error("Cannot setAutoCommit", e); 165 } 166 return connection; 167 } 168 169 174 boolean release() { 175 return pool.release(); 176 } 177 178 184 class DBConnectionPool { 185 private int checkedOut = 0; private Vector freeConnections = new Vector (); 187 188 private int maxConn = 0; 189 private String password = null; 190 private String URL = null; 191 private String user = null; 192 193 202 public DBConnectionPool(String URL, String user, String password, int maxConn) { 203 this.URL = URL; 204 this.user = user; 205 this.password = password; 206 this.maxConn = maxConn; 207 } 208 209 217 synchronized void freeConnection(Connection con) { 218 if (con != null) { if (checkedOut <= 0) { 221 try { 227 log.debug("DBConnectionManager: about to close the orphan connection."); 228 con.close(); 229 } catch (SQLException ex) { } 230 } else { 231 freeConnections.addElement(con); 235 checkedOut--; notifyAll(); } 240 } 241 } 242 243 250 synchronized Connection getConnection() { 251 Connection con = null; 252 253 while ( (freeConnections.size() > 0) && (con == null) ) { 254 con = (Connection) freeConnections.firstElement(); 257 freeConnections.removeElementAt(0); 258 try { 259 if (con.isClosed()) { 260 log.info("Removed bad connection in DBConnectionPool."); 261 con = null; } 263 } catch (SQLException e) { 264 con = null; } 266 } 268 if (con == null) { if (maxConn == 0 || checkedOut < maxConn) { con = newConnection(); 271 } 272 } 273 if (con != null) { 274 checkedOut++; 275 } 276 return con; 277 } 278 279 292 297 Connection getConnection(long timeout) { 298 long startTime = System.currentTimeMillis(); 299 Connection con; 300 while ((con = getConnection()) == null) { 301 long elapsedTime = System.currentTimeMillis() - startTime; 302 if (elapsedTime >= timeout) { 303 return null; 305 } 306 307 long timeToWait = timeout - elapsedTime; 308 if (timeToWait > TIME_BETWEEN_RETRIES) timeToWait = TIME_BETWEEN_RETRIES; try { 310 Thread.sleep(timeToWait); 311 } catch (InterruptedException e) {} 312 } 313 return con; 314 } 315 316 321 synchronized boolean release() { 322 boolean retValue = true; 323 Enumeration allConnections = freeConnections.elements(); 324 while (allConnections.hasMoreElements()) { 325 Connection con = (Connection) allConnections.nextElement(); 326 try { 327 con.close(); 328 } catch (SQLException e) { 329 log.error("Cannot close connection in DBConnectionPool."); 330 } 331 } 332 freeConnections.removeAllElements(); 333 if (checkedOut != 0) { 334 retValue = false; 335 log.warn("DBConnectionManager: the built-in connection pool is not balanced."); 336 } 337 checkedOut = 0; 338 return retValue; 339 } 340 341 346 private Connection newConnection() { 347 Connection con = null; 348 try { 349 if (user == null) { 350 con = DriverManager.getConnection(URL); 351 } else { 352 con = DriverManager.getConnection(URL, user, password); 353 } 354 } catch (SQLException e) { 358 log.error("Cannot create a new connection in DBConnectionPool. URL = " + URL, e); 359 return null; 360 } 361 return con; 362 } 363 } 364 } 365 | Popular Tags |