Lock obtain timed out error : NativeFSLock@C:\lucene\indexes\com.srccodes.example.hibernate.Contact\write.lock

Lock obtain timed out error : NativeFSLock@C:\lucene\indexes\com.srccodes.example.hibernate.Contact\write.lock

我需要为 SQLite 数据库集成弹性搜索引擎。因此,我使用 Hibernate Search 进行搜索引擎集成。在我有一个新记录到 table 之后我得到了错误。

我收到以下错误:

错误:HSEARCH000058:发生异常org.apache.lucene.store.LockObtainFailedException:锁获取超时: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>

存在多个问题。

不关闭 SessionFactory

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

参见SessionFactory#close()

没有使用 Elasticsearch

另一个问题是这个错误暗示您使用的是基于 Lucene 的索引,而不是 Elasticsearch 集成。您可能想要重新配置 Hibernate Search 以改为使用 Elasticsearch。

使用旧版本

第三个问题:您使用的是非常旧版本的Hibernate ORM 和Hibernate Search。这些旧版本无法连接到 Elasticsearch,所以如果您不先升级,请不要打扰 "reconfiguring"。

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

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

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