cmlenz      02/02/25 12:44:09

  Modified:    src/stores/slidestore/reference Tag: SLIDE_1_0
                        JDBCContentStore.java JDBCDescriptorsStore.java
  Added:       src/stores/slidestore/reference/util Tag: SLIDE_1_0
                        JDBCAwareInputStream.java
  Log:
  Porting bugfixes/enhancements from the HEAD branch:
  - Committed by dirkv, 02/01/02 13:16:48
    "helper class to solve bug 5582
     submitted by Chris Kimpton"
  - Committed by dirkv, 02/01/02 13:17:39
    "solve bug 5582: JDBCContentStore.retrieveRevisionContent not closing
     connections
     submitted by Chris Kimpton"
  - Committed by dirkv, 02/01/06 16:44:54
    "performance improvement
     patch by Colin Britton"
  - Committed by dirkv, 02/01/12 08:04:26
    "- reconnect when connection is lost
     - isConnected includes test with sql statement"
  - Committed by remm, 02/01/16 15:43:53
    "- Add missing statement close.
     - Thanks to Jason Harrop for the patch."
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.12.2.1  +77 -28    
jakarta-slide/src/stores/slidestore/reference/JDBCContentStore.java
  
  Index: JDBCContentStore.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-slide/src/stores/slidestore/reference/JDBCContentStore.java,v
  retrieving revision 1.12
  retrieving revision 1.12.2.1
  diff -u -r1.12 -r1.12.2.1
  --- JDBCContentStore.java     22 Sep 2001 12:53:12 -0000      1.12
  +++ JDBCContentStore.java     25 Feb 2002 20:44:09 -0000      1.12.2.1
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-slide/src/stores/slidestore/reference/JDBCContentStore.java,v 1.12 
2001/09/22 12:53:12 dirkv Exp $
  - * $Revision: 1.12 $
  - * $Date: 2001/09/22 12:53:12 $
  + * $Header: 
