VIP免费

Lucene org.apache.lucene.store.LockObtainFailedException: Lock held by this virtual machine: XX 问题解决

精品 原创 lucene全文搜索synchronized
65
DEMO程序园
程序猿 2020-07-01
积分:0

简介:

最近在集成Lucene做全文你检索,在生成索引文件时,遇到这样一个报错:Lucene org.apache.lucene.store.LockObtainFailedException: Lock held by this virtual machine: E:\lucene\write.lock首先排查出现这样报错的原因:Lucene当中,一个索引目录只能打开一个IndexWriter, 当在并

最近在集成Lucene做全文你检索,在生成索引文件时,遇到这样一个报错:


Lucene org.apache.lucene.store.LockObtainFailedException: Lock held by this virtual machine: E:\lucene\write.lock


file

首先排查出现这样报错的原因:


Lucene当中,一个索引目录只能打开一个IndexWriter, 当在并发操做时,便容易出现线程1 IndexWriter 还未关闭,线程2开始了索引文档写入,也就是线程1的 Lucene 索引锁还未释放,线程2开始获取。


知道原因,那我们解决起来就方便了,只需要在方法体上加上锁关键字即可:


public synchronized void addIndex(ProjectSource projectSource){

    ReentrantLock lock = new ReentrantLock();

    IndexWriter writer = null;

    lock.lock();

    try{

        writer=getWriter();

        Document doc=new Document();

        doc.add(new StringField("id",String.valueOf(projectSource.getId()), Field.Store.YES));

        doc.add(new TextField("name",projectSource.getTitle(),Field.Store.YES));

        doc.add(new StringField("publishDate", DateUtils.parseDateToStr("yyyy-MM-dd",new Date()),Field.Store.YES));

        doc.add(new TextField("content",projectSource.getFuctionContent(),Field.Store.YES));

        writer.addDocument(doc);

        writer.close();

    }catch(Exception e){

        logger.error("add article index error :",e);

    }finally{

        lock.unlock();

    }

}


评论
最新发布
2025-10-22
2025-10-22
2025-07-22
2025-06-20
2025-05-27
2025-05-21
2025-05-18
2025-05-15
2025-05-15
2025-05-14
layui

微信扫码关注DEMO程序园公众号

本周热门
441
399
394
315
266
252
200
189
170
169
热门下载
27
20
19
14
14
12
12
12
12
11