1 16 19 package org.apache.xalan.lib.sql; 20 21 import java.sql.Connection ; 22 import java.sql.DatabaseMetaData ; 23 import java.sql.Driver ; 24 import java.sql.DriverManager ; 25 import java.sql.SQLException ; 26 import java.util.Enumeration ; 27 import java.util.Properties ; 28 import java.util.Vector ; 29 30 import org.apache.xalan.res.XSLMessages; 31 import org.apache.xalan.res.XSLTErrorResources; 32 33 37 public class DefaultConnectionPool implements ConnectionPool 38 { 39 43 private Driver m_Driver = null; 44 46 private static final boolean DEBUG = false; 47 48 51 private String m_driver = new String (""); 52 54 private String m_url = new String (""); 55 56 57 63 private int m_PoolMinSize = 1; 64 65 66 71 private Properties m_ConnectionProtocol = new Properties (); 72 73 76 private Vector m_pool = new Vector (); 77 78 81 private boolean m_IsActive = false; 82 83 85 public DefaultConnectionPool( ) {} 86 87 88 92 public boolean isEnabled( ) 93 { 94 return m_IsActive; 95 } 96 97 102 public void setDriver( String d ) 103 { 104 m_driver = d; 105 } 106 107 112 public void setURL( String url ) 113 { 114 m_url = url; 115 } 116 117 122 public void freeUnused( ) 123 { 124 for ( int x = 0; x < m_pool.size(); x++ ) 127 { 128 129 130 PooledConnection pcon = 131 (PooledConnection) m_pool.elementAt(x); 132 133 if ( pcon.inUse() == false ) 135 { 136 if (DEBUG) 137 { 138 System.err.println("Closing JDBC Connection " + x); 139 } 140 141 pcon.close(); 142 } 143 } 144 145 } 146 147 151 public boolean hasActiveConnections( ) 152 { 153 return (m_pool.size() > 0); 154 } 155 156 157 162 public void setPassword( String p ) 163 { 164 m_ConnectionProtocol.put("password", p); 165 } 166 167 172 public void setUser( String u ) 173 { 174 m_ConnectionProtocol.put("user", u); 175 } 176 177 184 public void setProtocol( Properties p ) 185 { 186 Enumeration e = p.keys(); 187 while (e.hasMoreElements()) 188 { 189 String key = (String ) e.nextElement(); 190 m_ConnectionProtocol.put(key, p.getProperty(key)); 191 } 192 } 193 194 195 202 public void setMinConnections( int n ) 203 { 204 m_PoolMinSize = n; 205 } 206 207 213 public boolean testConnection( ) 214 { 215 try 216 { 217 if (DEBUG) 218 { 219 System.out.println("Testing Connection"); 220 } 221 222 Connection conn = getConnection(); 223 224 if (DEBUG) 225 { 226 DatabaseMetaData dma = conn.getMetaData(); 227 228 System.out.println("\nConnected to " + dma.getURL()); 229 System.out.println("Driver " + dma.getDriverName()); 230 System.out.println("Version " + dma.getDriverVersion()); 231 System.out.println(""); 232 } 233 234 if (conn == null) return false; 235 236 releaseConnection(conn); 237 238 if (DEBUG) 239 { 240 System.out.println("Testing Connection, SUCCESS"); 241 } 242 243 return true; 244 } 245 catch(Exception e) 246 { 247 if (DEBUG) 248 { 249 System.out.println("Testing Connection, FAILED"); 250 e.printStackTrace(); 251 } 252 253 return false; 254 } 255 256 } 257 258 259 265 public synchronized Connection getConnection( )throws IllegalArgumentException , SQLException 266 { 267 268 PooledConnection pcon = null; 269 270 if ( m_pool.size() < m_PoolMinSize ) { initializePool(); } 275 276 for ( int x = 0; x < m_pool.size(); x++ ) 278 { 279 280 pcon = (PooledConnection) m_pool.elementAt(x); 281 282 if ( pcon.inUse() == false ) 284 { 285 pcon.setInUse(true); 287 return pcon.getConnection(); 290 } 291 } 292 293 296 Connection con = createConnection(); 298 299 pcon = new PooledConnection(con); 302 303 pcon.setInUse(true); 305 306 m_pool.addElement(pcon); 308 309 return pcon.getConnection(); 311 } 312 313 318 public synchronized void releaseConnection( Connection con )throws SQLException 319 { 320 321 for ( int x = 0; x < m_pool.size(); x++ ) 323 { 324 325 PooledConnection pcon = 326 (PooledConnection) m_pool.elementAt(x); 327 328 if ( pcon.getConnection() == con ) 330 { 331 if (DEBUG) 332 { 333 System.out.println("Releasing Connection " + x); 334 } 335 336 if (! isEnabled()) 337 { 338 con.close(); 339 m_pool.removeElementAt(x); 340 if (DEBUG) 341 { 342 System.out.println("-->Inactive Pool, Closing connection"); 343 } 344 345 } 346 else 347 { 348 pcon.setInUse(false); 351 } 352 353 break; 354 } 355 } 356 } 357 358 359 364 public synchronized void releaseConnectionOnError( Connection con )throws SQLException 365 { 366 367 for ( int x = 0; x < m_pool.size(); x++ ) 369 { 370 371 PooledConnection pcon = 372 (PooledConnection) m_pool.elementAt(x); 373 374 if ( pcon.getConnection() == con ) 376 { 377 if (DEBUG) 378 { 379 System.out.println("Releasing Connection On Error" + x); 380 } 381 382 con.close(); 383 m_pool.removeElementAt(x); 384 if (DEBUG) 385 { 386 System.out.println("-->Inactive Pool, Closing connection"); 387 } 388 break; 389 } 390 } 391 } 392 393 394 398 private Connection createConnection( )throws SQLException 399 { 400 Connection con = null; 401 402 con = m_Driver.connect(m_url, m_ConnectionProtocol ); 405 406 return con; 407 } 408 409 415 public synchronized void initializePool( )throws IllegalArgumentException , SQLException 416 { 417 418 if ( m_driver == null ) 420 { 421 throw new IllegalArgumentException (XSLMessages.createMessage(XSLTErrorResources.ER_NO_DRIVER_NAME_SPECIFIED, null)); 422 } 424 425 if ( m_url == null ) 426 { 427 throw new IllegalArgumentException (XSLMessages.createMessage(XSLTErrorResources.ER_NO_URL_SPECIFIED, null)); 428 } 430 431 if ( m_PoolMinSize < 1 ) 432 { 433 throw new IllegalArgumentException (XSLMessages.createMessage(XSLTErrorResources.ER_POOLSIZE_LESS_THAN_ONE, null)); 434 } 436 437 440 try 441 { 442 m_Driver = (Driver ) ObjectFactory.newInstance( 445 m_driver, ObjectFactory.findClassLoader(), true); 446 447 DriverManager.registerDriver(m_Driver); 451 } 452 catch(ObjectFactory.ConfigurationError e) 453 { 454 throw new IllegalArgumentException (XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_DRIVER_NAME, null)); 455 } 457 catch(Exception e) 458 { 459 throw new IllegalArgumentException (XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_DRIVER_NAME, null)); 460 } 461 462 if ( !m_IsActive) return; 465 466 do 468 { 469 470 Connection con = createConnection(); 471 472 if ( con != null ) 473 { 474 475 PooledConnection pcon = new PooledConnection(con); 478 479 addConnection(pcon); 481 482 if (DEBUG) System.out.println("Adding DB Connection to the Pool"); 483 } 484 } 485 while (m_pool.size() < m_PoolMinSize); 486 } 487 488 493 private void addConnection( PooledConnection value ) 494 { 495 m_pool.addElement(value); 497 } 498 499 500 504 protected void finalize( )throws Throwable 505 { 506 if (DEBUG) 507 { 508 System.out.println("In Default Connection Pool, Finalize"); 509 } 510 511 for ( int x = 0; x < m_pool.size(); x++ ) 514 { 515 516 if (DEBUG) 517 { 518 System.out.println("Closing JDBC Connection " + x); 519 } 520 521 PooledConnection pcon = 522 (PooledConnection) m_pool.elementAt(x); 523 524 if ( pcon.inUse() == false ) { pcon.close(); } 526 else 527 { 528 if (DEBUG) 529 { 530 System.out.println("--> Force close"); 531 } 532 533 try 536 { 537 java.lang.Thread.sleep(30000); 538 pcon.close(); 539 } 540 catch (InterruptedException ie) 541 { 542 if (DEBUG) System.err.println(ie.getMessage()); 543 } 544 } 545 } 546 547 if (DEBUG) 548 { 549 System.out.println("Exit Default Connection Pool, Finalize"); 550 } 551 552 super.finalize(); 553 } 554 555 568 public void setPoolEnabled( boolean flag ) 569 { 570 m_IsActive = flag; 571 if ( ! flag ) 572 freeUnused(); 573 } 574 575 } 576 | Popular Tags |