锁获取超时错误:NativeFSLock @ C:\ lucene \ indexes \ com.srccodes.example.hibernate.Contact \ write.lock

时间:2018-04-21 17:53:04

标签: java elasticsearch hibernate-search

我需要为SQLite数据库集成弹性搜索引擎。所以,我正在使用Hibernate Search进行搜索引擎集成。在我有一张新记录到表后我得到了错误。

我得到以下错误:

错误:HSEARCH000058:发生异常org.apache.lucene.store.LockObtainFailedException:Lock获取超时:NativeFSLock @ C:\ lucene \ indexes \ com.srccodes.example.hibernate.Contact \ write.lock 主要失败:     实体com.srccodes.example.hibernate.Contact Id null工作类型org.hibernate.search.backend.PurgeAllLuceneWork

请在下面找到我的代码:

public class App {

private static void doIndex() throws InterruptedException {
    Session session = HibernateUtil.getSession();

    FullTextSession fullTextSession = Search.getFullTextSession(session);
    fullTextSession.createIndexer().startAndWait();

    fullTextSession.close();
}

private static List<Contact> search(String queryString) {
    Session session = HibernateUtil.getSession();
    FullTextSession fullTextSession = Search.getFullTextSession(session);

    QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Contact.class).get();
    org.apache.lucene.search.Query luceneQuery = queryBuilder.keyword().onFields("name").matching(queryString).createQuery();

    // wrap Lucene query in a javax.persistence.Query
    org.hibernate.Query fullTextQuery = fullTextSession.createFullTextQuery(luceneQuery, Contact.class);

    List<Contact> contactList = fullTextQuery.list();

    fullTextSession.close();

    return contactList;
}

private static void displayContactTableData() {
    Session session = null;

    try {
        session = HibernateUtil.getSession();

        // Fetching saved data
        List<Contact> contactList = session.createQuery("from Contact").list();

        for (Contact contact : contactList) {
            System.out.println(contact);
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    } finally{
        if(session != null) {
            session.close();
        }
    }
}

public static void main(String[] args) throws InterruptedException {
    System.out.println("\n\n******Data stored in Contact table******\n");
    displayContactTableData();

    // Create an initial Lucene index for the data already present in the database
    doIndex();

    Scanner scanner = new Scanner(System.in);
    String consoleInput = null;

    while (true) {
        // Prompt the user to enter query string
        System.out.println("\n\nEnter search key (To exit type 'X')");      
        System.out.println();
        consoleInput = scanner.nextLine();

        if("X".equalsIgnoreCase(consoleInput)) {
            System.out.println("End");
            System.exit(0);
        }   

        List<Contact> result = search(consoleInput);            
        System.out.println("\n\n>>>>>>Record found for '" + consoleInput + "'");

        for (Contact contact : result) {
            System.out.println(contact);
        }               
    }           
}

}

我的依赖关系:

<dependencies>
    <!-- hibernate -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.1.4.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-search</artifactId>
        <version>4.1.1.Final</version>
    </dependency>

    <!-- SQLite JDBC library -->
    <dependency>
        <groupId>org.xerial</groupId>
        <artifactId>sqlite-jdbc</artifactId>
        <version>3.7.2</version>
    </dependency>

1 个答案:

答案 0 :(得分:1)

存在多个问题。

未关闭SessionFactory

错误 LockObtainFailedException 建议你运行多个Hibernate SessionFactory 实例,我的第一个猜测是你的 HibernateUtil 中的代码启动了一个新的< em> SessionFactory 但您没有关闭任何正在启动的 SessionFactory 实例。

参见 SessionFactory#close()

不使用Elasticsearch

另一个问题是,此错误意味着您使用的是基于Lucene的索引,而不是Elasticsearch集成。您可能希望重新配置Hibernate Search以使用Elasticsearch。

使用旧版本

第三个问题:您使用的是旧版本的Hibernate ORM和Hibernate Search。这些旧版本无法连接到Elasticsearch,因此如果您不先升级,请不要“重新配置”它。

我建议使用Hibernate Search版本5.9.1.Final和Hibernate ORM 5.2.16.Final。

有关版本,兼容性等的详细信息可以在项目界面上找到,每个版本组织得很好。

例如,Hibernate Search 5.9.x在此处记录: http://hibernate.org/search/releases/5.9