Index: src/main/java/org/apache/jackrabbit/core/persistence/bundle/BundleDbPersistenceManager.java
===================================================================
--- src/main/java/org/apache/jackrabbit/core/persistence/bundle/BundleDbPersistenceManager.java	(revision 892317)
+++ src/main/java/org/apache/jackrabbit/core/persistence/bundle/BundleDbPersistenceManager.java	(working copy)
@@ -688,7 +688,7 @@
      * {@linkplain NodePropBundle bundles} here
      */
     protected void checkBundleConsistency(NodeId id, NodePropBundle bundle,
-                                          boolean fix, Collection modifications) {
+                                          boolean fix, Collection modifications, Collection orphans) {
         //log.info(name + ": checking bundle '" + id + "'");
 
         // skip all system nodes except root node
@@ -745,6 +745,9 @@
             if (parentId != null && !id.toString().endsWith("babecafebabe")) {
                 if (!existsBundle(parentId)) {
                     log.error("NodeState '" + id + "' references inexistent parent uuid '" + parentId + "'");
+                    if (fix) {
+                        orphans.add(bundle);
+                    }
                 }
             }
         } catch (ItemStateException e) {
@@ -761,6 +764,7 @@
         int count = 0;
         int total = 0;
         Collection modifications = new ArrayList();
+        Collection orphans = new ArrayList();
 
         if (uuids == null) {
             // get all node bundles in the database with a single sql statement,
@@ -803,29 +807,32 @@
                         Statement bSmt = connectionManager.executeStmt(bundleSelectSQL, getKey(id.getUUID()));
                         bRs = bSmt.getResultSet();
                         if (!bRs.next()) {
-                            throw new SQLException("bundle cannot be retrieved?");
+                            //throw new SQLException("bundle cannot be retrieved?");
+                            log.error("invalid bundle '" + id + "', bundle cannot be retrieved or empty result?");
+                        } else {
+                            Blob blob = bRs.getBlob(1);
+                            data = getBytes(blob);
+                            try {
+                                // parse and check bundle
+                                // checkBundle will log any problems itself
+                                DataInputStream din = new DataInputStream(new ByteArrayInputStream(data));
+                                if (binding.checkBundle(din)) {
+                                    // reset stream for readBundle()
+                                    din = new DataInputStream(new ByteArrayInputStream(data));
+                                    NodePropBundle bundle = binding.readBundle(din, id);
+                                    checkBundleConsistency(id, bundle, fix, modifications, orphans);
+                                } else {
+                                    log.error("invalid bundle '" + id + "', see previous BundleBinding error log entry");
+                                }
+                           } catch (Exception e) {
+                               log.error("Error in bundle " + id + ": " + e);
+                           }
                         }
-                        Blob blob = bRs.getBlob(1);
-                        data = getBytes(blob);
                     } finally {
                         closeResultSet(bRs);
                     }
 
-
-                    try {
-                        // parse and check bundle
-                        // checkBundle will log any problems itself
-                        DataInputStream din = new DataInputStream(new ByteArrayInputStream(data));
-                        if (binding.checkBundle(din)) {
-                            // reset stream for readBundle()
-                            din = new DataInputStream(new ByteArrayInputStream(data));
-                            NodePropBundle bundle = binding.readBundle(din, id);
-                            checkBundleConsistency(id, bundle, fix, modifications);
-                        } else {
-                            log.error("invalid bundle '" + id + "', see previous BundleBinding error log entry");
-                        }
-                    } catch (Exception e) {
-                        log.error("Error in bundle " + id + ": " + e);
+                    if (data != null) { 
                     }
                     count++;
                     if (count % 1000 == 0) {
@@ -870,7 +877,7 @@
                         continue;
                     }
 
-                    checkBundleConsistency(id, bundle, fix, modifications);
+                    checkBundleConsistency(id, bundle, fix, modifications, orphans);
 
                     if (recursive) {
                         Iterator iter = bundle.getChildNodeEntries().iterator();
@@ -908,7 +915,23 @@
                 }
             }
         }
-
+        
+        // remove orphans
+        if (fix && !orphans.isEmpty()) {
+            log.info(name + ": Removing " + orphans.size() + " orphan bundle(s)...");
+            Iterator iterator = orphans.iterator();
+            while (iterator.hasNext()) {
+                NodePropBundle bundle = (NodePropBundle) iterator.next();
+                try {
+                    log.info(name + ": Removing orphan bundle '" + bundle.getId() + "'");
+                    bundle.markOld(); // use UPDATE instead of INSERT
+                    destroyBundle(bundle);
+                    evictBundle(bundle.getId());
+                } catch (ItemStateException e) {
+                    log.error(name + ": Error removing bundle: " + e);
+                }
+            }
+        }
         log.info(name + ": checked " + count + "/" + total + " bundles.");
     }
 
