commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmcna...@apache.org
Subject cvs commit: jakarta-commons-sandbox/jdbc2pool/src/java/org/apache/commons/jdbc2pool/adapter PooledConnectionImpl.java
Date Wed, 03 Apr 2002 05:10:47 GMT
jmcnally    02/04/02 21:10:47

  Modified:    jdbc2pool/src/java/org/apache/commons/jdbc2pool
                        CPDSConnectionFactory.java Jdbc2PoolDataSource.java
               jdbc2pool/src/java/org/apache/commons/jdbc2pool/adapter
                        PooledConnectionImpl.java
  Log:
  o handle the exceptions thrown by ObjectPool
  
  o make sure that one and only one of setDataSourceName or setConnectionPoolDataSource
  is called before getConnection is called.
  
  Revision  Changes    Path
  1.2       +37 -11    jakarta-commons-sandbox/jdbc2pool/src/java/org/apache/commons/jdbc2pool/CPDSConnectionFactory.java
  
  Index: CPDSConnectionFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jdbc2pool/src/java/org/apache/commons/jdbc2pool/CPDSConnectionFactory.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- CPDSConnectionFactory.java	4 Mar 2002 07:52:37 -0000	1.1
  +++ CPDSConnectionFactory.java	3 Apr 2002 05:10:46 -0000	1.2
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-commons-sandbox/jdbc2pool/src/java/org/apache/commons/jdbc2pool/CPDSConnectionFactory.java,v
1.1 2002/03/04 07:52:37 jmcnally Exp $
  - * $Revision: 1.1 $
  - * $Date: 2002/03/04 07:52:37 $
  + * $Header: /home/cvs/jakarta-commons-sandbox/jdbc2pool/src/java/org/apache/commons/jdbc2pool/CPDSConnectionFactory.java,v
