private void CleanUp(SearchException originalException)
        {
            //release all readers and writers, then release locks
            SearchException raisedException = originalException;
            foreach (IndexReader reader in readers.Values)
            {
                try
                {
                    reader.Close();
                }
                catch (IOException e)
                {
                    if (raisedException != null)
                        log.Error("Subsequent Exception while closing IndexReader", e);
                    else
                        raisedException = new SearchException("Exception while closing IndexReader", e);
                }
            }
            readers.Clear();

            foreach (IndexWriter writer in writers.Values)
            {
                try
                {
                    writer.Close();
                }
                catch (IOException e)
                {
                    if (raisedException != null)
                        log.Error("Subsequent Exception while closing IndexWriter", e);
                    else
                        raisedException = new SearchException("Exception while closing IndexWriter", e);
                }
            }
            writers.Clear();

            foreach (IDirectoryProvider provider in lockedProviders)
            {
                object syncLock = searchFactoryImplementor.GetLockableDirectoryProviders()[provider];
                Monitor.Exit(syncLock);
            }
            lockedProviders.Clear();

            if (raisedException != null) throw raisedException;
        }
        private void CleanUp(SearchException originalException)
        {
            // Release all readers and writers, then release locks
            SearchException raisedException = originalException;
            foreach (IndexReader reader in readers.Values)
            {
                try
                {
                    reader.Close();
                }
                catch (IOException e)
                {
                    if (raisedException != null)
                    {
                        log.Error("Subsequent Exception while closing IndexReader", e);
                    }
                    else
                    {
                        raisedException = new SearchException("Exception while closing IndexReader", e);
                    }
                }
            }
            readers.Clear();

            // TODO release lock of all indexes that do not need optimization early
            // don't optimize if there is a failure
            if (raisedException == null)
            {
                foreach (IDirectoryProvider provider in lockedProviders)
                {
                    var stats = dpStatistics[provider];
                    if (!stats.OptimizationForced)
                    {
                        IOptimizerStrategy optimizerStrategy = searchFactoryImplementor.GetOptimizerStrategy(provider);
                        optimizerStrategy.AddTransaction(stats.Operations);
                        try
                        {
                            optimizerStrategy.Optimize(this);
                        }
                        catch (SearchException e)
                        {
                            raisedException = new SearchException("Exception whilst optimizing directoryProvider: " + provider.Directory, e);
                            break; // No point in continuing
                        }
                    }
                }
            }

            foreach (IndexWriter writer in writers.Values)
            {
                try
                {
                    writer.Close();
                }
                catch (IOException e)
                {
                    if (raisedException != null)
                    {
                        log.Error("Subsequent Exception while closing IndexWriter", e);
                    }
                    else
                    {
                        raisedException = new SearchException("Exception while closing IndexWriter", e);
                    }
                }
            }

            foreach (IDirectoryProvider provider in lockedProviders)
            {
                object syncLock = searchFactoryImplementor.GetLockableDirectoryProviders()[provider];
                Monitor.Exit(syncLock);
            }

            writers.Clear();
            lockedProviders.Clear();
            dpStatistics.Clear();

            if (raisedException != null)
            {
                throw raisedException;
            }
        }