1 19 20 package com.sslexplorer.jdbc; 21 22 import java.sql.Connection ; 23 import java.sql.DriverManager ; 24 import java.sql.PreparedStatement ; 25 import java.sql.ResultSet ; 26 import java.sql.SQLException ; 27 import java.sql.Statement ; 28 import java.util.Hashtable ; 29 import java.util.Iterator ; 30 import java.util.Map ; 31 import java.util.Vector ; 32 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 36 public class JDBCConnectionImpl { 37 38 final static Log log = LogFactory.getLog(JDBCConnectionImpl.class); 39 40 static long currentId = 0; 41 42 private String key; 44 private long id; 45 private Connection conn; 46 private Hashtable preparedStatements = new Hashtable (); 47 48 private JDBCConnectionImpl(String key, JDBCConnectionSettings settings) throws SQLException , 50 ClassNotFoundException { 51 id = currentId++; 52 this.key = key; 53 Class.forName(settings.driver); 54 conn = DriverManager.getConnection(settings.url, settings.username, settings.password); 55 } 56 57 public void setAutoCommit(boolean autoCommit) throws SQLException { 58 conn.setAutoCommit(autoCommit); 59 } 60 61 String getKey() { 62 return key; 63 } 64 65 boolean isClosed() throws SQLException { 66 return conn.isClosed(); 67 } 68 69 ResultSet executeQuery(String sqlString) throws SQLException { 70 Statement stmt = conn.createStatement(); 71 ResultSet rs = stmt.executeQuery(sqlString); 72 return rs; 73 } 74 75 void execute(String sqlString) throws SQLException { 76 Statement stmt = conn.createStatement(); 77 try { 78 stmt.execute(sqlString); 79 } 80 finally { 81 stmt.close(); 82 } 83 } 84 85 synchronized PreparedStatement aquirePreparedStatement(String key, String sqlString) throws SQLException { 86 Vector avail = (Vector ) preparedStatements.get(key); 87 if (avail == null) { 88 avail = new Vector (); 89 preparedStatements.put(key, avail); 90 } 91 92 if (avail.size() > 0) { 93 return (PreparedStatement ) avail.remove(0); 94 } else 95 return conn.prepareStatement(sqlString); 96 } 97 98 synchronized void releasePreparedStatement(String key, PreparedStatement ps) throws SQLException { 99 ps.clearParameters(); 100 Vector avail = (Vector ) preparedStatements.get(key); 101 avail.add(ps); 102 } 103 104 public long getId() { 105 return id; 106 } 107 108 public Connection getConnection() { 109 return conn; 110 } 111 112 public static class JDBCPool { 113 private Hashtable poolDictionary = new Hashtable (); 115 private Hashtable poolSettings = new Hashtable (); 117 118 private JDBCPool() { 120 } 122 private static JDBCPool _instance; 124 public static JDBCPool getInstance() { 126 if (_instance == null) { 127 synchronized (JDBCPool.class) { 128 if (_instance == null) { 129 _instance = new JDBCPool(); 130 } 131 } 132 } 133 return _instance; 134 } 135 136 public synchronized void createImpl(String key, String driver, String url, String username, String password) 137 throws SQLException , ClassNotFoundException { 138 139 Vector pool = new Vector (); 140 141 JDBCConnectionSettings settings = new JDBCConnectionSettings(); 142 settings.driver = driver; 143 settings.url = url; 144 settings.username = username; 145 settings.password = password; 146 147 poolDictionary.put(key, pool); 148 poolSettings.put(key, settings); 149 150 pool.addElement(new JDBCConnectionImpl(key, settings)); 151 152 } 153 154 public Map getPools() { 155 return poolDictionary; 156 } 157 158 public synchronized void closeAll() { 159 for (Iterator i = poolDictionary.entrySet().iterator(); i.hasNext();) { 160 Map.Entry entry = (Map.Entry ) i.next(); 161 if (log.isInfoEnabled()) 162 log.info("Closing connections for " + entry.getKey()); 163 Vector pool = (Vector ) entry.getValue(); 164 for (Iterator j = pool.iterator(); j.hasNext();) { 165 JDBCConnectionImpl conx = (JDBCConnectionImpl) j.next(); 166 try { 167 conx.conn.close(); 168 } catch (SQLException sqle) { 169 log.warn("Failed to close pooled connection. ", sqle); 170 } 171 } 172 } 173 poolDictionary.clear(); 174 175 } 176 177 public synchronized JDBCConnectionImpl acquireImpl(String key) throws SQLException , ClassNotFoundException { 179 if (log.isDebugEnabled()) 180 log.debug("Aquiring connection for " + key); 181 182 Vector pool = (Vector ) poolDictionary.get(key); 184 if (pool != null) { 185 while (pool.size() > 0) { 186 if (log.isDebugEnabled()) 187 log.debug(pool.size() + " connections in pool."); 188 JDBCConnectionImpl impl = null; 189 impl = (JDBCConnectionImpl) pool.elementAt(pool.size() - 1); 191 pool.removeElementAt(pool.size() - 1); 193 194 if (impl.isClosed()) { 197 if (log.isDebugEnabled()) 198 log.debug("Connecting for " + key + " is closed, get the next one"); 199 continue; 200 } 201 202 if (log.isDebugEnabled()) 203 log.debug("Found connection " + impl.toString()); 204 205 return impl; 206 } 207 } 208 if (log.isDebugEnabled()) 209 log.debug("Pool is empty, getting "); 210 211 JDBCConnectionSettings settings = (JDBCConnectionSettings) poolSettings.get(key); 212 return new JDBCConnectionImpl(key, settings); 214 } 215 216 public synchronized void releaseImpl(JDBCConnectionImpl impl) { 218 String key = impl.getKey(); 219 Vector pool = (Vector ) poolDictionary.get(key); 220 if (pool == null) { 221 pool = new Vector (); 222 poolDictionary.put(key, pool); 223 } 224 225 pool.addElement(impl); 226 } 227 } 228 229 static class JDBCConnectionSettings { 230 String url; 231 String driver; 232 String cls; 233 String username; 234 String password; 235 } 236 237 public void rollback() throws SQLException { 238 conn.rollback(); 239 } 240 241 public void commit() throws SQLException { 242 conn.commit(); 243 } 244 } 245 | Popular Tags |