You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@excalibur.apache.org by le...@apache.org on 2005/04/13 05:42:39 UTC

svn commit: r161151 - excalibur/trunk/components/datasource/src/java/org/apache/avalon/excalibur/datasource/AbstractJdbcConnection.java

Author: leif
Date: Tue Apr 12 20:42:38 2005
New Revision: 161151

URL: http://svn.apache.org/viewcvs?view=rev&rev=161151
Log:
If a connection is reused consistently at a high enough rate that the DB pinging feature would never be triggered, connection failures on a socket level would never be detected and the same bad connection would be returned over and over.  This was not a problem on MySQL as its Connection.isClosed method returns true in such cases.  Oracle on the other hand behaved badly here.  The isClosed method only returns true if the close method has been called.
The fix was to set a flag if any exception is thrown while calling a Connection method so that the DB will be pinged before the DB is allowed to be reused.  Ideally this should be done if a method on the Statement or PreparedStatement classes fail as well, but that is not easily done as they are not currently proxied as the Connection class is.

Modified:
    excalibur/trunk/components/datasource/src/java/org/apache/avalon/excalibur/datasource/AbstractJdbcConnection.java

Modified: excalibur/trunk/components/datasource/src/java/org/apache/avalon/excalibur/datasource/AbstractJdbcConnection.java
URL: http://svn.apache.org/viewcvs/excalibur/trunk/components/datasource/src/java/org/apache/avalon/excalibur/datasource/AbstractJdbcConnection.java?view=diff&r1=161150&r2=161151
==============================================================================
--- excalibur/trunk/components/datasource/src/java/org/apache/avalon/excalibur/datasource/AbstractJdbcConnection.java (original)
+++ excalibur/trunk/components/datasource/src/java/org/apache/avalon/excalibur/datasource/AbstractJdbcConnection.java Tue Apr 12 20:42:38 2005
@@ -55,6 +55,10 @@
 
     private Object m_proxy;
     protected Pool m_pool;
+    
+    /** Flag to keep track of whether or not an error has been thrown since the last
+     *   time the DB was pinged. */
+    protected boolean m_encounteredError;
 
     /** The maximum time since a connection was last used before it will be pinged. */
     protected int m_testAge;
@@ -209,27 +213,66 @@
             return true;
         }
 
-        long age = System.currentTimeMillis() - m_lastUsed;
-
-        // If the connection has not been used for for longer than the keep alive age,
-        //  then make sure it is still alive.
-        if ( ( m_testStatement != null ) && ( age > m_testAge ) )
+        if ( m_testStatement == null )
         {
-            if( getLogger().isDebugEnabled() )
+            // No test statement was configured so it is not possible to ping the DB to
+            //  revalidate the connection.
+            if ( m_encounteredError )
             {
-                getLogger().debug( "Pinging database after " + age + "ms of inactivity." );
+                // There is no way to guarantee that this connection is still good.
+                //  Cause the connection to be invalidated.
+                return true;
             }
-
-            try
+        }
+        else
+        {
+            // A ping statement was configured.
+            long age = System.currentTimeMillis() - m_lastUsed;
+            boolean ping = false;
+            if ( age > m_testAge )
             {
-                ResultSet rs = m_testStatement.executeQuery();
-                rs.close();
+                if( getLogger().isDebugEnabled() )
+                {
+                    getLogger().debug( "Pinging database after " + age + "ms of inactivity." );
+                }
+                ping = true;
             }
-            catch( final SQLException se )
+            else if ( m_encounteredError )
             {
-                getLogger().debug( "Ping of connection failed.", se );
-                this.dispose();
-                return true;
+                // If an error was encountered since the last time the DB has been pinged we want
+                //  to force a ping.
+                // If a client is attempting to query a DB once every couple seconds then the
+                //  connection will never be unused for long enough to cause a ping on its own.
+                //  This means that if any reocurring error, like the socket being closed, occurrs
+                //  the pool would normally never invalidate the connection.  Ideally the underlying
+                //  Connection's isClosed() method should return false in such a case, but it does
+                //  not always do so. (Oracle for example)
+                //
+                // The encounteredError flag is only set if an error is thrown from a Connection
+                //  method.  It will not be set if an exception is thrown by a Statement or other
+                //  object obtained from the Connection.  We are mainly worried about low level
+                //  socket problems here however so this should not be a problem.  A failed query
+                //  caused by bad SQL does not mean the connection might fail for future calls.
+                getLogger().debug( "Pinging database after a previously thrown error." );
+                ping = true;
+            }
+            
+            // Always reset the flag here in case the connection was old and there was an error.
+            m_encounteredError = false;
+            
+            if( ping )
+            {
+                try
+                {
+                    ResultSet rs = m_testStatement.executeQuery();
+                    rs.close();
+                }
+                catch( final SQLException se )
+                {
+                    getLogger().debug( "Ping of connection failed.", se );
+                    this.dispose();
+                    return true;
+                }
             }
         }
 
@@ -345,6 +388,9 @@
     public Object invoke( Object proxy, Method method, Object[] args )
             throws Throwable
     {
+        // NOTE - getLogger() will return null in here before the enableLogging method
+        //  has been called.
+        
         Object retVal = null;
         Method executeMethod = (Method)m_methods.get( method.getName() );
 
@@ -361,6 +407,10 @@
         }
         catch( InvocationTargetException e )
         {
+            // Remember that an error of some sort was thrown so we can make sure to retest
+            //  the connection before it is once again used by a client.
+            m_encounteredError = true;
+            
             throw e.getTargetException();
         }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: scm-unsubscribe@excalibur.apache.org
For additional commands, e-mail: scm-help@excalibur.apache.org