1.2 2002/04/03 05:10:46 jmcnally Exp $
  + * $Revision: 1.2 $
  + * $Date: 2002/04/03 05:10:46 $
    *
    * ====================================================================
    *
  @@ -77,7 +77,7 @@
    * {*link PoolableConnection}s.
    *
    * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a>
  - * @version $Id: CPDSConnectionFactory.java,v 1.1 2002/03/04 07:52:37 jmcnally Exp $
  + * @version $Id: CPDSConnectionFactory.java,v 1.2 2002/04/03 05:10:46 jmcnally Exp $
    */
   public class CPDSConnectionFactory 
       implements PoolableObjectFactory, ConnectionEventListener {
  @@ -125,9 +125,25 @@
        * Sets the {*link ObjectPool} in which to pool {*link Connection}s.
        * @param pool the {*link ObjectPool} in which to pool those {*link Connection}s
        */
  -    synchronized public void setPool(ObjectPool pool) {
  +    synchronized public void setPool(ObjectPool pool) 
  +        throws SQLException
  +    {
           if(null != _pool && pool != _pool) {
  -            _pool.close();
  +            try
  +            {
  +                _pool.close();
  +            }
  +            catch (Exception e)
  +            {
  +                if (e instanceof RuntimeException) 
  +                {
  +                    throw (RuntimeException)e;
  +                }
  +                else 
  +                {
  +                    throw new SQLException(e.getMessage());
  +                }
  +            }
           }
           _pool = pool;
       }
  @@ -172,10 +188,7 @@
           }
       }
   
  -    // FIXME! Connection from the PooledConnection must be closed before
  -    // another one can be requested and closing it will send an event
  -    // to the pool
  -    synchronized public boolean validateObject(Object obj) {
  +    public boolean validateObject(Object obj) {
           boolean valid = false;
           if(obj instanceof PooledConnection) {
               PooledConnection pconn = (PooledConnection)obj;
  @@ -184,6 +197,10 @@
                   Connection conn = null;
                   Statement stmt = null;
                   ResultSet rset = null;
  +                // logical Connection from the PooledConnection must be closed
  +                // before another one can be requested and closing it will 
  +                // generate an event. Keep track so we know not to return
  +                // the PooledConnection 
                   validatingMap.put(pconn, null);
                   try {
                       conn = pconn.getConnection();
  @@ -248,7 +265,16 @@
           // otherwise return the connection to the pool.
           if (!validatingMap.containsKey(pc)) 
           {
  -            _pool.returnObject(pc);
  +            try
  +            {
  +                _pool.returnObject(pc);
  +            }
  +            catch (Exception e)
  +            {
  +                destroyObject(pc);
  +                System.err.println("CLOSING DOWN CONNECTION AS IT COULD " + 
  +                                   "NOT BE RETURNED TO THE POOL");
  +            }
           }
       }
   
  
  
  
  1.5       +44 -5     jakarta-commons-sandbox/jdbc2pool/src/java/org/apache/commons/jdbc2pool/Jdbc2PoolDataSource.java
  
  Index: Jdbc2PoolDataSource.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jdbc2pool/src/java/org/apache/commons/jdbc2pool/Jdbc2PoolDataSource.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- Jdbc2PoolDataSource.java	18 Mar 2002 06:49:32 -0000	1.4
  +++ Jdbc2PoolDataSource.java	3 Apr 2002 05:10:46 -0000	1.5
  @@ -91,7 +91,7 @@
    * ConnectionPoolDataSource.
    *
    * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a>
  - * @version $Id: Jdbc2PoolDataSource.java,v 1.4 2002/03/18 06:49:32 jmcnally Exp $
  + * @version $Id: Jdbc2PoolDataSource.java,v 1.5 2002/04/03 05:10:46 jmcnally Exp $
    */
   public class Jdbc2PoolDataSource
       implements DataSource, Referenceable, Serializable, ObjectFactory
  @@ -179,7 +179,17 @@
        */
       public void setConnectionPoolDataSource(ConnectionPoolDataSource  v) 
       {
  +        assertInitializationAllowed();
  +        if (dataSourceName != null) 
  +        {
  +            throw new IllegalStateException(
  +                "Cannot set the DataSource, if JNDI is used.");
  +        }
           this.cpds = v;
  +        if (isNew) 
  +        {
  +            registerInstance();
  +        }
       }
   
       /**
  @@ -203,7 +213,18 @@
        */
       public void setDataSourceName(String  v) 
       {
  +        assertInitializationAllowed();
  +        if (cpds != null) 
  +        {
  +            throw new IllegalStateException(
  +                "Cannot set the JNDI name for the DataSource, if already " +
  +                "set using setConnectionPoolDataSource.");
  +        }
           this.dataSourceName = v;
  +        if (isNew) 
  +        {
  +            registerInstance();
  +        }
       }
   
       
  @@ -789,11 +810,13 @@
       public Connection getConnection(String username, String password)
           throws SQLException
       {
  -        getConnectionCalled = true;
           if (isNew) 
           {
  -            registerInstance();
  +            throw new SQLException("Must set the ConnectionPoolDataSource " + 
  +                "through setDataSourceName or setConnectionPoolDataSource " + 
  +                "before calling getConnection.");
           }
  +        getConnectionCalled = true;
           Map pools = (Map)dsInstanceMap.get(instanceKey);
           String key = getKey(username);
           ObjectPool pool = (ObjectPool)pools.get(key);
  @@ -810,8 +833,23 @@
               }
           }
   
  -        Connection con = 
  -            ((PooledConnection)pool.borrowObject()).getConnection();
  +        PooledConnection pc = null;
  +        try
  +        {
  +            pc = (PooledConnection)pool.borrowObject();
  +        }
  +        catch (Exception e)
  +        {
  +            if (e instanceof RuntimeException) 
  +            {
  +                throw (RuntimeException)e;
  +            }
  +            else 
  +            {
  +                throw new SQLException(e.getMessage());
  +            }
  +        }
  +        Connection con = pc.getConnection();
   
           boolean defaultAutoCommit = isDefaultAutoCommit();
           if ( username != null ) 
  @@ -849,6 +887,7 @@
           }
           return key;
       }
  +
   
       synchronized private void registerInstance()
       {
  
  
  
  1.4       +26 -5     jakarta-commons-sandbox/jdbc2pool/src/java/org/apache/commons/jdbc2pool/adapter/PooledConnectionImpl.java
  
  Index: PooledConnectionImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jdbc2pool/src/java/org/apache/commons/jdbc2pool/adapter/PooledConnectionImpl.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- PooledConnectionImpl.java	4 Mar 2002 07:50:15 -0000	1.3
  +++ PooledConnectionImpl.java	3 Apr 2002 05:10:47 -0000	1.4
  @@ -78,7 +78,7 @@
    * PooledConnectionDataSource.
    *
    * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a>
  - * @version $Id: PooledConnectionImpl.java,v 1.3 2002/03/04 07:50:15 jmcnally Exp $
  + * @version $Id: PooledConnectionImpl.java,v 1.4 2002/04/03 05:10:47 jmcnally Exp $
    */
   public class PooledConnectionImpl 
       implements PooledConnection, KeyedPoolableObjectFactory
  @@ -144,11 +144,32 @@
           throws SQLException
       {        
           isClosed = true;
  -        if(null != _pstmtPool) {
  -            _pstmtPool.close();
  -            _pstmtPool = null;
  +        if(null != _pstmtPool) 
  +        {
  +            try
  +            {
  +                try
  +                {
  +                    _pstmtPool.close();
  +                }
  +                finally
  +                {
  +                    _pstmtPool = null;
  +                    connection.close();
  +                }
  +            }
  +            catch (Exception e)
  +            {
  +                if (e instanceof RuntimeException) 
  +                {
  +                    throw (RuntimeException)e;
  +                }
  +                else 
  +                {
  +                    throw new SQLException(e.getMessage());
  +                }
  +            }
           }
  -        connection.close();
       }
   
       /**
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message