首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络基础 >

Lucene的IndexWriter初始化时的LockObtainFailedException的解决办法

2013-07-01 
Lucene的IndexWriter初始化时的LockObtainFailedException的解决方法原文链接:http://www.javaarch.net/ji

Lucene的IndexWriter初始化时的LockObtainFailedException的解决方法

原文链接:

http://www.javaarch.net/jiagoushi/904.htm

Lucene的IndexWriter初始化时的LockObtainFailedException的解决方法

?

本网站使用了lucene来支持搜索功能,然后定时重建索引,但是最近日志里面出现了下面的异常。

?

这个异常是因为lucene进入到索引目录中,发现里面就是一个write.lock。而IndexWriter的构造函数在试图获取另外一个IndexWriter已经加锁的索引目录时就会抛出一个LockObtainFailedException。

?

?

[ERROR] 2013-06-28 14:00:01,009 [org.springweb.lucene.DataIndex.index] - DataIndex index got error Lock obtain timed out: NativeFSLo

ck@/root/data/write.lock

org.apache.lucene.store.LockObtainFailedException: Lock obtain timed out: NativeFSLock@/root/data/write.lock

? ? ? ? at org.apache.lucene.store.Lock.obtain(Lock.java:84)

? ? ? ? at org.apache.lucene.index.IndexWriter.<init>(IndexWriter.java:1098)

? ? ? ? at org.apache.lucene.index.IndexWriter.<init>(IndexWriter.java:926)

? ? ? ? at org.springweb.lucene.DataIndex.index(DataIndex.java:36)

?

之前的代码是这样的

?

indexwrite.commit();

indexwrite.close();

?

在close方法里面也会调用unlock方法,就是删除write.lock的锁文件,但是之前在索引过程中抛出了异常,导致这个锁没有被释放,下次再建索引的时候就抛异常了。

?

所以要么在indexwrite构造方法前调用,要么保证indexwrite.close();能被调用。

?

?

// indexwrite.getDirectory().close();

if (IndexWriter.isLocked(directory)) {

IndexWriter.unlock(directory);

}

?

unlock就是删除public static final String WRITE_LOCK_NAME = "write.lock";这个文件

?

public static void unlock(Directory directory) throws IOException {

directory.makeLock(IndexWriter.WRITE_LOCK_NAME).release();

}

1 楼 liangcoder 2 小时前   good job~

建议:是不是在indexWriter.close() 时,如果发生异常就unlock下,不是更优雅?

热点排行