1 package com.protomatter.jdbc.pool; 2 import com.protomatter.syslog.Channel; 3 4 import com.protomatter.util.Debug; 5 6 55 56 import java.sql.*; 57 import java.text.*; 58 import java.util.*; 59 import com.protomatter.syslog.Channel; 60 import com.protomatter.util.Debug; 61 62 83 public class JdbcConnectionPoolDriver 84 implements Driver 85 { 86 private static Map pools = new HashMap(); 87 private static int MAJOR_VERSION = 1; 88 private static int MINOR_VERSION = 1; 89 90 protected static Debug DEBUG = Debug.forPackage(JdbcConnectionPoolDriver.class); 91 protected static Channel log = Channel.forPackage(JdbcConnectionPoolDriver.class); 92 93 94 97 public JdbcConnectionPoolDriver() 98 { 99 super(); 100 } 101 102 106 static 107 { 108 try 110 { 111 if (DEBUG.debug()) 112 { 113 log.debug(JdbcConnectionPoolDriver.class, "Registering JDBC driver"); 114 } 115 Driver driver = new JdbcConnectionPoolDriver(); 116 DriverManager.registerDriver(driver); 117 } 118 catch (SQLException x) 119 { 120 log.error(JdbcConnectionPoolDriver.class, "Cannot register JDBC driver", x); 121 } 122 } 123 124 125 static int countRegisteredPools() 126 { 127 return pools.size(); 128 } 129 130 131 static void registerPool(JdbcConnectionPool pool) 132 { 133 if (DEBUG.debug()) 134 log.debug(JdbcConnectionPoolDriver.class, "Registering pool: " + pool); 135 pools.put(pool.getName(), pool); 136 } 137 138 139 static void unRegisterPool(JdbcConnectionPool pool) 140 { 141 if (DEBUG.debug()) 142 log.debug(JdbcConnectionPoolDriver.class, "Un-registering pool: " + pool); 143 pools.remove(pool.getName()); 144 } 145 146 147 153 public static JdbcConnectionPool getPool(String poolName) 154 { 155 return (JdbcConnectionPool)pools.get(poolName); 156 } 157 158 159 165 public static Iterator getPoolNames() 166 { 167 return pools.keySet().iterator(); 168 } 169 170 171 178 public static void shutdownAllConnections() 179 { 180 if (DEBUG.debug()) 181 log.debug(JdbcConnectionPoolDriver.class, "Shutting down all connections"); 182 Map thePools = pools; 184 pools = new HashMap(); 185 186 Iterator i = thePools.keySet().iterator(); 187 while (i.hasNext()) 188 { 189 String poolName = (String )i.next(); 190 if (DEBUG.debug()) 191 log.debug(JdbcConnectionPoolDriver.class, "Closing connections on pool " + poolName); 192 JdbcConnectionPool pool = (JdbcConnectionPool)thePools.get(poolName); 193 pool.closeAllConnections(); 194 } 195 } 196 197 198 203 public static String URL_PREFIX = "jdbc:protomatter:pool:"; 204 205 206 216 public Connection connect(String url, Properties props) 217 throws SQLException 218 { 219 if (DEBUG.debug()) 220 log.debug(this, "connect(" + url + ", " + props + ")"); 221 222 if (!url.startsWith(URL_PREFIX)) 223 return null; 224 225 String poolName = url.substring(URL_PREFIX.length()); 226 227 JdbcConnectionPool pool = (JdbcConnectionPool)pools.get(poolName); 228 if (pool == null) 229 { 230 throw new SQLException(MessageFormat.format( 231 PoolResources.getResourceString(MessageConstants.UNKNOWN_POOL_MESSAGE), 232 new Object []{poolName})); 233 } 234 235 try 237 { 238 JdbcConnectionPoolConnection c = (JdbcConnectionPoolConnection)pool.checkout(); 239 c.setCheckoutStackTrace(new JdbcCheckoutExceptionTrace()); 240 c.resetLastTimeUsed(); 241 return new ConnectionWrapper(c, pool); 242 } 243 catch (Exception x) 244 { 245 throw new PoolSQLException(MessageFormat.format( 246 PoolResources.getResourceString(MessageConstants.CANNOT_CHECKOUT_MESSAGE), 247 new Object []{pool.getName(), x.toString()}), x); 248 } 249 } 250 251 252 258 public boolean acceptsURL(String url) 259 throws SQLException 260 { 261 return url.startsWith(URL_PREFIX); 262 } 263 264 265 272 public DriverPropertyInfo[] getPropertyInfo(String url, Properties props) 273 throws SQLException 274 { 275 return null; 276 } 277 278 279 283 public int getMajorVersion() 284 { 285 return MAJOR_VERSION; 286 } 287 288 289 293 public int getMinorVersion() 294 { 295 return MINOR_VERSION; 296 } 297 298 299 303 public boolean jdbcCompliant() 304 { 305 return false; 306 } 307 } 308 309 | Popular Tags |