001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.commons.dsi;
020:
021: import java.sql.Connection;
022: import java.sql.DriverManager;
023: import java.sql.SQLException;
024: import java.util.logging.*;
025: import java.util.logging.Logger;
026:
027: import org.openharmonise.commons.pool.*;
028:
029: /**
030: * A pool of database connections.
031: *
032: * @author Michael Bell
033: * @version $Revision: 1.2 $
034: *
035: */
036: public class DBConnectionPool extends AbstractPool {
037:
038: /**
039: * The database URI
040: */
041: private String dsn;
042:
043: /**
044: * The database user name
045: */
046: private String usr;
047:
048: /**
049: * The database user password
050: */
051: private String pwd;
052:
053: /**
054: * Logger for this class
055: */
056: private static final Logger m_logger = Logger
057: .getLogger(DBConnectionPool.class.getName());
058:
059: /**
060: * Constructs a DB connection pool which will pool connections with
061: * the given parameters.
062: *
063: * @param driver the JDBC driver class name
064: * @param dsn the database URI
065: * @param usr the database user name
066: * @param pwd the database user password
067: */
068: public DBConnectionPool(String driver, String dsn, String usr,
069: String pwd) {
070: try {
071: Class.forName(driver).newInstance();
072: } catch (Exception e) {
073: m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
074: }
075:
076: this .dsn = dsn;
077: this .usr = usr;
078: this .pwd = pwd;
079:
080: this .setMinPoolSize(5);
081: }
082:
083: /* (non-Javadoc)
084: * @see org.openharmonise.commons.pool.AbstractPool#create()
085: */
086: public Object create() throws SQLException {
087: return (DriverManager.getConnection(dsn, usr, pwd));
088: }
089:
090: /* (non-Javadoc)
091: * @see org.openharmonise.commons.pool.AbstractPool#validate(java.lang.Object)
092: */
093: public boolean validate(Object o) {
094: try {
095: return (!((Connection) o).isClosed());
096: } catch (SQLException e) {
097: m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
098:
099: return false;
100: }
101: }
102:
103: /* (non-Javadoc)
104: * @see org.openharmonise.commons.pool.AbstractPool#expire(java.lang.Object)
105: */
106: public void expire(Object o) {
107: try {
108: ((Connection) o).close();
109: } catch (SQLException e) {
110: m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
111: }
112: }
113:
114: /**
115: * Returns a connection from the pool.
116: *
117: * @return a connection from the pool
118: * @throws SQLException if an error occurs
119: */
120: public Connection borrowConnection() throws SQLException {
121: try {
122: return (Connection) super .checkOut();
123: } catch (Exception e) {
124: throw (SQLException) e;
125: }
126: }
127:
128: /**
129: * Checks the connection back in to the pool.
130: *
131: * @param c the connection to be checked back in
132: */
133: public void returnConnection(Connection c) {
134: super.checkIn(c);
135: }
136: }
|