Index: jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/BundleDbPersistenceManager.java
===================================================================
--- jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/BundleDbPersistenceManager.java	(revision 657773)
+++ jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/BundleDbPersistenceManager.java	Mon May 19 12:09:11 CAT 2008
@@ -82,6 +82,7 @@
  * <li>&lt;param name="{@link #setUrl(String) url}" value=""/>
  * <li>&lt;param name="{@link #setUser(String) user}" value=""/>
  * <li>&lt;param name="{@link #setPassword(String) password}" value=""/>
+ * <li>&lt;param name="{@link #setCheckValidConnectionSql(String) sql} " value=""/>
  * <li>&lt;param name="{@link #setSchema(String) schema}" value=""/>
  * <li>&lt;param name="{@link #setSchemaObjectPrefix(String) schemaObjectPrefix}" value=""/>
  * <li>&lt;param name="{@link #setErrorHandling(String) errorHandling}" value=""/>
@@ -118,6 +119,9 @@
     /** the jdbc password */
     protected String password;
 
+     /** A SQL statement that executes before a connection is used to make sure it is still valid */
+    protected String checkValidConnectionSql;
+
     /** the schema identifier */
     protected String schema;
 
@@ -258,7 +262,24 @@
         this.driver = driver;
     }
 
-    /**
+     /**
+     * Get the check valid connection sql
+     * @return the sql statement
+     */
+    public String getCheckValidConnectionSql() {
+        return checkValidConnectionSql;
+    }
+
+    /**
+     * Set the check valid connection sql
+     * Example: Select 1
+     * @param checkValidConnectionSql
+     */
+    public void setCheckValidConnectionSql(final String checkValidConnectionSql) {
+        this.checkValidConnectionSql = checkValidConnectionSql;
+    }
+
+    /**
      * Returns the configured schema object prefix.
      * @return the configured schema object prefix.
      */
@@ -556,7 +577,7 @@
         this.name = context.getHomeDir().getName();
 
         connectionManager = new ConnectionRecoveryManager(blockOnConnectionLoss,
-                getDriver(), getUrl(), getUser(), getPassword());
+                getDriver(), getUrl(), getUser(), getPassword(), getCheckValidConnectionSql());
 
         // make sure schemaObjectPrefix consists of legal name characters only
         prepareSchemaObjectPrefix();
Index: jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/ConnectionRecoveryManager.java
===================================================================
--- jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/ConnectionRecoveryManager.java	(revision 657773)
+++ jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/ConnectionRecoveryManager.java	Sat May 31 16:35:23 CAT 2008
@@ -74,6 +74,11 @@
     private final String password;
 
     /**
+     * The check valid connection sql statement
+     */
+    private final String checkValidConnectionSql;
+
+    /**
      * The database connection that is managed by this {@link ConnectionRecoveryManager}.
      */
     private Connection connection;
@@ -125,16 +130,18 @@
      * @param url the url to use for the connection
      * @param user the user to use for the connection
      * @param password the password to use for the connection
+     * @param checkValidConnectionSql the sql statement to execute to validate a connection before use
      * @throws RepositoryException if the database driver could not be loaded
      */
     public ConnectionRecoveryManager(
-            boolean block, String driver, String url, String user, String password)
+            boolean block, String driver, String url, String user, String password, String checkValidConnectionSql)
             throws RepositoryException {
         this.block = block;
         this.driver = driver;
         this.url = url;
         this.user = user;
         this.password = password;
+        this.checkValidConnectionSql = checkValidConnectionSql;
         try {
             setupConnection();
             isClosed = false;
@@ -149,6 +156,9 @@
      * connection has been closed, and autoReconnect==true
      * then an attempt is made to reestablish the connection.
      *
+     * Checks the connection for validity if it is enabled
+     * and autoReconnect==true
+     *
      * @return the database connection that is managed
      * @throws SQLException on error
      * @throws RepositoryException if the database driver could not be loaded
@@ -161,6 +171,7 @@
                 throw new SQLException("connection has been closed and autoReconnect == false");
             }
         }
