sboag       01/03/15 09:50:07

  Modified:    java/src/org/apache/xalan/lib/sql ConnectionPool.java
                        DefaultConnectionPool.java ExtensionError.java
                        SQLExtensionError.java XConnection.java
                        XConnectionPoolManager.java XStatement.java
  Log:
  Update from John Gentilin <[EMAIL PROTECTED]> to
  fix connection cleanup.
  
  Revision  Changes    Path
  1.4       +7 -6      
xml-xalan/java/src/org/apache/xalan/lib/sql/ConnectionPool.java
  
  Index: ConnectionPool.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xalan/java/src/org/apache/xalan/lib/sql/ConnectionPool.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ConnectionPool.java       2001/03/12 02:04:05     1.3
  +++ ConnectionPool.java       2001/03/15 17:50:02     1.4
  @@ -78,13 +78,14 @@
   public interface ConnectionPool
   {
     /**
  -   * The Active Flag determines if the ConnectionPool is usable
  -   * If the Connection Pool is inactive, as connections are released
  -   * they will also be closed, so the min number of connections will
  -   * slowly be diminised.
  +   * The Pool can be Enabled and Disabled. Disabling the pool
  +   * closes all the outstanding Unused connections and any new
  +   * connections will be closed upon release.
  +   *
      */
  -  public void     setActive(boolean flag);
  -  public boolean  getActive();
  +  public void     enablePool();
  +  public void     disablePool();
  +  public boolean  isEnabled();
   
     /**
      * The Driver and URL are the only required parmeters.
  
  
  
  1.4       +39 -17    
xml-xalan/java/src/org/apache/xalan/lib/sql/DefaultConnectionPool.java
  
  Index: DefaultConnectionPool.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xalan/java/src/org/apache/xalan/lib/sql/DefaultConnectionPool.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- DefaultConnectionPool.java        2001/03/12 02:04:05     1.3
  +++ DefaultConnectionPool.java        2001/03/15 17:50:03     1.4
  @@ -69,7 +69,7 @@
   
   public class DefaultConnectionPool implements ConnectionPool
   {
  -  private final static boolean DEBUG = true;
  +  private final static boolean DEBUG = false;
   
     /**
      * The basic information to make a JDBC Connection
  @@ -99,7 +99,7 @@
     /**
      * Storage for the PooledConnections
      */
  -  private Vector      m_pool = null;
  +  private Vector      m_pool = new Vector();
   
     /**
      * Are we active ??
  @@ -114,15 +114,21 @@
      *
      * @param <code>boolean flag</code>, Set the active flag.
      */
  -  public void setActive(boolean flag)
  +  public void disablePool()
     {
  -    m_IsActive = flag;
  +    m_IsActive = false;
  +    freeUnused();
     }
   
  +  public void enablePool()
  +  {
  +    m_IsActive = true;
  +  }
  +
     /**
      * Return our current Active state
      */
  -  public boolean getActive()
  +  public boolean isEnabled()
     {
       return m_IsActive;
     }
  @@ -233,6 +239,11 @@
     {
       try
       {
  +      if (DEBUG)
  +      {
  +        System.out.println("Testing Connection");
  +      }
  +
         Connection conn = getConnection();
   
         if (DEBUG)
  @@ -249,10 +260,21 @@
   
         releaseConnection(conn);
   
  +      if (DEBUG)
  +      {
  +        System.out.println("Testing Connection, SUCCESS");
  +      }
  +
         return true;
       }
       catch(Exception e)
       {
  +      if (DEBUG)
  +      {
  +        System.out.println("Testing Connection, FAILED");
  +        e.printStackTrace();
  +      }
  +
         return false;
       }
   
  @@ -266,7 +288,11 @@
   
       PooledConnection pcon = null;
   
  -    if (m_pool == null) { initializePool(); }
  +    // We will fill up the pool any time it is less than the
  +    // Minimum. THis could be cause by the enableing and disabling
  +    // or the pool.
  +    //
  +    if ( m_pool.size() < m_PoolMinSize ) { initializePool(); }
   
       // find a connection not in use
       for ( int x = 0; x < m_pool.size(); x++ )
  @@ -324,7 +350,7 @@
             System.out.println("Releasing Connection " + x);
           }
   
  -        if (getActive() == false)
  +        if (! isEnabled())
           {
             con.close();
             m_pool.removeElementAt(x);
  @@ -339,7 +365,6 @@
             // Set it's inuse attribute to false, which
             // releases it for use
             pcon.setInUse(false);
  -          pcon.close();
           }
   
           break;
  @@ -364,6 +389,7 @@
     public synchronized void initializePool()
       throws IllegalArgumentException, SQLException
     {
  +
        // Check our initial values
        if ( m_driver == null )
        {
  @@ -392,9 +418,12 @@
          throw new IllegalArgumentException("Invalid Driver Name Specified!");
        }
   
  +     // IF we are not active, don't actuall build a pool yet
  +     // Just set up the driver and periphal items.
  +     if ( !m_IsActive) return;
   
       // Create Connections based on the size member
  -    for ( int x = 0; x < m_PoolMinSize; x++ )
  +    do
       {
   
         Connection con = createConnection();
  @@ -412,19 +441,12 @@
           if (DEBUG) System.out.println("Adding DB Connection to the Pool");
         }
       }
  +    while (m_pool.size() < m_PoolMinSize);
     }
   
     // Adds the PooledConnection to the pool
     private void addConnection(PooledConnection value)
     {
  -
  -    // If the pool is null, create a new vector
  -    // with the initial size of "size"
  -    if ( m_pool == null )
  -    {
  -      m_pool = new Vector( m_PoolMinSize);
  -    }
  -
       // Add the PooledConnection Object to the vector
       m_pool.addElement(value);
     }
  
  
  
  1.5       +41 -40    
