1 package com.protomatter.jdbc.pool; 2 3 52 53 import com.protomatter.util.*; 54 import com.protomatter.pool.*; 55 import com.protomatter.syslog.Syslog; 56 import java.util.*; 57 import java.io.*; 58 import java.sql.*; 59 import java.text.MessageFormat ; 60 import javax.sql.*; 61 62 72 public class JdbcConnectionPoolDataSource 73 implements DataSource, ConnectionPoolDataSource 74 { 75 private String poolName = null; 76 private PrintWriter logWriter = null; 77 private int loginTimeout = 0; 78 private JdbcConnectionPool pool = null; 79 80 87 public JdbcConnectionPoolDataSource(String poolName) 88 throws SQLException 89 { 90 pool = JdbcConnectionPoolDriver.getPool(poolName); 91 if (pool == null) 92 throw new SQLException(MessageFormat.format( 93 PoolResources.getResourceString(MessageConstants.UNKNOWN_POOL_MESSAGE), 94 new Object [] { poolName })); 95 this.poolName = poolName; 96 } 97 98 106 public Connection getConnection() 107 throws SQLException 108 { 109 Connection c = null; 110 111 if (loginTimeout > 0) 113 { 114 GetConnectionSlaveThread t = new GetConnectionSlaveThread(pool); 117 t.start(); 118 119 int timeout = loginTimeout; 121 long end = System.currentTimeMillis(); 122 end += (long)((long)loginTimeout * (long)1000); 123 124 try { Thread.sleep(1); } catch (InterruptedException x) { ; } 127 128 while ((t.getConnection() == null) && (t.isAlive()) && (System.currentTimeMillis() < end)) 131 { 132 try { Thread.sleep(10); } catch (InterruptedException x) { ; } 133 Thread.yield(); } 135 136 pool.removeWaiter(t); 139 140 c = t.getConnection(); 142 if (c == null) { 144 if (t.isAlive()) t.interrupt(); 147 if (t.getException() != null) throw t.getException(); 149 else throw new SQLException(MessageFormat.format( 151 PoolResources.getResourceString(MessageConstants.CANNOT_GET_CONNECTION_TIMEOUT_MESSAGE), 152 new Object [] { String.valueOf(timeout) })); 153 } 154 } 155 else 156 { 157 c = DriverManager.getConnection(JdbcConnectionPoolDriver.URL_PREFIX + poolName); 159 } 160 161 return c; 163 } 164 165 175 public Connection getConnection(String user, String password) 176 throws SQLException 177 { 178 return getConnection(); 179 } 180 181 186 public PrintWriter getLogWriter() 187 throws SQLException 188 { 189 return this.logWriter; 190 } 191 192 197 public void setLogWriter(PrintWriter logWriter) 198 throws SQLException 199 { 200 this.logWriter = logWriter; 201 } 202 203 208 public int getLoginTimeout() 209 throws SQLException 210 { 211 return this.loginTimeout; 212 } 213 214 224 public void setLoginTimeout(int loginTimeout) 225 throws SQLException 226 { 227 this.loginTimeout = loginTimeout; 228 } 229 230 240 public PooledConnection getPooledConnection(String user, String password) 241 throws SQLException 242 { 243 return getPooledConnection(); 244 } 245 246 255 public PooledConnection getPooledConnection() 256 throws SQLException 257 { 258 return new JdbcConnectionPoolPooledConnection((JdbcConnectionPoolConnection)getConnection()); 259 } 260 261 264 private class GetConnectionSlaveThread 265 extends Thread 266 { 267 private JdbcConnectionPool pool = null; 268 private Connection connection = null; 269 private SQLException exception = null; 270 271 public GetConnectionSlaveThread(JdbcConnectionPool pool) 272 { 273 super(); 274 setDaemon(true); 275 this.pool = pool; 276 } 277 278 public void run() 279 { 280 try 281 { 282 connection = DriverManager.getConnection(JdbcConnectionPoolDriver.URL_PREFIX + pool.getName()); 283 } 284 catch (SQLException sqlx) 285 { 286 pool.removeWaiter(this); 287 exception = sqlx; 288 close(); 289 } 290 catch (Exception x) { 292 pool.removeWaiter(this); 293 close(); 294 } 295 } 296 297 private void close() 298 { 299 if (connection != null) 300 { 301 try 302 { 303 connection.close(); 304 } 305 catch (Exception x) 306 { 307 if (pool.useSyslog()) 308 { 309 Syslog.error(this, MessageFormat.format( 310 PoolResources.getResourceString(MessageConstants.CANNOT_CLOSE_OPEN_CONNECTION_MESSAGE), 311 new Object [] { pool.getName() }), x); 312 } 313 else 314 { 315 PrintWriter pw = DriverManager.getLogWriter(); 316 if (pw != null) 317 { 318 pw.println("JdbcConnectionPoolDataSource: " + 319 MessageFormat.format(PoolResources.getResourceString(MessageConstants.CANNOT_CLOSE_OPEN_CONNECTION_MESSAGE), 320 new Object [] { pool.getName() })); 321 x.printStackTrace(pw); 322 } 323 } 324 } 325 } 326 } 327 328 public Connection getConnection() 329 { 330 return this.connection; 331 } 332 333 public SQLException getException() 334 { 335 return this.exception; 336 } 337 } 338 } 339 | Popular Tags |