1 19 package org.openharmonise.commons.dsi; 20 21 import java.sql.Connection ; 22 import java.sql.DriverManager ; 23 import java.sql.SQLException ; 24 import java.util.logging.*; 25 import java.util.logging.Logger ; 26 27 import org.openharmonise.commons.pool.*; 28 29 30 37 public class DBConnectionPool extends AbstractPool { 38 39 42 private String dsn; 43 44 47 private String usr; 48 49 52 private String pwd; 53 54 57 private static final Logger m_logger = Logger.getLogger(DBConnectionPool.class.getName()); 58 59 68 public DBConnectionPool(String driver, String dsn, String usr, 69 String pwd) { 70 try { 71 Class.forName(driver).newInstance(); 72 } catch (Exception e) { 73 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 74 } 75 76 this.dsn = dsn; 77 this.usr = usr; 78 this.pwd = pwd; 79 80 this.setMinPoolSize(5); 81 } 82 83 84 87 public Object create() throws SQLException { 88 return (DriverManager.getConnection(dsn, usr, pwd)); 89 } 90 91 94 public boolean validate(Object o) { 95 try { 96 return (!((Connection ) o).isClosed()); 97 } catch (SQLException e) { 98 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 99 100 return false; 101 } 102 } 103 104 107 public void expire(Object o) { 108 try { 109 ((Connection ) o).close(); 110 } catch (SQLException e) { 111 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 112 } 113 } 114 115 121 public Connection borrowConnection() throws SQLException { 122 try { 123 return (Connection ) super.checkOut(); 124 } catch (Exception e) { 125 throw (SQLException ) e; 126 } 127 } 128 129 134 public void returnConnection(Connection c) { 135 super.checkIn(c); 136 } 137 } | Popular Tags |