xml-xalan/java/src/org/apache/xalan/lib/sql/ExtensionError.java
  
  Index: ExtensionError.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xalan/java/src/org/apache/xalan/lib/sql/ExtensionError.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ExtensionError.java       2001/03/12 22:20:07     1.4
  +++ ExtensionError.java       2001/03/15 17:50:03     1.5
  @@ -74,10 +74,6 @@
   import org.w3c.dom.traversal.NodeFilter;
   import org.w3c.dom.traversal.NodeIterator;
   
  -// Imported Serializer classes
  -//import org.apache.xml.serialize.OutputFormat;
  -//import org.apache.xml.serialize.Serializer;
  -//import org.apache.xml.serialize.XMLSerializer;
   import java.io.StringWriter;
   
   import java.io.ByteArrayOutputStream;
  @@ -148,27 +144,29 @@
   
         etmp = m_doc.createElement("message");
         info.appendChild(etmp);
  -      text = m_doc.createTextNode(err.getMessage());
  +      text = m_doc.createTextNode(err.getLocalizedMessage());
         etmp.appendChild(text);
   
  -      ByteArrayOutputStream bos = new ByteArrayOutputStream();
  -      PrintStream ps = new PrintStream(bos);
  -      err.printStackTrace(ps);
  -      String stack = bos.toString();
  +      // System.out.println("MESSAGE[" + err.getLocalizedMessage() + "]");
   
  +      //ByteArrayOutputStream bos = new ByteArrayOutputStream();
  +      //PrintStream ps = new PrintStream(bos);
  +      //err.printStackTrace(ps);
  +      //String stack = bos.toString();
  +
         // System.out.println(stack);
   
  -      etmp = m_doc.createElement("stack");
  -      info.appendChild(etmp);
  -      cdata = m_doc.createCDATASection(stack);
  -      etmp.appendChild(text);
  +      //etmp = m_doc.createElement("stack");
  +      //info.appendChild(etmp);
  +      //cdata = m_doc.createCDATASection(stack);
  +      //etmp.appendChild(text);
   
         populateSpecificData(m_doc, root);
   
       }
       catch(Exception e)
       {
  -      e.printStackTrace();
  +      // e.printStackTrace();
   
         m_doc = null;
       }
  @@ -271,33 +269,36 @@
     {
     }
   
  -//   public void dump()
  -//   {
   
  -//     try
  -//     {
  -//       //Serialize DOM
  -//       OutputFormat                format  = new OutputFormat();
  -//       //Writer will be a String
  -//       StringWriter                stringOut = new StringWriter();
  -
  -//       XMLSerializer               serial = new XMLSerializer( stringOut, 
