001: /*
002: * $Id: ConnectionManager.java,v 1.8 2002/09/16 08:05:03 jkl Exp $
003: *
004: * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
005: *
006: * Use is subject to license terms, as defined in
007: * Anvil Sofware License, Version 1.1. See LICENSE
008: * file, or http://njet.org/license-1.1.txt
009: */
010: package anvil.database;
011:
012: import java.util.Enumeration;
013: import java.util.Hashtable;
014:
015: /**
016: * Class providing interface for acquiring and
017: * releasing JDBC connections.
018: *
019: * Usage:
020: * <pre>
021: *
022: * import java.sql.*;
023: * import anvil.database.*;
024: *
025: * ConnectionManager manager = ...;
026:
027: * PooledConnection connImpl = null;
028: * Connection conn = null;
029: *
030: * try {
031: *
032: * connImpl = manager.acquire("njet");
033: * conn = (Connection)connImpl.getConnection();
034: * // do some work
035: *
036: * } catch (NoConnectionPoolException e) {
037: * // handle error
038: * } catch (CannotReturnPooledConnectionException e) {
039: * // handle error
040: * } catch (SQLException e) {
041: * // It is recommended that the connection is closed in case of SQLException.
042: * // When the connection is closed the Connection.isClosed() method returns
043: * // false and connection will be removed from the queue by the house-keeping
044: * // thread after a short while.
045: * if (conn!= null) {
046: * conn.close();
047: * }
048: * } finally {
049: * if (connImpl != null) {
050: * connImpl.release();
051: * }
052: * }
053: *
054: * </pre>
055: *
056: * @version $Revision: 1.8 $
057: * @author Jani Lehtimäki
058: */
059: public class ConnectionManager {
060:
061: /**
062: * <code>Hashtable</code> of <code>ConnectionAccessQueue</code>'s.
063: */
064: private Hashtable _queues = new Hashtable();
065:
066: /**
067: * Constructs connection manager.
068: */
069: public ConnectionManager() {
070: }
071:
072: public boolean hasPool(String pool) {
073: return _queues.containsKey(pool);
074: }
075:
076: public void addPool(ConnectionPool pool) {
077: ConnectionAccessQueue queue = new ConnectionAccessQueue(pool);
078: _queues.put(pool.getName(), queue);
079: }
080:
081: /**
082: * Gets the enumeration of <code>ConnectionAccessQueues</code>.
083: *
084: * @return Enumeration
085: * @see ConnectionAccessQueue
086: */
087: public Enumeration getAccessQueues() {
088: return _queues.elements();
089: }
090:
091: /**
092: * Acquires connection of given type.
093: *
094: * @param connectionKey Type of connection
095: * @throws NoConnectionPoolException If connectionKey was <code>null</code>
096: * or given pool has not been configured.
097: * @throws CannotReturnPooledConnectionException If connection couldn't be
098: * constructed, propably due the acquire timeout.
099: * @return Instance of <code>ConnectionManager</code>
100: */
101: public PooledConnection acquire(String connectionKey)
102: throws NoConnectionPoolException,
103: CannotReturnPooledConnectionException {
104: return acquire(connectionKey, 0);
105: }
106:
107: /**
108: * Acquires connection of given type, overriding the connection reserve timeout.
109: *
110: * @param connectionKey Type of connection
111: * @param timeout Timeout override in seconds (if > 0)
112: * @throws NoConnectionPoolException If connectionKey was <code>null</code>
113: * or given pool has not been configured.
114: * @throws CannotReturnPooledConnectionException If connection couldn't be
115: * constructed, propably due the acquire timeout.
116: * @return Instance of <code>ConnectionManager</code>
117: */
118: public PooledConnection acquire(String connectionKey, int timeout)
119: throws NoConnectionPoolException,
120: CannotReturnPooledConnectionException {
121: if (connectionKey == null) {
122: throw new NoConnectionPoolException("Key not given");
123: } else {
124: connectionKey = connectionKey.toLowerCase();
125: }
126: ConnectionAccessQueue queue = (ConnectionAccessQueue) _queues
127: .get(connectionKey);
128: if (queue == null) {
129: throw new NoConnectionPoolException("With key "
130: + connectionKey);
131: }
132:
133: anvil.script.Context context = anvil.script.Context
134: .getExistingInstance();
135: if (context != null) {
136: context.checkAcquire(connectionKey);
137: }
138:
139: PooledConnection conn = queue.acquire(timeout * 1000);
140: return conn;
141: }
142:
143: /**
144: * Shuts down the connection manager and all related
145: * classes. Shutdown is complete when this method finishes.
146: */
147: public void stop() {
148: ConnectionAccessQueue queue;
149: Enumeration e = _queues.elements();
150: while (e.hasMoreElements()) {
151: queue = (ConnectionAccessQueue) e.nextElement();
152: queue.stop();
153: }
154: _queues.clear();
155: }
156:
157: }
|