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 644105)
+++ jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/BundleDbPersistenceManager.java	(working copy)
@@ -760,35 +760,59 @@
             // get all node bundles in the database with a single sql statement,
             // which is (probably) faster than loading each bundle and traversing the tree
             ResultSet rs = null;
-            DataInputStream din = null;
             try {
-                String sql;
+                String sql = "select count(*) from " + schemaObjectPrefix + "BUNDLE";
+                Statement stmt = connectionManager.executeStmt(sql, new Object[0]);
+                try {
+                    rs = stmt.getResultSet();
+                    if (!rs.next()) {
+                        log.error("Could not retrieve total number of bundles. empty result set.");
+                        return;
+                    }
+                    total = rs.getInt(1);
+                } finally {
+                    closeResultSet(rs);
+                }
                 if (getStorageModel() == SM_BINARY_KEYS) {
-                    sql = "select NODE_ID, BUNDLE_DATA from " + schemaObjectPrefix + "BUNDLE";
+                    sql = "select NODE_ID from " + schemaObjectPrefix + "BUNDLE";
                 } else {
-                    sql = "select NODE_ID_HI, NODE_ID_LO, BUNDLE_DATA from " + schemaObjectPrefix + "BUNDLE";
+                    sql = "select NODE_ID_HI, NODE_ID_LO from " + schemaObjectPrefix + "BUNDLE";
                 }
-                Statement stmt = connectionManager.executeStmt(sql, new Object[0]);
+                stmt = connectionManager.executeStmt(sql, new Object[0]);
                 rs = stmt.getResultSet();
 
                 // iterate over all nodebundles in the db
                 while (rs.next()) {
                     NodeId id;
-                    Blob blob;
                     if (getStorageModel() == SM_BINARY_KEYS) {
                         id = new NodeId(new UUID(rs.getBytes(1)));
-                        blob = rs.getBlob(2);
                     } else {
                         id = new NodeId(new UUID(rs.getLong(1), rs.getLong(2)));
-                        blob = rs.getBlob(3);
                     }
-                    din = new DataInputStream(blob.getBinaryStream());
+
+                    // issuing 2nd statement to circumvent issue JCR-1474
+                    ResultSet bRs = null;
+                    byte[] data = null;
                     try {
+                        Statement bSmt = connectionManager.executeStmt(bundleSelectSQL, getKey(id.getUUID()));
+                        bRs = bSmt.getResultSet();
+                        if (!bRs.next()) {
+                            throw new SQLException("bundle cannot be retrieved?");
+                        }
+                        Blob blob = bRs.getBlob(1);
+                        data = getBytes(blob);
+                    } finally {
+                        closeResultSet(bRs);
+                    }
+
+
+                    try {
                         // parse and check bundle
-                        // check bundle will log any problems itself
+                        // checkBundle will log any problems itself
+                        DataInputStream din = new DataInputStream(new ByteArrayInputStream(data));
                         if (binding.checkBundle(din)) {
                             // reset stream for readBundle()
-                            din = new DataInputStream(blob.getBinaryStream());
+                            din = new DataInputStream(new ByteArrayInputStream(data));
                             NodePropBundle bundle = binding.readBundle(din, id);
                             checkBundleConsistency(id, bundle, fix, modifications);
                         } else {
@@ -799,13 +823,12 @@
                     }
                     count++;
                     if (count % 1000 == 0) {
-                        log.info(name + ": checked " + count + " bundles...");
+                        log.info(name + ": checked " + count + "/" + total + " bundles...");
                     }
                 }
             } catch (Exception e) {
                 log.error("Error loading bundle", e);
             } finally {
-                IOUtils.closeQuietly(din);
                 closeResultSet(rs);
                 total = count;
             }
@@ -864,7 +887,7 @@
         }
 
         // repair collected broken bundles
-        if (consistencyFix && !modifications.isEmpty()) {
+        if (fix && !modifications.isEmpty()) {
             log.info(name + ": Fixing " + modifications.size() + " inconsistent bundle(s)...");
             Iterator iterator = modifications.iterator();
             while (iterator.hasNext()) {
@@ -1048,8 +1071,7 @@
                 }
                 result.add(current);
             }
-            ListNodeIdIterator it = new ListNodeIdIterator(result);
-            return it;
+            return new ListNodeIdIterator(result);
         } catch (SQLException e) {
             String msg = "getAllNodeIds failed.";
             log.error(msg, e);
@@ -1068,6 +1090,30 @@
     }
     
     /**
+     * Reads the blob's bytes and returns it. this is a helper method to
+     * circumvent issue JCR-1039 and JCR-1474
+     * @param blob blob to read
+     * @return bytes of the blob
+     * @throws SQLException if an SQL error occurs
+     * @throws IOException if an IO error occurs
+     */
+    private byte[] getBytes(Blob blob) throws SQLException, IOException {
+        InputStream in = null;
+        try {
+            long length = blob.length();
+            byte[] bytes = new byte[(int) length];
+            in = blob.getBinaryStream();
+            int read, pos = 0;
+            while ((read = in.read(bytes, pos, bytes.length - pos)) > 0) {
+                pos += read;
+            }
+            return bytes;
+        } finally {
+            IOUtils.closeQuietly(in);
+        }
+    }
+
+    /**
      * Loads a bundle from the underlying system and optionally performs
      * a check on the bundle first.
      *
@@ -1082,7 +1128,6 @@
             throws ItemStateException {
         ResultSet rs = null;
         InputStream in = null;
-        byte[] bytes = null;
         try {
             Statement stmt = connectionManager.executeStmt(bundleSelectSQL, getKey(id.getUUID()));
             rs = stmt.getResultSet();
@@ -1090,14 +1135,7 @@
                 return null;
             }
             Blob b = rs.getBlob(1);
-            // JCR-1039: pre-fetch/buffer blob data
-            long length = b.length();
-            bytes = new byte[(int) length];
-            in = b.getBinaryStream();
-            int read, pos = 0;
-            while ((read = in.read(bytes, pos, bytes.length - pos)) > 0) {
-                pos += read;
-            }
+            byte[] bytes = getBytes(b);
             DataInputStream din = new DataInputStream(new ByteArrayInputStream(bytes));
             
             if (checkBeforeLoading) {
@@ -1111,7 +1149,7 @@
             }
             
             NodePropBundle bundle = binding.readBundle(din, id);
-            bundle.setSize(length);
+            bundle.setSize(bytes.length);
             return bundle;
         } catch (Exception e) {
             String msg = "failed to read bundle: " + id + ": " + e;
@@ -1540,7 +1578,7 @@
 
     /**
      * Iterator over an in-memory list of node ids.
-     * This helper class is used by {@link BundleDbPersistenceManager#getAllNodeIds()}.
+     * This helper class is used by {@link BundleDbPersistenceManager#getAllNodeIds}.
      */
     private class ListNodeIdIterator implements NodeIdIterator {
 
