} catch (DBException e) { LOG.warn("db error while removing document", e);
    public boolean removeCollection(final Txn transaction, Collection collection) throws PermissionDeniedException {        if (readOnly)            throw new PermissionDeniedException(DATABASE_IS_READ_ONLY);                if (!collection.getPermissions().validate(user, Permission.WRITE))            throw new PermissionDeniedException("User '"+ user.getName() + "' not allowed to remove collection '" + collection.getName() + "'");                long start = System.currentTimeMillis();                final boolean isRoot = collection.getParentPath() == null;                final CollectionCache collectionsCache = pool.getCollectionsCache();        synchronized(collectionsCache) {            String name = collection.getName();                        if (!isRoot) {                // remove from parent collection                Collection parent = openCollection(collection.getParentPath(), Lock.WRITE_LOCK);                // keep the lock for the transaction                if (transaction != null)                    transaction.registerLock(parent.getLock(), Lock.WRITE_LOCK);                if (parent != null) {                    try {                        parent.removeCollection(name.substring(name.lastIndexOf("/") + 1));                        saveCollection(transaction, parent);                    } catch (LockException e) {                        LOG.warn("LockException while removing collection " + name);                    } finally {                        if (transaction == null)                            collection.getLock().release();                    }                }            }                        // remove child collections            String childName;            Collection childCollection;            LOG.debug("removing sub-collections");            for (Iterator i = collection.collectionIterator(); i.hasNext();) {                childName = (String) i.next();                childCollection = openCollection(name + '/' + childName, Lock.WRITE_LOCK);                try {                    removeCollection(transaction, childCollection);                } finally {                    childCollection.getLock().release();                }            }                        Lock lock = collectionsDb.getLock();            try {                lock.acquire(Lock.WRITE_LOCK);                                // if this is not the root collection remove it completely                if (isRoot)                    saveCollection(transaction, collection);                else {                    Value key;                    try {                        key = new Value(name.getBytes("UTF-8"));                    } catch (UnsupportedEncodingException uee) {                        key = new Value(name.getBytes());                    }	                    collectionsDb.remove(transaction, key);                    collectionsCache.remove(collection);                    freeCollection(transaction, collection.getId());                }                            } catch (LockException e) {                LOG.warn("Failed to acquire lock on collections.dbx");            } catch (ReadOnlyException e) {                throw new PermissionDeniedException(DATABASE_IS_READ_ONLY);            } finally {                lock.release();            }                        notifyDropIndex(collection);//            textEngine.dropIndex(collection);//            elementIndex.dropIndex(collection);//            valueIndex.dropIndex(collection);//            qnameValueIndex.dropIndex(collection);                        LOG.debug("removing resources ...");            for (Iterator i = collection.iterator(this); i.hasNext();) {                final DocumentImpl doc = (DocumentImpl) i.next();//                LOG.debug("removing document " + doc.getFileName());                new DOMTransaction(this, domDb, Lock.WRITE_LOCK) {                    public Object start() {                        if(doc.getResourceType() == DocumentImpl.BINARY_FILE) {                            domDb.removeOverflowValue(transaction, ((BinaryDocument)doc).getPage());                        } else {                            NodeImpl node = (NodeImpl)doc.getFirstChild();                            domDb.removeAll(transaction, node.getInternalAddress());                        }                        return null;                    }                }                .run();                new DOMTransaction(this, domDb, Lock.WRITE_LOCK) {                    public Object start() {                        try {                            Value ref = new NodeRef(doc.getDocId());                            IndexQuery query =                                new IndexQuery(IndexQuery.TRUNC_RIGHT, ref);                            domDb.remove(transaction, query, null);                        } catch (BTreeException e) {                            LOG.warn("btree error while removing document", e);                        } catch (DBException e) {                            LOG.warn("db error while removing document", e);                        } catch (IOException e) {                            LOG.warn("io error while removing document", e);                        } catch (TerminatedException e) {                            LOG.warn("method terminated", e);                        }                        return null;                    }                }                .run();                removeDocMetadata(transaction, doc);                freeDocument(transaction, doc.getDocId());            }        }        if (LOG.isDebugEnabled())            LOG.debug("Removing collection took " + (System.currentTimeMillis() - start));        return true;    }