1 18 19 package org.apache.jmeter.protocol.jdbc.util; 20 21 import java.lang.reflect.Constructor ; 22 import java.sql.Connection ; 23 import java.sql.Driver ; 24 import java.sql.DriverManager ; 25 import java.sql.SQLException ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.Map ; 29 import java.util.NoSuchElementException ; 30 31 import org.apache.jmeter.protocol.jdbc.sampler.JDBCSampler; 32 import org.apache.jmeter.testelement.property.JMeterProperty; 33 import org.apache.jorphan.logging.LoggingManager; 34 import org.apache.log.Logger; 35 36 48 49 public final class DBConnectionManager 50 { 51 private static Logger log = LoggingManager.getLoggerForClass(); 52 53 private static DBConnectionManager manager = new DBConnectionManager(); 54 55 56 private Map poolMap = new HashMap (); 57 58 61 private DBConnectionManager() 62 { 63 } 64 65 public static DBConnectionManager getManager() 66 { 67 return manager; 68 } 69 70 83 public DBKey getKey( 84 String url, 85 String username, 86 String password, 87 String driver, 88 Map properties) 89 throws ConnectionPoolException 90 { 91 DBKey key = 92 new DBKey( 93 driver, 94 url, 95 username, 96 password); 97 98 synchronized (poolMap) 99 { 100 if (!poolMap.containsKey(key)) 101 { 102 if (registerDriver(driver)) 103 { 104 poolMap.put( 105 key, 106 createConnectionPool(key, properties)); 107 } 108 else 109 { 110 return null; 111 } 112 } 113 } 114 115 return key; 116 } 117 118 private ConnectionPool createConnectionPool(DBKey key, Map properties) 119 throws ConnectionPoolException 120 { 121 String className = 122 ((JMeterProperty) properties.get(JDBCSampler.CONNECTION_POOL_IMPL)) 123 .getStringValue(); 124 125 Class types[] = new Class [] {DBKey.class, Map .class}; 126 Object params[] = new Object [] {key, properties}; 127 128 try 129 { 130 Class poolClass = Class.forName(className); 131 Constructor constructor = poolClass.getConstructor(types); 132 return (ConnectionPool)constructor.newInstance(params); 133 } 134 catch (Exception e) 135 { 136 log.error( 137 "Error instantiating JDBC connection pool class '" 138 + className 139 + "'", 140 e); 141 throw new ConnectionPoolException( 142 "Error instantiating JDBC connection pool class '" 143 + className 144 + "': " 145 + e); 146 } 147 } 148 149 public void shutdown() 150 { 151 log.debug("Running shutdown from "+Thread.currentThread().getName()); 152 synchronized (poolMap) 153 { 154 Iterator iter = poolMap.keySet().iterator(); 155 while (iter.hasNext()) 156 { 157 DBKey key = (DBKey)iter.next(); 158 ConnectionPool pool = (ConnectionPool) poolMap.remove(key); 159 pool.close(); 160 } 161 } 162 } 163 164 private ConnectionPool getPool(DBKey key) 165 { 166 synchronized (poolMap) 167 { 168 return (ConnectionPool)poolMap.get(key); 169 } 170 } 171 172 176 public Connection getConnection(DBKey key) 177 throws NoConnectionsAvailableException 178 { 179 ConnectionPool pool = getPool(key); 180 181 if (pool == null) 182 { 183 throw new NoConnectionsAvailableException(); 184 } 185 else 186 { 187 try 188 { 189 return pool.getConnection(); 190 } 191 catch (NoSuchElementException e) 192 { 193 log.warn( 194 "NoSuchElementException getting database connection", 195 e); 196 return null; 197 } 198 catch (Exception e) 199 { 200 log.warn("Exception getting database connection from pool", e); 201 throw new NoConnectionsAvailableException(); 202 } 203 } 204 } 205 206 210 public void releaseConnection(DBKey key, Connection c) 211 { 212 getPool(key).returnConnection(c); 213 } 214 215 220 private boolean registerDriver(String driver) 221 { 222 try 223 { 224 DriverManager.registerDriver( 225 (Driver ) Class.forName(driver).newInstance()); 226 } 227 catch (SQLException e) 228 { 229 log.error("Error registering database driver '" + driver + "'", e); 230 return false; 231 } catch (InstantiationException e) { 232 log.error("Error registering database driver '" + driver + "'", e); 233 return false; 234 } catch (IllegalAccessException e) { 235 log.error("Error registering database driver '" + driver + "'", e); 236 return false; 237 } catch (ClassNotFoundException e) { 238 log.error("Error registering database driver '" + driver + "'", e); 239 return false; 240 } 241 return true; 242 } 243 } 244 | Popular Tags |