/*
* $Id: ConnectionManager.java,v 1.8 2002/09/16 08:05:03 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.database;
import java.util.Enumeration;
import java.util.Hashtable;
/**
* Class providing interface for acquiring and
* releasing JDBC connections.
*
* Usage:
* <pre>
*
* import java.sql.*;
* import anvil.database.*;
*
* ConnectionManager manager = ...;
* PooledConnection connImpl = null;
* Connection conn = null;
*
* try {
*
* connImpl = manager.acquire("njet");
* conn = (Connection)connImpl.getConnection();
* // do some work
*
* } catch (NoConnectionPoolException e) {
* // handle error
* } catch (CannotReturnPooledConnectionException e) {
* // handle error
* } catch (SQLException e) {
* // It is recommended that the connection is closed in case of SQLException.
* // When the connection is closed the Connection.isClosed() method returns
* // false and connection will be removed from the queue by the house-keeping
* // thread after a short while.
* if (conn!= null) {
* conn.close();
* }
* } finally {
* if (connImpl != null) {
* connImpl.release();
* }
* }
*
* </pre>
*
* @version $Revision: 1.8 $
* @author Jani Lehtimki
*/
public class ConnectionManager
{
/**
* <code>Hashtable</code> of <code>ConnectionAccessQueue</code>'s.
*/
private Hashtable _queues = new Hashtable();
/**
* Constructs connection manager.
*/
public ConnectionManager()
{
}
public boolean hasPool(String pool)
{
return _queues.containsKey(pool);
}
public void addPool(ConnectionPool pool)
{
ConnectionAccessQueue queue = new ConnectionAccessQueue(pool);
_queues.put(pool.getName(), queue);
}
/**
* Gets the enumeration of <code>ConnectionAccessQueues</code>.
*
* @return Enumeration
* @see ConnectionAccessQueue
*/
public Enumeration getAccessQueues()
{
return _queues.elements();
}
/**
* Acquires connection of given type.
*
* @param connectionKey Type of connection
* @throws NoConnectionPoolException If connectionKey was <code>null</code>
* or given pool has not been configured.
* @throws CannotReturnPooledConnectionException If connection couldn't be
* constructed, propably due the acquire timeout.
* @return Instance of <code>ConnectionManager</code>
*/
public PooledConnection acquire(String connectionKey)
throws NoConnectionPoolException, CannotReturnPooledConnectionException
{
return acquire(connectionKey, 0);
}
/**
* Acquires connection of given type, overriding the connection reserve timeout.
*
* @param connectionKey Type of connection
* @param timeout Timeout override in seconds (if > 0)
* @throws NoConnectionPoolException If connectionKey was <code>null</code>
* or given pool has not been configured.
* @throws CannotReturnPooledConnectionException If connection couldn't be
* constructed, propably due the acquire timeout.
* @return Instance of <code>ConnectionManager</code>
*/
public PooledConnection acquire(String connectionKey, int timeout)
throws NoConnectionPoolException, CannotReturnPooledConnectionException
{
if (connectionKey == null) {
throw new NoConnectionPoolException("Key not given");
} else {
connectionKey = connectionKey.toLowerCase();
}
ConnectionAccessQueue queue = (ConnectionAccessQueue) _queues.get(connectionKey);
if (queue == null) {
throw new NoConnectionPoolException("With key " + connectionKey);
}
anvil.script.Context context = anvil.script.Context.getExistingInstance();
if (context != null) {
context.checkAcquire(connectionKey);
}
PooledConnection conn = queue.acquire(timeout * 1000);
return conn;
}
/**
* Shuts down the connection manager and all related
* classes. Shutdown is complete when this method finishes.
*/
public void stop()
{
ConnectionAccessQueue queue;
Enumeration e = _queues.elements();
while(e.hasMoreElements()) {
queue = (ConnectionAccessQueue)e.nextElement();
queue.stop();
}
_queues.clear();
}
}
|