/home/cvs/jakarta-slide/src/stores/slidestore/reference/JDBCContentStore.java,v 
1.12.2.1 2002/02/25 20:44:09 cmlenz Exp $
  + * $Revision: 1.12.2.1 $
  + * $Date: 2002/02/25 20:44:09 $
    *
    * ====================================================================
    *
  @@ -73,7 +73,6 @@
   import java.io.OutputStream;
   import java.io.FileInputStream;
   import java.io.FileOutputStream;
  -import java.io.InputStreamReader;
   import java.io.IOException;
   import java.io.File;
   import java.sql.*;
  @@ -85,12 +84,13 @@
   import org.apache.slide.security.*;
   import org.apache.slide.content.*;
   import org.apache.slide.util.logger.Logger;
  +import slidestore.reference.util.JDBCAwareInputStream;
   
   /**
    * JDBC 2.0 compliant implementation of ContentStore.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Remy Maucherat</a>
  - * @version $Revision: 1.12 $
  + * @version $Revision: 1.12.2.1 $
    */
   public class JDBCContentStore extends AbstractSimpleService
       implements ContentStore {
  @@ -100,7 +100,10 @@
       
       
       public static final int BUFFER_SIZE = 2048;
  +
  +    /** @deprecated */ // FIXME: remove this
       public static final String CHARACTER_ENCODING = "8859_1";
  +    
       protected static final int REVISION_URI = 1;
       protected static final int REVISION_NUMBER = 2;
       protected static final int REVISION_CONTENT = 3;
  @@ -157,6 +160,14 @@
       
           return statements;
       }
  +
  +    /**
  +     * Returns a sql statement that can be used to test the database connection.
  +     */
  +    protected String getDatabaseConnectionTestStatement() 
  +    {
  +        return "select 1 from revisioncontent where uri is null";
  +    }
       
       /**
        * Read parameters.
  @@ -260,6 +271,17 @@
           }
       }
       
  +    /**
  +     * Tries to reconnect if needed but doesn't report failure.
  +     */
  +    protected synchronized void connectIfNeededAndPossible() {
  +        try  {
  +            connectIfNeeded();
  +        }
  +        catch (Throwable ex) {
  +            // ignore
  +        }
  +    }
       
       /**
        * Initializes content store.
  @@ -335,15 +357,26 @@
        * @return boolean true if we are connected
        * @exception ServiceAccessException Service access error
        */
  -    public boolean isConnected()
  -        throws ServiceAccessException {
  -        try {
  -            return ((connection != null) && (!connection.isClosed()));
  -        } catch (SQLException e) {
  -            throw new ServiceAccessException(this, e);
  -        }
  -    }
  -    
  +     public boolean isConnected()
  +         throws ServiceAccessException {
  +         try {
  +             if ((connection == null) || (connection.isClosed())) {
  +                 return false;
  +             }
  +             
  +             PreparedStatement statement = 
  +                 connection.prepareStatement(getDatabaseConnectionTestStatement());
  +             statement.executeQuery();
  +             statement.close();
  +             
  +             // testStatement executed without throwing an exception
  +             return true;
  +             
  +         } catch (SQLException e) {
  +             throw new ServiceAccessException(this, e);
  +         }
  +     }
  +   
       
       // ----------------------------------------------------- XAResource Methods
       
  @@ -390,10 +423,14 @@
           if (!alreadyEnlisted) 
           {
               try {
  -//                getLogger().log("start",LOG_CHANNEL,Logger.DEBUG);
  +                // getLogger().log("start",LOG_CHANNEL,Logger.DEBUG);
  +
  +                // test connection
  +                connectIfNeeded();
  +                
                   // discard changes made outside a tranaction
                   connection.rollback();
  -            } catch (SQLException e) {
  +            } catch (Exception e) {
                   throw new XAException(XAException.XAER_RMERR);
               }
               alreadyEnlisted=true;
  @@ -442,26 +479,25 @@
                       (uri.toString(),
                        revisionDescriptor.getRevisionNumber());
               }
  -            InputStreamReader reader = new InputStreamReader
  -                (is, CHARACTER_ENCODING);
               result = new NodeRevisionContent();
  -            result.setContent(reader);
               result.setContent(is);
               
  -            // Don't close the statement or the result set here (because 
  -            // otherwise the is and the reader returned would be closed).
  -            // If this proves to be a problem, then the binary content of the 
  -            // resource must either be buffer to the disk or to memory.
  -            // FIXME ?
  +            // this input stream passes on the closure of itself onto the
  +            // jdbc statement and resultSet
  +            result.setContent( new JDBCAwareInputStream(is,selectStatement) );
               
           } catch (SQLException e) {
               getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
  +            connectIfNeededAndPossible();
               throw new ServiceAccessException(this, e.getMessage());
           } catch (RevisionNotFoundException e) {
  -            // we do NOT want this caught by next clause.
  -            throw e;
  +            getLogger().log("RevisionNotFoundException encountered for " +
  +                            revisionUri + " revision " + revisionNumber,
  +                            LOG_CHANNEL,Logger.WARNING);
  +            throw e; // we do NOT want this caught by next clause.
           } catch (Exception e) {
               getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
  +            connectIfNeededAndPossible();
               throw new ServiceAccessException(this, e.getMessage());
           }
           
  @@ -491,7 +527,7 @@
           try {
               
               selectStatement = connection.prepareStatement
  -                ("select * from revisioncontent where uri = ? and "
  +                ("select 1 from revisioncontent where uri = ? and "
                    + "xnumber = ?");
               selectStatement.setString(1, revisionUri);
               selectStatement.setString(2, revisionNumber);
  @@ -510,14 +546,20 @@
               
           } catch (SQLException e) {
               getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
  +            connectIfNeededAndPossible();
               throw new ServiceAccessException(this, e.getMessage());
           } catch (IOException e) {
               getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
  +            connectIfNeededAndPossible();
               throw new ServiceAccessException(this, e.getMessage());
           } catch(RevisionAlreadyExistException e) {
  +            getLogger().log("RevisionNotFoundException encountered for " +
  +                            revisionUri + " revision " + revisionNumber,
  +                            LOG_CHANNEL,Logger.WARNING);
               throw e; // we do NOT want this caught by next clause.
           } catch (Exception e) {
               getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
  +            connectIfNeededAndPossible();
               throw new ServiceAccessException(this, e.getMessage());
           } finally {
               closeStatement(selectStatement);
  @@ -546,7 +588,7 @@
           try {
               
               selectStatement = connection.prepareStatement
  -                ("select * from revisioncontent where uri = ? and "
  +                ("select 1 from revisioncontent where uri = ? and "
                    + "xnumber = ?");
               selectStatement.setString(1, revisionUri);
               selectStatement.setString(2, revisionNumber);
  @@ -567,14 +609,20 @@
               
           } catch (SQLException e) {
               getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
  +            connectIfNeededAndPossible();
               throw new ServiceAccessException(this, e.getMessage());
           } catch (IOException e) {
               getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
  +            connectIfNeededAndPossible();
               throw new ServiceAccessException(this, e.getMessage());
           } catch(RevisionNotFoundException e) {
  +            getLogger().log("RevisionNotFoundException encountered for " +
  +                            revisionUri + " revision " + revisionNumber,
  +                            LOG_CHANNEL,Logger.WARNING);
               throw e; // we do NOT want this caught by next clause.
           } catch (Exception e) {
               getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
  +            connectIfNeededAndPossible();
               throw new ServiceAccessException(this, e.getMessage());
           } finally {
               closeStatement(selectStatement);
  @@ -603,6 +651,7 @@
               
           } catch (Exception e) {
               getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
  +            connectIfNeededAndPossible();
               throw new ServiceAccessException(this, e.getMessage());
           }
           
  
  
  
  1.28.2.1  +59 -9     
jakarta-slide/src/stores/slidestore/reference/JDBCDescriptorsStore.java
  
  Index: JDBCDescriptorsStore.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-slide/src/stores/slidestore/reference/JDBCDescriptorsStore.java,v
  retrieving revision 1.28
  retrieving revision 1.28.2.1
  diff -u -r1.28 -r1.28.2.1
  --- JDBCDescriptorsStore.java 7 Oct 2001 19:32:04 -0000       1.28
  +++ JDBCDescriptorsStore.java 25 Feb 2002 20:44:09 -0000      1.28.2.1
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-slide/src/stores/slidestore/reference/JDBCDescriptorsStore.java,v 
1.28 2001/10/07 19:32:04 dirkv Exp $
  - * $Revision: 1.28 $
  - * $Date: 2001/10/07 19:32:04 $
  + * $Header: 
/home/cvs/jakarta-slide/src/stores/slidestore/reference/JDBCDescriptorsStore.java,v 
1.28.2.1 2002/02/25 20:44:09 cmlenz Exp $
  + * $Revision: 1.28.2.1 $
  + * $Date: 2002/02/25 20:44:09 $
    *
    * ====================================================================
    *
  @@ -86,7 +86,7 @@
    * 
    * @author <a href="mailto:[EMAIL PROTECTED]";>Remy Maucherat</a>
    * @author Dirk Verbeeck
  - * @version $Revision: 1.28 $
  + * @version $Revision: 1.28.2.1 $
    */
   
   public class JDBCDescriptorsStore
  @@ -250,6 +250,11 @@
           return statements;
       }
       
  +    protected String getDatabaseConnectionTestStatement() 
  +    {
  +        return "select 1 from objects where uri is null";
  +    }
  +    
       /**
        * Initializes the data source with a set of parameters.
        *
  @@ -361,7 +366,18 @@
           }
       }
       
  -    
  +    /**
  +     * Tries to reconnect if needed but doesn't report failure.
  +     */
  +    protected synchronized void connectIfNeededAndPossible() {
  +        try  {
  +            connectIfNeeded();
  +        }
  +        catch (Throwable ex) {
  +            // ignore
  +        }
  +    }
  +        
       /**
        * Initializes data source.
        * <p/>
  @@ -485,7 +501,18 @@
       public boolean isConnected()
           throws ServiceAccessException {
           try {
  -            return ((connection != null) && (!connection.isClosed()));
  +            if ((connection == null) || (connection.isClosed())) {
  +                return false;
  +            }
  +            
  +            PreparedStatement statement = 
  +                connection.prepareStatement(getDatabaseConnectionTestStatement());
  +            statement.executeQuery();
  +            statement.close();
  +            
  +            // testStatement executed without throwing an exception
  +            return true;
  +            
           } catch (SQLException e) {
               throw new ServiceAccessException(this, e);
           }
  @@ -537,10 +564,14 @@
           if (!alreadyEnlisted) 
           {
               try {
  -//                getLogger().log("start",LOG_CHANNEL,Logger.DEBUG);
  +                // getLogger().log("start",LOG_CHANNEL,Logger.DEBUG);
  +
  +                // test connection
  +                connectIfNeeded();
  +                
                   // discard changes made outside a tranaction
                   connection.rollback();
  -            } catch (SQLException e) {
  +            } catch (Exception e) {
                   throw new XAException(XAException.XAER_RMERR);
               }
               alreadyEnlisted=true;
  @@ -656,6 +687,7 @@
               
           } catch (SQLException e) {
               getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
  +            connectIfNeededAndPossible();
               throw new ServiceAccessException(this, e);
           } finally {
               closeStatement(statement);
  @@ -742,6 +774,7 @@
               
           } catch (SQLException e) {
               getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
  +            connectIfNeededAndPossible();
               throw new ServiceAccessException(this, e);
           } finally {
               closeStatement(statement);
  @@ -827,6 +860,7 @@
               
           } catch (SQLException e) {
               getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
  +            connectIfNeededAndPossible();
               throw new ServiceAccessException(this, e);
           } finally {
               closeStatement(statement);
  @@ -878,6 +912,7 @@
               
           } catch (SQLException e) {
               getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
  +            connectIfNeededAndPossible();
               throw new ServiceAccessException(this, e);
           }
       }
  @@ -920,6 +955,7 @@
               statement.execute();
           } catch (SQLException e) {
               getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
  +            connectIfNeededAndPossible();
               throw new ServiceAccessException(this, e);
           } finally {
               closeStatement(statement);
  @@ -960,6 +996,7 @@
               statement.execute();
           } catch (SQLException e) {
               getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
  +            connectIfNeededAndPossible();
               throw new ServiceAccessException(this, e);
           } finally {
               closeStatement(statement);
  @@ -987,6 +1024,7 @@
               statement.execute();
           } catch (SQLException e) {
               getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
  +            connectIfNeededAndPossible();
               throw new ServiceAccessException(this, e);
           } finally {
               closeStatement(statement);
  @@ -1035,6 +1073,7 @@
               
           } catch (SQLException e) {
               getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
  +            connectIfNeededAndPossible();
               throw new ServiceAccessException(this, e);
           } finally {
               closeStatement(statement);
  @@ -1079,6 +1118,7 @@
               statement.execute();
           } catch (SQLException e) {
               getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
  +            connectIfNeededAndPossible();
               throw new ServiceAccessException(this, e);
           } finally {
               closeStatement(statement);
  @@ -1131,6 +1171,7 @@
               
           } catch (SQLException e) {
               getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
  +            connectIfNeededAndPossible();
               throw new ServiceAccessException(this, e);
           } finally {
               closeStatement(statement);
  @@ -1167,6 +1208,7 @@
               
           } catch (SQLException e) {
               getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
  +            connectIfNeededAndPossible();
               throw new ServiceAccessException(this, e);
           } finally {
               closeStatement(statement);
  @@ -1233,6 +1275,7 @@
               
           } catch (SQLException e) {
               getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
  +            connectIfNeededAndPossible();
               throw new ServiceAccessException(this, e);
           } finally {
               closeStatement(statement);
  @@ -1341,6 +1384,7 @@
               
           } catch (SQLException e) {
               getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
  +            connectIfNeededAndPossible();
               throw new ServiceAccessException(this, e);
           } finally {
               closeStatement(statement);
  @@ -1408,6 +1452,7 @@
               
           } catch (SQLException e) {
               getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
  +            connectIfNeededAndPossible();
               throw new ServiceAccessException(this, e);
           } finally {
               closeStatement(statement);
  @@ -1474,6 +1519,7 @@
               
           } catch (SQLException e) {
               getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
  +            connectIfNeededAndPossible();
               throw new ServiceAccessException(this, e);
           } finally {
               closeStatement(statement);
  @@ -1563,6 +1609,7 @@
               
           } catch (SQLException e) {
               getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
  +            connectIfNeededAndPossible();
               throw new ServiceAccessException(this, e);
           } finally {
               closeStatement(statement);
  @@ -1642,6 +1689,7 @@
               
           } catch (SQLException e) {
               getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
  +            connectIfNeededAndPossible();
               throw new ServiceAccessException(this, e);
           } finally {
               closeStatement(statement);
  @@ -1708,6 +1756,7 @@
               
           } catch (SQLException e) {
               getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
  +            connectIfNeededAndPossible();
               throw new ServiceAccessException(this, e);
           } finally {
               closeStatement(statement);
  @@ -1727,9 +1776,10 @@
               try {
                   statement.close();
               } catch (SQLException e) {
  +                connectIfNeededAndPossible();
               }
           }
       }
       
       
  -}
  \ No newline at end of file
  +}
  
  
  
  No                   revision
  
  
  No                   revision
  
  
  1.2.2.1   +4 -4      
jakarta-slide/src/stores/slidestore/reference/util/JDBCAwareInputStream.java
  
  Index: JDBCAwareInputStream.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-slide/src/stores/slidestore/reference/util/JDBCAwareInputStream.java,v
  retrieving revision 1.2
  retrieving revision 1.2.2.1
  diff -u -r1.2 -r1.2.2.1
  --- JDBCAwareInputStream.java 25 Feb 2002 17:22:54 -0000      1.2
  +++ JDBCAwareInputStream.java 25 Feb 2002 20:44:09 -0000      1.2.2.1
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-slide/src/stores/slidestore/reference/util/JDBCAwareInputStream.java,v
 1.2 2002/02/25 17:22:54 cmlenz Exp $
  - * $Revision: 1.2 $
  - * $Date: 2002/02/25 17:22:54 $
  + * $Header: 
/home/cvs/jakarta-slide/src/stores/slidestore/reference/util/JDBCAwareInputStream.java,v
 1.2.2.1 2002/02/25 20:44:09 cmlenz Exp $
  + * $Revision: 1.2.2.1 $
  + * $Date: 2002/02/25 20:44:09 $
    *
    * ====================================================================
    *
  @@ -74,7 +74,7 @@
    * precious system resource - or at least Oracle cursors  ;-)
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Chris Kimpton</a>
  - * @version $Revision: 1.2 $
  + * @version $Revision: 1.2.2.1 $
    */
   public class JDBCAwareInputStream extends InputStream {                             
                    
   
  
  
  

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

Reply via email to