为什么Lucene。抛出一个系统。io。IOException未处理的吗?

时间:2022-04-28 03:07:54

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

有时会抛出异常,表示文件正在写入。锁不能被使用,因为它正在被另一个进程使用,然而这是一个非常简单的测试应用。没有其他进程使用它,也不知道这是怎么回事

The exception details are as follows:

例外情况如下:

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

The relevant code throwing the exception is,

抛出异常的相关代码是,

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

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

The code is mostly from this article.

代码主要来自本文。

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.

索引使用Task.Factory.StartNew()构建在后台线程上,WPF GUI在构建索引时进行搜索。只有一个线程将文档写入索引。

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

问:使用Lucene的是哪个过程。净指数吗?

1 个解决方案

#1


2  

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.

假设所呈现的代码与搜索过程(而不是索引过程)相关,您不应该尝试每次尝试访问索引时删除锁文件。抛出异常是因为后台线程当前正在向索引写入,并且当线程本身处理删除时,您正在任意地尝试删除它的锁文件。

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.

在您发布的文章中,该机制用于在系统/应用程序编写索引时恢复Lucene索引,使其处于锁定状态。然而,这种情况并不常见。我相信在CodeProject的文章中,它假定为对索引的单线程访问,因此采用了这种方法。

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.

在您的项目中,您需要能够检查锁文件的存在是由于当前的写访问,还是由于以前的应用程序/系统崩溃。您可以在代码中使用一个锁对象(在崩溃时动态释放锁对象)来区分这两种情况。

#1


2  

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.

假设所呈现的代码与搜索过程(而不是索引过程)相关,您不应该尝试每次尝试访问索引时删除锁文件。抛出异常是因为后台线程当前正在向索引写入,并且当线程本身处理删除时,您正在任意地尝试删除它的锁文件。

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.

在您发布的文章中,该机制用于在系统/应用程序编写索引时恢复Lucene索引,使其处于锁定状态。然而,这种情况并不常见。我相信在CodeProject的文章中,它假定为对索引的单线程访问,因此采用了这种方法。

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.

在您的项目中,您需要能够检查锁文件的存在是由于当前的写访问,还是由于以前的应用程序/系统崩溃。您可以在代码中使用一个锁对象(在崩溃时动态释放锁对象)来区分这两种情况。