home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / VCafe / prosrc.bin / ConnectionManager.java < prev    next >
Encoding:
Java Source  |  1998-03-19  |  6.5 KB  |  175 lines

  1. /*
  2.  * @(#)ConnectionManager.java
  3.  *
  4.  * Copyright (c) 1997 Symantec Corporation. All Rights Reserved.
  5.  *
  6.  */
  7.  /**
  8.  * <P> This is meant to be an abstract base class.  It can't be an actual abstract base class,
  9.  * and it must have a public default constructor, in order to work in Visual Cafe's IDE.
  10.  * Database projects should always have at least one (usually only one) class which extends from
  11.  * this class and which contains all the instances of JdbcConnections that need be managed.
  12.  * Programmers should not instantiate one of these classes.
  13.  * A public method is provided to get an instance based on class name.  This instance can then be
  14.  * used to get instances of JdbcConnection objects.
  15.  *
  16.  * All JdbcConnection's are "shareable".  To obtain an instance of a JdbcConnection, all that is
  17.  * required is the appropriate identifying string.  This string is usually a concatenation of the
  18.  * URL,user-name,user-password,driver-name; but, need not be.  As long as two "clients" agree to the
  19.  * identifier, the connection can be "shared".
  20.  *
  21.  * Each "client" is responsible for releasing the connection when it is no longer needed.  When the
  22.  * manager sees that the connection is no longer in use by any "client", the connection will be closed.
  23.  *
  24.  * A "client" is a particular class of RecordDefinitions.  When the first instance of a particular
  25.  * RecordDefinition class is instanciated it gets a ConnectionManager instance.  When the last instance
  26.  * of a particular class of RecordDefinition's is finalized it releases the ConnectionManager instance.
  27.  *
  28.  */
  29.  
  30. package symantec.itools.db.beans.jdbc;
  31.  
  32. import java.sql.*;
  33. import java.util.Hashtable;
  34. import java.util.Enumeration;
  35.  
  36. public class ConnectionManager
  37. {
  38. //PUBLIC SECTION
  39.  
  40. //Constructors
  41.     /**
  42.      * Default constructor.  This constructor is only provided because Bean-aware IDE's
  43.      * and serialization require all beans have a default constructor.
  44.      */
  45.     public ConnectionManager()
  46.     {
  47.     }
  48.  
  49.     public synchronized void finalize() throws Throwable
  50.     {
  51.         Enumeration e= m_ConnectionsTable.elements();
  52.         while (e.hasMoreElements()){
  53.             JdbcConnection con=(JdbcConnection)e.nextElement();
  54.             con.getSQLConnection().close();
  55.         }
  56.         m_ConnectionsTable=null;
  57.         super.finalize();
  58.     }
  59.  
  60. //Methods
  61.     /**
  62.      * Programmers should always use this method to obtain a ConnectionManager instance.
  63.      *
  64.      * @param   String className    name of class derived from ConnectionManager
  65.      *
  66.      * @return      ConnectionManager   a usable instance of this class or a derivitive
  67.      */
  68.     public static ConnectionManager getManager(String className)
  69.     {
  70.         ConnectionManager aConnectionManager = null;
  71.  
  72.         if (m_ConnectionManagersTable.containsKey(className)) {
  73.             aConnectionManager = (ConnectionManager)m_ConnectionManagersTable.get(className);
  74.         }
  75.         else {
  76.             try {
  77.                 aConnectionManager = (ConnectionManager)Class.forName(className).newInstance();
  78.                 m_ConnectionManagersTable.put(className, aConnectionManager);
  79.             }
  80.             catch (Exception ex) {
  81.                 throw new RuntimeException(ex.getMessage());
  82.             }
  83.         }
  84.         return aConnectionManager;
  85.     }
  86.  
  87.     public static ConnectionManager getManager()
  88.         throws SQLException
  89.     {
  90.         return getManager("ConnectionManager");
  91.     }
  92.  
  93.     /**
  94.      * Adds a JdbcConnection to be managed.
  95.      * This method is usually called in the init section of ConnectionManager derivative classes.
  96.      *
  97.      * @param   JdbcConnection
  98.      */
  99.     public void add(JdbcConnection connection)
  100.     {
  101.         if (java.beans.Beans.isDesignTime()) {
  102.             return;
  103.         }
  104.         synchronized(m_ConnectionsTable) {
  105.             m_ConnectionsTable.put(connection.getIdentifier(),connection);
  106.         }
  107.         synchronized(m_ConnectionsCounter) {
  108.             m_ConnectionsCounter.put(connection.getIdentifier(),new Integer(1));
  109.         }
  110.     }
  111.  
  112.     /**
  113.      * This method is used by RecordDefinition to obtain a JdbcConnection instance.
  114.      * The JdbcConnection instance is used to connect/disconnect to a database and
  115.      * to create/prepare/execute statements.
  116.      *
  117.      * @param   String          connectionIdentifier
  118.      *
  119.      * @return  JdbcConnection
  120.      *
  121.      * @Exception SQLException
  122.      */
  123.     public synchronized JdbcConnection getConnectionBean(String connectionIdentifier)
  124.         throws SQLException
  125.     {
  126.         JdbcConnection m_Connection = null;
  127.  
  128.         // If we can find a matching connection we return that one
  129.         // If not, the application is not setup appropriately.
  130.         if(m_ConnectionsTable.containsKey(connectionIdentifier))
  131.         {
  132.                 m_Connection=(JdbcConnection)m_ConnectionsTable.get(connectionIdentifier);
  133.                 int cont=((Integer)m_ConnectionsCounter.get(connectionIdentifier)).intValue();
  134.                 cont++;
  135.                 m_ConnectionsCounter.remove(connectionIdentifier);
  136.                 m_ConnectionsCounter.put(connectionIdentifier,new Integer(cont));
  137.         }
  138.  
  139.         return m_Connection;
  140.     }
  141.  
  142.     /**
  143.      * This method is used by RecordDefinition to release JdbcConnection's when the
  144.      * last instance of the RecordDefinition of a particular class has been finalized.
  145.      *
  146.      * @param   String          connectionIdentifier
  147.      */
  148.     public synchronized void releaseConnection(String connectionIdentifier)
  149.     {
  150.         JdbcConnection m_Connection = null;
  151.         if(m_ConnectionsTable.containsKey(connectionIdentifier)) {
  152.             int cont = ((Integer)m_ConnectionsCounter.get(connectionIdentifier)).intValue();
  153.             cont--;
  154.             m_Connection=(JdbcConnection)m_ConnectionsTable.get(connectionIdentifier);
  155.             m_ConnectionsCounter.remove(connectionIdentifier);
  156.             m_ConnectionsCounter.put(connectionIdentifier,new Integer(cont));
  157.             if (cont == 0) {
  158.                 try {
  159.                     if (!m_Connection.getSQLConnection().isClosed()) {
  160.                         m_Connection.setConnectionClosed(true);
  161.                     }
  162.                 }
  163.                 catch (Exception ex) {
  164.                     System.out.println(ex.getMessage());
  165.                 }
  166.             }
  167.         }
  168.     }
  169.  
  170. //PRIVATE SECTION
  171.     private static Hashtable m_ConnectionManagersTable = new Hashtable();
  172.     private Hashtable m_ConnectionsTable = new Hashtable();
  173.     private Hashtable m_ConnectionsCounter = new Hashtable();
  174.  
  175. }