format );
  -
  -//       // As a DOM Serializer
  -//       serial.asDOMSerializer();
  -
  -//       Element e = m_doc.getDocumentElement();
  -//       serial.serialize(e);
  -//       System.out.println("Extension Error:");
  -//       String display = stringOut.toString();
  -//       System.out.println( display );
  -//     }
  -//     catch(Exception e)
  -//     {
  -//       // Empty
  -//     }
  +  /*
  +  public void dump()
  +  {
  +
  +    try
  +    {
  +      //Serialize DOM
  +      OutputFormat           format  = new OutputFormat();
  +      //Writer will be a String
  +      StringWriter           stringOut = new StringWriter();
  +
  +      XMLSerializer          serial = new XMLSerializer( stringOut, format );
  +
  +      // As a DOM Serializer
  +      serial.asDOMSerializer();
  +
  +      Element e = m_doc.getDocumentElement();
  +      serial.serialize(e);
  +      System.out.println("Extension Error:");
  +      String display = stringOut.toString();
  +      System.out.println( display );
  +    }
  +    catch(Exception e)
  +    {
  +      // Empty
  +    }
   
  -//   }
  +  }
  +  */
   
     public Node getCurrentNode()
     {
  @@ -360,4 +361,4 @@
     public void setLast(int last)
     {
     }
  -}
  +}
  \ No newline at end of file
  
  
  
  1.4       +3 -3      
xml-xalan/java/src/org/apache/xalan/lib/sql/SQLExtensionError.java
  
  Index: SQLExtensionError.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xalan/java/src/org/apache/xalan/lib/sql/SQLExtensionError.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- SQLExtensionError.java    2001/03/12 02:04:08     1.3
  +++ SQLExtensionError.java    2001/03/15 17:50:04     1.4
  @@ -100,7 +100,7 @@
       n.appendChild(root);
   
   
  -    Element code = doc.createElement("error_code");
  +    Element code = doc.createElement("error-code");
       root.appendChild(code);
   
       int ecode = m_sql_ex.getErrorCode();
  @@ -114,8 +114,8 @@
       text = doc.createTextNode(m_sql_ex.getSQLState());
       state.appendChild(text);
   
  -    m_sql_ex = m_sql_ex.getNextException();
  -    if ( null != m_sql_ex ) populateSpecificData(doc, n);
  +    // m_sql_ex = m_sql_ex.getNextException();
  +    // if ( null != m_sql_ex ) populateSpecificData(doc, n);
     }
   
   }
  
  
  
  1.10      +129 -10   