+        checkConnection();
         return connection;
     }
 
@@ -374,6 +385,34 @@
     }
 
     /**
+     * if autoReconnect==false and checkValidConnectionSql is not empty
+     * the sql statement is executed on the connection, if any exception
+     * is thrown the connection is re-established
+     * @throws SQLException if reconnecting fails
+     */
+    public void checkConnection() throws SQLException, RepositoryException {
+        if (autoReconnect && (isClosed || connection == null)) {
+           reestablishConnection();
+           return;
+        }
+        if (!autoReconnect || checkValidConnectionSql == null || checkValidConnectionSql.trim().length() == 0) {
+            return;
+        }
+        PreparedStatement prep;
+        try {
+            prep = (PreparedStatement) preparedStatements.get(checkValidConnectionSql);
+            if (prep == null) {
+                prep = connection.prepareStatement(checkValidConnectionSql);
+                preparedStatements.put(checkValidConnectionSql, prep);
+            }
+            prep.execute();
+        } catch (SQLException e) {
+            log.warn("valid connection sql statement failed: " + e.getMessage());
+            reestablishConnection();
+        }
+    }
+
+    /**
      * Re-establishes the database connection.
      *
      * @throws SQLException if reconnecting failed
Index: jackrabbit-core/src/main/java/org/apache/jackrabbit/core/data/db/DbDataStore.java
===================================================================
--- jackrabbit-core/src/main/java/org/apache/jackrabbit/core/data/db/DbDataStore.java	(revision 662024)
+++ jackrabbit-core/src/main/java/org/apache/jackrabbit/core/data/db/DbDataStore.java	Sat May 31 13:37:54 CAT 2008
@@ -144,6 +144,11 @@
     protected String databaseType;
 
     /**
+     * A SQL statement that executes before a connection is used to make sure it is still valid
+     */
+    protected String checkValidConnectionSql;
+
+    /**
      * The minimum size of an object that should be stored in this data store.
      */
     protected int minRecordLength = DEFAULT_MIN_RECORD_LENGTH;
@@ -285,6 +290,7 @@
         TempFileInputStream fileInput = null;
         ConnectionRecoveryManager conn = getConnection();
         try {
+            conn.checkConnection();
             conn.setAutoReconnect(false);
             String id = null, tempId = null;
             long now;
@@ -361,12 +367,12 @@
                 }
             }
             usesIdentifier(identifier);
-            DbDataRecord record = new DbDataRecord(this, identifier, length, now);
+            DbDataRecord record = new DbDataRecord(this, identifier, length, now);            
-            conn.setAutoReconnect(true);
             return record;
         } catch (Exception e) {
             throw convert("Can not insert new record", e);
         } finally {
+            conn.setAutoReconnect(true);
             conn.closeSilently(rs);
             putBack(conn);
             if (fileInput != null) {
@@ -767,6 +773,24 @@
     }
 
     /**
+     * Get the check valid connection sql
+     * @return the sql statement
+     */
+    public String getCheckValidConnectionSql() {
+        return checkValidConnectionSql;
+    }
+
+    /**
+     * Set the check valid connection sql
+     * Example: Select 1
+     * @param checkValidConnectionSql
+     */
+
+    public void setCheckValidConnectionSql(final String checkValidConnectionSql) {
+        this.checkValidConnectionSql = checkValidConnectionSql;
+    }
+
+    /**
      * Get the user name.
      *
      * @return the user name
@@ -859,7 +883,7 @@
      * @return the new connection
      */
     public ConnectionRecoveryManager createNewConnection() throws RepositoryException {
-        ConnectionRecoveryManager conn = new ConnectionRecoveryManager(false, driver, url, user, password);
+        ConnectionRecoveryManager conn = new ConnectionRecoveryManager(false, driver, url, user, password, checkValidConnectionSql);
         return conn;
     }
 
