001 //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/io/DBConnectionPool.java $
002 /*----------------------------------------------------------------------------
003 This file is part of deegree, http://deegree.org/
004 Copyright (C) 2001-2009 by:
005 Department of Geography, University of Bonn
006 and
007 lat/lon GmbH
008
009 This library is free software; you can redistribute it and/or modify it under
010 the terms of the GNU Lesser General Public License as published by the Free
011 Software Foundation; either version 2.1 of the License, or (at your option)
012 any later version.
013 This library is distributed in the hope that it will be useful, but WITHOUT
014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016 details.
017 You should have received a copy of the GNU Lesser General Public License
018 along with this library; if not, write to the Free Software Foundation, Inc.,
019 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020
021 Contact information:
022
023 lat/lon GmbH
024 Aennchenstr. 19, 53177 Bonn
025 Germany
026 http://lat-lon.de/
027
028 Department of Geography, University of Bonn
029 Prof. Dr. Klaus Greve
030 Postfach 1147, 53001 Bonn
031 Germany
032 http://www.geographie.uni-bonn.de/deegree/
033
034 e-mail: info@deegree.org
035 ----------------------------------------------------------------------------*/
036 package org.deegree.io;
037
038 import java.sql.Connection;
039 import java.util.HashMap;
040 import java.util.Map;
041 import java.util.Properties;
042
043 /**
044 * class to manage a database connection pool. this is part of the combination of the object pool pattern an the
045 * singelton pattern.
046 *
047 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth </a>
048 * @version 07.02.2001
049 */
050 public class DBConnectionPool {
051
052 private static DBConnectionPool instance = null;
053
054 private Map<String, DBPool> pools = null;
055
056 /**
057 * Creates a new DBConnectionPool object.
058 */
059 private DBConnectionPool() {
060 pools = new HashMap<String, DBPool>();
061 }
062
063 /**
064 * realize singelton pattern using double checked locking pattern.
065 *
066 * @return an instance of the data base pool. it is gauranteed that there exists only one instance of pool for each
067 * submitted class name.
068 *
069 */
070 public static DBConnectionPool getInstance() {
071 if ( instance == null ) {
072 synchronized ( DBConnectionPool.class ) {
073 if ( instance == null ) {
074 instance = new DBConnectionPool();
075 }
076 }
077 }
078
079 return instance;
080 }
081
082 /**
083 * @param driver
084 * driver to look for
085 * @param database
086 * the database to open
087 * @param user
088 * the username
089 * @param password
090 * the password.
091 * @return get a Connection from the Connection pool
092 * @throws DBPoolException
093 */
094 public synchronized Connection acquireConnection( final String driver, final String database, final String user,
095 final String password )
096 throws DBPoolException {
097 String q = driver + database + user + password;
098 DBPool pool = null;
099 if ( pools.get( q ) == null ) {
100 pool = new DBPool( driver, database, user, password );
101 pools.put( q, pool );
102 } else {
103 pool = pools.get( q );
104 }
105 return (Connection) pool.acquireObject();
106 }
107
108 /**
109 * @param driver
110 * driver to look for
111 * @param database
112 * the database to open
113 * @param properties
114 * the properties of the database
115 * @return get a Connection from the Connection pool
116 * @throws DBPoolException
117 */
118 public synchronized Connection acquireConnection( final String driver, final String database,
119 final Properties properties )
120 throws DBPoolException {
121 String q = driver + database + properties.toString();
122
123 if ( pools.get( q ) == null ) {
124 DBPool pool = new DBPool( driver, database, properties );
125 pools.put( q, pool );
126 return (Connection) pool.acquireObject();
127 }
128 DBPool pool = pools.get( q );
129 return (Connection) pool.acquireObject();
130 }
131
132 /**
133 * releases a connection back to the pool
134 *
135 * @param con
136 * connections to be released
137 * @param driver
138 * driver to look for
139 * @param database
140 * the database to open
141 * @param user
142 * the username
143 * @param password
144 * the password.
145 * @throws DBPoolException
146 */
147 public void releaseConnection( final Connection con, final String driver, final String database, final String user,
148 final String password )
149 throws DBPoolException {
150 // prevent dead locks because of stupid code trying to release null connections
151 if ( con == null ) {
152 return;
153 }
154 String q = driver + database + user + password;
155 DBPool pool = pools.get( q );
156 try {
157 con.setAutoCommit( true );
158 pool.releaseObject( con );
159 } catch ( Exception e ) {
160 throw new DBPoolException( "could not release connection", e );
161 }
162 }
163
164 /**
165 * releases a connection back to the pool
166 *
167 * @param con
168 * connections to be released
169 * @param driver
170 * driver to look for
171 * @param database
172 * the database to open
173 * @param properties
174 * containing username and password.
175 * @throws Exception
176 */
177 public void releaseConnection( final Connection con, final String driver, final String database,
178 final Properties properties )
179 throws Exception {
180 String q = driver + database + properties.toString();
181 DBPool pool = pools.get( q );
182 con.setAutoCommit( true );
183 pool.releaseObject( con );
184 }
185
186 }