home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-19 | 6.5 KB | 175 lines |
- /*
- * @(#)ConnectionManager.java
- *
- * Copyright (c) 1997 Symantec Corporation. All Rights Reserved.
- *
- */
- /**
- * <P> This is meant to be an abstract base class. It can't be an actual abstract base class,
- * and it must have a public default constructor, in order to work in Visual Cafe's IDE.
- * Database projects should always have at least one (usually only one) class which extends from
- * this class and which contains all the instances of JdbcConnections that need be managed.
- * Programmers should not instantiate one of these classes.
- * A public method is provided to get an instance based on class name. This instance can then be
- * used to get instances of JdbcConnection objects.
- *
- * All JdbcConnection's are "shareable". To obtain an instance of a JdbcConnection, all that is
- * required is the appropriate identifying string. This string is usually a concatenation of the
- * URL,user-name,user-password,driver-name; but, need not be. As long as two "clients" agree to the
- * identifier, the connection can be "shared".
- *
- * Each "client" is responsible for releasing the connection when it is no longer needed. When the
- * manager sees that the connection is no longer in use by any "client", the connection will be closed.
- *
- * A "client" is a particular class of RecordDefinitions. When the first instance of a particular
- * RecordDefinition class is instanciated it gets a ConnectionManager instance. When the last instance
- * of a particular class of RecordDefinition's is finalized it releases the ConnectionManager instance.
- *
- */
-
- package symantec.itools.db.beans.jdbc;
-
- import java.sql.*;
- import java.util.Hashtable;
- import java.util.Enumeration;
-
- public class ConnectionManager
- {
- //PUBLIC SECTION
-
- //Constructors
- /**
- * Default constructor. This constructor is only provided because Bean-aware IDE's
- * and serialization require all beans have a default constructor.
- */
- public ConnectionManager()
- {
- }
-
- public synchronized void finalize() throws Throwable
- {
- Enumeration e= m_ConnectionsTable.elements();
- while (e.hasMoreElements()){
- JdbcConnection con=(JdbcConnection)e.nextElement();
- con.getSQLConnection().close();
- }
- m_ConnectionsTable=null;
- super.finalize();
- }
-
- //Methods
- /**
- * Programmers should always use this method to obtain a ConnectionManager instance.
- *
- * @param String className name of class derived from ConnectionManager
- *
- * @return ConnectionManager a usable instance of this class or a derivitive
- */
- public static ConnectionManager getManager(String className)
- {
- ConnectionManager aConnectionManager = null;
-
- if (m_ConnectionManagersTable.containsKey(className)) {
- aConnectionManager = (ConnectionManager)m_ConnectionManagersTable.get(className);
- }
- else {
- try {
- aConnectionManager = (ConnectionManager)Class.forName(className).newInstance();
- m_ConnectionManagersTable.put(className, aConnectionManager);
- }
- catch (Exception ex) {
- throw new RuntimeException(ex.getMessage());
- }
- }
- return aConnectionManager;
- }
-
- public static ConnectionManager getManager()
- throws SQLException
- {
- return getManager("ConnectionManager");
- }
-
- /**
- * Adds a JdbcConnection to be managed.
- * This method is usually called in the init section of ConnectionManager derivative classes.
- *
- * @param JdbcConnection
- */
- public void add(JdbcConnection connection)
- {
- if (java.beans.Beans.isDesignTime()) {
- return;
- }
- synchronized(m_ConnectionsTable) {
- m_ConnectionsTable.put(connection.getIdentifier(),connection);
- }
- synchronized(m_ConnectionsCounter) {
- m_ConnectionsCounter.put(connection.getIdentifier(),new Integer(1));
- }
- }
-
- /**
- * This method is used by RecordDefinition to obtain a JdbcConnection instance.
- * The JdbcConnection instance is used to connect/disconnect to a database and
- * to create/prepare/execute statements.
- *
- * @param String connectionIdentifier
- *
- * @return JdbcConnection
- *
- * @Exception SQLException
- */
- public synchronized JdbcConnection getConnectionBean(String connectionIdentifier)
- throws SQLException
- {
- JdbcConnection m_Connection = null;
-
- // If we can find a matching connection we return that one
- // If not, the application is not setup appropriately.
- if(m_ConnectionsTable.containsKey(connectionIdentifier))
- {
- m_Connection=(JdbcConnection)m_ConnectionsTable.get(connectionIdentifier);
- int cont=((Integer)m_ConnectionsCounter.get(connectionIdentifier)).intValue();
- cont++;
- m_ConnectionsCounter.remove(connectionIdentifier);
- m_ConnectionsCounter.put(connectionIdentifier,new Integer(cont));
- }
-
- return m_Connection;
- }
-
- /**
- * This method is used by RecordDefinition to release JdbcConnection's when the
- * last instance of the RecordDefinition of a particular class has been finalized.
- *
- * @param String connectionIdentifier
- */
- public synchronized void releaseConnection(String connectionIdentifier)
- {
- JdbcConnection m_Connection = null;
- if(m_ConnectionsTable.containsKey(connectionIdentifier)) {
- int cont = ((Integer)m_ConnectionsCounter.get(connectionIdentifier)).intValue();
- cont--;
- m_Connection=(JdbcConnection)m_ConnectionsTable.get(connectionIdentifier);
- m_ConnectionsCounter.remove(connectionIdentifier);
- m_ConnectionsCounter.put(connectionIdentifier,new Integer(cont));
- if (cont == 0) {
- try {
- if (!m_Connection.getSQLConnection().isClosed()) {
- m_Connection.setConnectionClosed(true);
- }
- }
- catch (Exception ex) {
- System.out.println(ex.getMessage());
- }
- }
- }
- }
-
- //PRIVATE SECTION
- private static Hashtable m_ConnectionManagersTable = new Hashtable();
- private Hashtable m_ConnectionsTable = new Hashtable();
- private Hashtable m_ConnectionsCounter = new Hashtable();
-
- }