001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/io/DBPool.java $
002: /*---------------- FILE HEADER ------------------------------------------
003:
004: This file is part of deegree.
005: Copyright (C) 2001-2008 by:
006: EXSE, Department of Geography, University of Bonn
007: http://www.giub.uni-bonn.de/deegree/
008: lat/lon GmbH
009: http://www.lat-lon.de
010:
011: This library is free software; you can redistribute it and/or
012: modify it under the terms of the GNU Lesser General Public
013: License as published by the Free Software Foundation; either
014: version 2.1 of the License, or (at your option) any later version.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: Contact:
026:
027: Andreas Poth
028: lat/lon GmbH
029: Aennchenstr. 19
030: 53115 Bonn
031: Germany
032: E-Mail: poth@lat-lon.de
033:
034: Prof. Dr. Klaus Greve
035: Department of Geography
036: University of Bonn
037: Meckenheimer Allee 166
038: 53115 Bonn
039: Germany
040: E-Mail: greve@giub.uni-bonn.de
041:
042:
043: ---------------------------------------------------------------------------*/
044:
045: package org.deegree.io;
046:
047: import java.sql.Connection;
048: import java.sql.Driver;
049: import java.sql.DriverManager;
050: import java.sql.SQLException;
051: import java.util.Properties;
052:
053: import org.deegree.framework.util.ObjectPool;
054:
055: /**
056: * class to manage a pool of database connections.
057: *
058: *
059: * @version $Revision: 9342 $
060: * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
061: * @author last edited by: $Author: apoth $
062: *
063: * @version 1.0. $Revision: 9342 $, $Date: 2007-12-27 04:32:57 -0800 (Thu, 27 Dec 2007) $
064: *
065: * @since 2.0
066: */
067: public class DBPool extends ObjectPool {
068:
069: private String driver = null;
070:
071: private String database = null;
072:
073: private Properties properties = new Properties();
074:
075: /**
076: * initialize pool for defined connection parameters
077: *
078: * @param driver
079: * @param database
080: * @param user
081: * @param password
082: */
083: public DBPool(final String driver, final String database,
084: final String user, final String password) {
085:
086: this .driver = driver;
087: this .database = database;
088: properties.put("user", user);
089: properties.put("password", password);
090: }
091:
092: /**
093: * initialize pool for defined connection parameters
094: *
095: * @param driver
096: * @param database
097: * @param properties
098: */
099: public DBPool(final String driver, final String database,
100: final Properties properties) {
101:
102: this .driver = driver;
103: this .database = database;
104: this .properties = properties;
105: }
106:
107: /**
108: * get an object from the object pool
109: *
110: * @throws DBPoolException
111: */
112: public synchronized Object acquireObject() throws DBPoolException {
113: try {
114: // if the maximum amount of instances are in use
115: // wait until an instance has been released back
116: // to the pool or 20 seconds has passed
117: long timediff = 0;
118: while (in_use.size() == getMaxInstances()
119: && timediff < 20000) {
120: Thread.sleep(100);
121: timediff += 100;
122: }
123: // if no instance has been released within 20 seconds
124: // or can newly be instantiated return null
125: if (timediff >= 20000)
126: return null;
127:
128: // if a none used is available from the pool
129: if (available.size() > 0) {
130:
131: // get/remove ojebct from the pool
132: Object o = available.remove(available.size() - 1);
133: if (((Connection) o).isClosed()) {
134: o = acquireObject();
135: }
136:
137: // add it to 'in use' container
138: in_use.add(o);
139:
140: // reset its start life time
141: startLifeTime.put(o, new Long(System
142: .currentTimeMillis()));
143: // set the start of its usage
144: startUsageTime.put(o, new Long(System
145: .currentTimeMillis()));
146:
147: // return the object
148: return o;
149:
150: }
151: // else instatiate a new object
152: // create a new class instance
153: DriverManager.registerDriver((Driver) Class.forName(driver)
154: .newInstance());
155:
156: Properties prop = (Properties) properties.clone();
157: Object connection = DriverManager.getConnection(database,
158: prop);
159:
160: existingInstances++;
161:
162: // add it to 'in use' container
163: in_use.add(connection);
164: // set the start of its life time
165: startLifeTime.put(connection, new Long(System
166: .currentTimeMillis()));
167: // set the start of its usage
168: startUsageTime.put(connection, new Long(System
169: .currentTimeMillis()));
170: // return the object
171: return connection;
172: } catch (Exception e) {
173: e.printStackTrace();
174: throw new DBPoolException(
175: "Error while acquiring connection: "
176: + e.getMessage(), e);
177: }
178: }
179:
180: /**
181: * will be called when the object is removed from the pool
182: *
183: * @param o
184: */
185: public void onObjectKill(Object o) {
186: try {
187: ((Connection) o).close();
188: } catch (SQLException e) {
189: }
190: }
191:
192: }
|