1 19 package org.openharmonise.commons.dsi; 20 21 22 import java.sql.*; 23 import java.util.*; 24 25 26 35 public class DBConnectionPooler { 36 37 40 private static Map m_DBCPinstance_map = Collections.synchronizedMap(new HashMap(50)); 41 42 45 private DBConnectionPool m_broker = null; 46 47 56 private DBConnectionPooler(String driver, String dsn, String usr, String pwd) throws DataStoreException { 57 try { 58 m_broker = new DBConnectionPool(driver, 59 dsn, 60 usr, 61 pwd); 62 } catch (Exception e) { 63 throw new DataStoreException("Fatal Error in Connection Pool,",e); 64 } 65 } 66 67 72 public int getNumConnections() { 73 return m_broker.countObjects(); 74 } 75 76 82 public Connection getConnection() throws SQLException { 83 return m_broker.borrowConnection(); 84 } 85 86 91 public void freeConnection(Connection conn) { 92 m_broker.returnConnection(conn); 93 } 94 95 107 synchronized public static DBConnectionPooler getInstance(String driver, String dsn, String usr, 108 String pwd) 109 throws DataStoreException { 110 111 StringBuffer sbuf = new StringBuffer (); 112 113 sbuf.append(driver).append(":").append(dsn).append(":").append(usr).append(":").append(pwd); 114 115 String sMapKey = sbuf.toString(); 116 117 DBConnectionPooler DBCPinstance = (DBConnectionPooler)m_DBCPinstance_map.get(sMapKey); 118 119 if (DBCPinstance == null) { 120 DBCPinstance = new DBConnectionPooler(driver,dsn,usr,pwd); 121 122 m_DBCPinstance_map.put(sMapKey,DBCPinstance); 123 } 124 125 return DBCPinstance; 126 } 127 } | Popular Tags |