xml-xalan/java/src/org/apache/xalan/lib/sql/XConnection.java
  
  Index: XConnection.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xalan/java/src/org/apache/xalan/lib/sql/XConnection.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- XConnection.java  2001/03/12 02:04:09     1.9
  +++ XConnection.java  2001/03/15 17:50:04     1.10
  @@ -93,7 +93,7 @@
     /**
      * Flag for DEBUG mode
      */
  -  private static final boolean DEBUG = true;
  +  private static final boolean DEBUG = false;
   
   
     /**
  @@ -104,9 +104,23 @@
     /**
      * Reference to the ConnectionPool used
      */
  -  public ConnectionPool m_ConnectionPool;
  +  private ConnectionPool  m_ConnectionPool = null;
  +  private String          m_ConnectionPoolName;
   
  +  private boolean         m_IsDefaultPool = false;
  +  private boolean         m_DefaultPoolingEnabled = false;
   
  +
  +  /**
  +   * Let's keep a copy of the ConnectionPoolMgr in
  +   * alive here so we are keeping the static pool alive
  +   *
  +   * We will also use this Pool Manager to register our default pools.
  +   *
  +   */
  +
  +   private XConnectionPoolManager m_PoolMgr = new XConnectionPoolManager();
  +
     /**
      * For PreparedStatements, we need a place to
      * to store the parameters in a vector.
  @@ -155,8 +169,12 @@
     {
       try
       {
  -      XConnectionPoolManager mgr = new XConnectionPoolManager();
  -      m_ConnectionPool = mgr.getPool(ConnPoolName);
  +      if ((m_ConnectionPool != null) && (m_IsDefaultPool))
  +        m_PoolMgr.removePool(m_ConnectionPoolName);
  +
  +      m_ConnectionPool = m_PoolMgr.getPool(ConnPoolName);
  +      m_ConnectionPoolName = ConnPoolName;
  +
         m_connection = m_ConnectionPool.getConnection();
       }
       catch(SQLException e)
  @@ -436,30 +454,74 @@
     private void init(String driver, String dbURL, Properties prop)
       throws SQLException
     {
  +    if (DEBUG)
  +      System.out.println("XConnection, Connection Init");
  +
       String user = prop.getProperty("user");
       if (user == null) user = "";
   
       String passwd = prop.getProperty("password");
       if (passwd == null) passwd = "";
   
  +
       String poolName = driver + dbURL + user + passwd;
  +    ConnectionPool cpool = m_PoolMgr.getPool(poolName);
  +
  +    // We are limited to only one Default Connection Pool
  +    // at a time.
  +    // If we are creating a new Default Pool, release the first
  +    // One so it is not lost when we close the Stylesheet
  +    if (
  +        (m_ConnectionPool != null) &&
  +        (m_IsDefaultPool) &&
  +        (cpool != m_ConnectionPool))
  +    {
  +      m_PoolMgr.removePool(m_ConnectionPoolName);
  +    }
   
  -    XConnectionPoolManager mgr = new XConnectionPoolManager();
  -    m_ConnectionPool = mgr.getPool(poolName);
  -    if (m_ConnectionPool == null)
  +    if (cpool == null)
       {
  +
  +      if (DEBUG)
  +      {
  +        System.out.println("XConnection, Creating Connection");
  +        System.out.println(" Driver  :" + driver);
  +        System.out.println(" URL     :" + dbURL);
  +        System.out.println(" user    :" + user);
  +        System.out.println(" passwd  :" + passwd);
  +      }
  +
  +
         DefaultConnectionPool defpool = new DefaultConnectionPool();
  +
  +      if ((DEBUG) && (defpool == null))
  +        System.out.println("Failed to Create a Default Connection Pool");
  +
         defpool.setDriver(driver);
         defpool.setURL(dbURL);
         defpool.setProtocol(prop);
  -      defpool.setActive(true);
   
  -      mgr.registerPool(poolName, defpool);
   
  +      // Only enable pooling in the default pool if we are explicatly
  +      // told too.
  +      if (m_DefaultPoolingEnabled) defpool.enablePool();
  +
  +      m_PoolMgr.registerPool(poolName, defpool);
  +
         m_ConnectionPool = defpool;
  +      m_ConnectionPoolName = poolName;
       }
  +    else
  +    {
  +      if (DEBUG)
  +        System.out.println("Default Connection already existed");
   
  +      m_ConnectionPool = cpool;
  +      m_ConnectionPoolName = poolName;
  +    }
   
  +    m_ConnectionPool.testConnection();
  +
       m_connection = m_ConnectionPool.getConnection();
     }
   
  @@ -484,11 +546,23 @@
       }
       catch(SQLException e)
       {
  +      if (DEBUG)
  +      {
  +        System.out.println("SQL Exception in Query");
  +        e.printStackTrace();
  +      }
  +
         SQLExtensionError err = new SQLExtensionError(e);
         return err;
       }
       catch (Exception e)
       {
  +      if (DEBUG)
  +      {
  +        System.out.println("Exception in Query");
  +        e.printStackTrace();
  +      }
  +
         ExtensionError err = new ExtensionError(e);
         return err;
       }
  @@ -691,6 +765,51 @@
       } while ( (n = n.getNextSibling()) != null);
     }
   
  +
  +  /**
  +   *
  +   * There is a problem with some JDBC drivers when a Connection
  +   * is open and the JVM shutsdown. If there is a problem, there
  +   * is no way to control the currently open connections in the
  +   * pool. So for the default connection pool, the actuall pooling
  +   * mechinsm is disabled by default. The Stylesheet designer can
  +   * re-enabled pooling to take advantage of connection pools.
  +   * The connection pool can even be disabled which will close all
  +   * outstanding connections.
  +   *
  +   *
  +   */
  +  public void enableDefaultConnectionPool()
  +  {
  +
  +    if (DEBUG)
  +      System.out.println("Enabling Default Connection Pool");
  +
  +    m_DefaultPoolingEnabled = true;
  +
  +    if (m_ConnectionPool == null) return;
  +    if (!m_IsDefaultPool) return;
  +
  +    m_ConnectionPool.enablePool();
  +
  +  }
  +
  +  /**
  +   * See enableDefaultConnectionPool
  +   */
  +  public void disableDefaultConnectionPool()
  +  {
  +    if (DEBUG)
  +      System.out.println("Disabling Default Connection Pool");
  +
  +    m_DefaultPoolingEnabled = false;
  +
  +    if (m_ConnectionPool == null) return;
  +    if (!m_IsDefaultPool) return;
  +
  +    m_ConnectionPool.disablePool();
  +  }
  +
     /**
      * Close the connection to the data source.
      *
  @@ -714,7 +833,7 @@
         {
           // something is wrong here, we have a connection
           // but no controlling pool, close it anyway the
  -        // error will show up as an exeption elsewhere.
  +        // error will show up as an excpeion elsewhere
           m_connection.close();
         }
       }
  
  
  
  1.4       +4 -7      
xml-xalan/java/src/org/apache/xalan/lib/sql/XConnectionPoolManager.java
  
  Index: XConnectionPoolManager.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xalan/java/src/org/apache/xalan/lib/sql/XConnectionPoolManager.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- XConnectionPoolManager.java       2001/03/12 02:04:10     1.3
  +++ XConnectionPoolManager.java       2001/03/15 17:50:04     1.4
  @@ -118,7 +118,7 @@
      * if a pool with the same name currently exists.
      *
      */
  -  public void registerPool(String name, ConnectionPool pool)
  +  public synchronized void registerPool(String name, ConnectionPool pool)
     {
       if ( m_poolTable.containsKey(name) )
       {
  @@ -136,7 +136,7 @@
      * @param <code>String name</code> name of the pool to remove.
      *
      */
  -  public void removePool(String name)
  +  public synchronized void removePool(String name)
     {
       ConnectionPool pool = getPool(name);
   
  @@ -146,11 +146,8 @@
         // Disable future use of this pool under the Xalan
         // extension only. This flag should only exist in the
         // wrapper and not in the actual pool implementation.
  -      pool.setActive(false);
  +      pool.disablePool();
   
  -      //
  -      // Let's trim down the number of connections that are in use
  -      pool.freeUnused();
   
         //
         // Remove the pool from the Hashtable if we don'd have
  @@ -172,7 +169,7 @@
      * null
      *
      */
  -  public ConnectionPool getPool(String name)
  +  public synchronized ConnectionPool getPool(String name)
     {
       return (ConnectionPool) m_poolTable.get(name);
     }
  
  
  
  1.12      +11 -11    
xml-xalan/java/src/org/apache/xalan/lib/sql/XStatement.java
  
  Index: XStatement.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xalan/java/src/org/apache/xalan/lib/sql/XStatement.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- XStatement.java   2001/03/12 02:04:10     1.11
  +++ XStatement.java   2001/03/15 17:50:05     1.12
  @@ -93,7 +93,7 @@
   {
   
     /** Flag for DEBUG mode          */
  -  private static final boolean DEBUG = true;
  +  private static final boolean DEBUG = false; 
   
     /**
      * Ths JDBC Statement that is used for the query.
  @@ -259,58 +259,58 @@
         stmt.setString(pos, p.getValue());
       }
   
  -    else if (type.equalsIgnoreCase("bigdecimal"))
  +    if (type.equalsIgnoreCase("bigdecimal"))
       {
         stmt.setBigDecimal(pos, new BigDecimal(p.getValue()));
       }
   
  -    else if (type.equalsIgnoreCase("boolean"))
  +    if (type.equalsIgnoreCase("boolean"))
       {
         Integer i = new Integer( p.getValue() );
         boolean b = ((i.intValue() != 0) ? false : true);
         stmt.setBoolean(pos, b);
       }
   
  -    else if (type.equalsIgnoreCase("bytes"))
  +    if (type.equalsIgnoreCase("bytes"))
       {
         stmt.setBytes(pos, p.getValue().getBytes());
       }
   
  -    else if (type.equalsIgnoreCase("date"))
  +    if (type.equalsIgnoreCase("date"))
       {
         stmt.setDate(pos, Date.valueOf(p.getValue()));
       }
   
  -    else if (type.equalsIgnoreCase("double"))
  +    if (type.equalsIgnoreCase("double"))
       {
         Double d = new Double(p.getValue());
         stmt.setDouble(pos, d.doubleValue() );
       }
   
  -    else if (type.equalsIgnoreCase("float"))
  +    if (type.equalsIgnoreCase("float"))
       {
         Float f = new Float(p.getValue());
         stmt.setFloat(pos, f.floatValue());
       }
   
  -    else if (type.equalsIgnoreCase("long"))
  +    if (type.equalsIgnoreCase("long"))
       {
         Long l = new Long(p.getValue());
         stmt.setLong(pos, l.longValue());
       }
   
  -    else if (type.equalsIgnoreCase("short"))
  +    if (type.equalsIgnoreCase("short"))
       {
         Short s = new Short(p.getValue());
         stmt.setShort(pos, s.shortValue());
       }
   
  -    else if (type.equalsIgnoreCase("time"))
  +    if (type.equalsIgnoreCase("time"))
       {
         stmt.setTime(pos, Time.valueOf(p.getValue()) );
       }
   
  -    else if (type.equalsIgnoreCase("timestamp"))
  +    if (type.equalsIgnoreCase("timestamp"))
       {
   
         stmt.setTimestamp(pos, Timestamp.valueOf(p.getValue()) );
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to