为什么Lucene.Net索引抛出一个System.IO.IOException是未处理?抛出、索引、未处理、Net

2023-09-03 17:07:20 作者:各自安好

该例外被抛起来的时候说文件 write.lock 不能使用,因为它正由另一个进程,然而,这是一个非常简单的测试应用程序的Lucene.Net而且也使用它没有其他的过程中,如何这可能是任何想法

The exception is thrown up at times saying the file write.lock cannot be used as it is being used by another process, however this is a very simple test app for Lucene.Net and there's no other process using it, any idea of how this may be

异常的详细信息如下:

System.IO.IOException was unhandled
HResult=-2147024864
Message=The process cannot access the file 
     'c:\temp\luceneidx\write.lock' because it is being used by another process.

Source=mscorlib
StackTrace:
    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
    at System.IO.File.InternalDelete(String path, Boolean checkHost)
    at System.IO.File.Delete(String path)
    at Lucene.Test.LuceneSearchInternal.get__directory() 
    in C:\Lucene.Test\LuceneSearchResumes.cs:line 35

相关code抛出的例外是,

The relevant code throwing the exception is,

var lockFilePath = Path.Combine(_luceneDir, "write.lock");

if (File.Exists(lockFilePath))
    File.Delete(lockFilePath);   // THROWS exception sometimes

在code是大多是从的这篇文章。

该指数正在建立使用 Task.Factory.StartNew()后台线程和WPF的图形用户界面做了搜索,而该指数正在建立。这里只有一个线程写入文件的索引。

The index is being built on a background thread using Task.Factory.StartNew() and the WPF GUI does a search while the index is being built. There's only one thread writing documents to the index.

问:还有哪些是使用Lucene.Net索引的过程

Question: Which other process is using the Lucene.Net index?

推荐答案

假设code presented是相关的搜索程序(没有索引的过程),你不应该试图删除锁文件的每一次尝试访问索引。该异常被抛出,因为后台线程正在写索引,你随意尝试删除其锁定文件时,线程本身应该处理删除。

Assuming that the code presented is relevant to the search procedure (not the indexing procedure), you shouldn't try to delete the lock file every single time you try to access the index. The exceptions are being thrown because the background threads are currently writing to the index, and you are arbitrarily trying to delete its lock file when the thread itself should handle the deletion.

在您发布的文章,这种机制用于修复的Lucene索引系统/应用程序崩溃后,而这是写索引,留下的锁定的。然而,这是几乎不常见的情况。我相信,在$ C $的CProject文章假定到索引单个线程访问,因此它采用的方法。

In the article you posted, this mechanism is used to recover the Lucene index after a system/application crash while it was writing the index, leaving it locked. However, this is hardly the common case. I believe that in the CodeProject article it is assumed a single-thread access to the index, hence the approach it takes.

在你的项目,你需要能够检查锁定文件的存在是否是由于当前的写访问或到previous应用/系统崩溃。您可以使用锁定对象在code,它是动态的碰撞事件中解脱出来,这两种情况区别对待。

In your project, you need to be able to check whether the existence of the lock file is due to current write access or to a previous application/system crash. You can use a lock object in your code, which is dynamically freed in the event of a crash, to discriminate between the two cases.