001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/io/sdeapi/SDEConnectionPool.java $
002 /*---------------- FILE HEADER ------------------------------------------
003
004 This file is part of deegree.
005 Copyright (C) 2006 by: M.O.S.S. Computer Grafik Systeme GmbH
006 Hohenbrunner Weg 13
007 D-82024 Taufkirchen
008 http://www.moss.de/
009
010 This library is free software; you can redistribute it and/or
011 modify it under the terms of the GNU Lesser General Public
012 License as published by the Free Software Foundation; either
013 version 2.1 of the License, or (at your option) any later version.
014
015 This library is distributed in the hope that it will be useful,
016 but WITHOUT ANY WARRANTY; without even the implied warranty of
017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
018 Lesser General Public License for more details.
019
020 You should have received a copy of the GNU Lesser General Public
021 License along with this library; if not, write to the Free Software
022 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
023
024 ---------------------------------------------------------------------------*/
025 package org.deegree.io.sdeapi;
026
027 import java.util.HashMap;
028 import java.util.Map;
029
030 import org.deegree.io.DBPoolException;
031
032 /**
033 * class to manage a pool of sde connections.
034 *
035 * @author <a href="mailto:cpollmann@moss.de">Christoph Pollmann</a>
036 * @version 2.0
037 */
038 public class SDEConnectionPool {
039
040 private static SDEConnectionPool instance = null;
041
042 private Map<String, SDEPool> pools = null;
043
044 /**
045 * Creates a new SDEConnectionPool object.
046 */
047 private SDEConnectionPool() {
048 pools = new HashMap<String, SDEPool>();
049 }
050
051 /**
052 * realize singelton pattern using double checked locking pattern.
053 *
054 * @return an instance of the data base pool. it is gauranteed that there exists only one
055 * instance of pool for each submitted class name.
056 *
057 */
058 public static SDEConnectionPool getInstance() {
059 if ( instance == null ) {
060 synchronized ( SDEConnectionPool.class ) {
061 if ( instance == null ) {
062 instance = new SDEConnectionPool();
063 }
064 }
065 }
066
067 return instance;
068 }
069
070 /**
071 * get an object from the object pool
072 *
073 * @param server
074 * @param instance
075 * @param database
076 * @param version
077 * @param user
078 * @param password
079 * @return
080 * @throws Exception
081 */
082 public synchronized SDEConnection acquireConnection( final String server, final int instance,
083 final String database, final String version,
084 final String user, final String password )
085 throws Exception {
086 String q = server + instance + database + version + user;
087 SDEPool pool = pools.get( q );
088 if ( pool == null ) {
089 pool = new SDEPool( server, instance, database, version, user, password );
090 pools.put( q, pool );
091 }
092 return (SDEConnection) pool.acquireObject();
093 }
094
095 /**
096 * releases a connection back to the pool
097 *
098 * @param con
099 * @param server
100 * @param instance
101 * @param database
102 * @param version
103 * @param user
104 * @throws DBPoolException
105 */
106 public synchronized void releaseConnection( final SDEConnection con, final String server, final int instance,
107 final String database, final String version, final String user )
108 throws DBPoolException {
109 String q = server + instance + database + version + user;
110 SDEPool pool = pools.get( q );
111 try {
112 pool.releaseObject( con );
113 } catch ( Exception e ) {
114 throw new DBPoolException( "could not release connection", e );
115 }
116 }
117 }