views:

30

answers:

3

I am using lucence.net which creates a file as a 'lock'. From what i can tell it simply creates a file to write to and if it cant the db is locked. I get the exception below

I call lucence_init() excepting it to happen once. I call it in Application_Start after i set the current working folder and other stuff.

I cant tell when this happens but i do know it only happens when i hit F5 in visual studios. And it (obviously) never happens on the first time. I think it happens when i get an exception, hit stop, fix and try to run the code. I need to use the icon in the system tray to stop the VS webserver and rerun the code (occasionally manually deleting the lock file but now i have visual studios doing it as a post build event. Which is weird, maybe i dont need this part)

Anyways because of this init problem my other code isnt run because of the write exception and i cant change the order and dont want to program around it so how might i solve this problem so its less annoying to debug this webapp?

Lock obtain timed out: NativeFSLock@c:\dev\prj\...\App_Data\LuceneIndex_a\write.lock: System.IO.IOException: The process cannot access the file 'c:\dev\prj\...\App_Data\LuceneIndex_a\write.lock' because it is being used by another process.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
   at Lucene.Net.Store.NativeFSLock.Obtain()
A: 

Lucene.Net creates a lock file when your index is opened for the purpose of being written to. If the index is opened but not properly closed, the lock file will remain. When you are debugging, it's possible that the index is being opened but an exception is happening (or you are cancelling the running process) before you close it correctly. It's hard to say what your particular solution is without any more detail, but I'd suggest fixing the problematic code without involving opening the index if possible or explicitly closing the index writer before moving onto the rest of the code.

Will S
IIRC theres only once function that WRITES to lucence and i am positive the exceptions are not happening in that line. But anyways the problem is i am launching the app again and it tries to open lucence while somehow it still holds the lock. Is there a way to kill the process or do SOMETHING when my process ends or just before i launch it again. Actually i am positive the exception never happens when i am writing bc the information is stuck inside of a struct then calls the function to insert and nothing else. So... ... its during writes....
acidzombie24
A: 

Well, if you're sure that you are using only one Lucene.Net index writer at a time, then this exception is happening due to the previously unsuccessful index write, which left write.lock file in your index directory.

The solution is to modify your lucene_init() to explicitly unlock the index directory before creating IndexWriter. Your code may look like this:

IndexWriter writer = null;
try {
    writer = new IndexWriter(indexDir, DefaultAnalyzer);
}
catch (LockObtainFailedException ex) {
    DirectoryInfo indexDirInfo = new DirectoryInfo(indexDir);
    FSDirectory indexFSDir = FSDirectory.Open(indexDirInfo, new Lucene.Net.Store.SimpleFSLockFactory(indexDirInfo));
    IndexWriter.Unlock(indexFSDir);
    writer = new IndexWriter(indexDir, DefaultAnalyzer);
}
buru
A: 

It seems like the problem was disposing writer. I left it as a member variable instead of having the scope of the one function. Changing this and using .Close() completely fixed it.

acidzombie24