繁体   English   中英

锁定获取超时错误: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进行搜索引擎集成。 在表中有新记录后,出现错误。

正在收到以下错误:

错误:HSEARCH000058:发生异常org.apache.lucene.store.LockObtainFailedException:锁定获取超时:NativeFSLock @ C:\\ lucene \\ indexes \\ com.srccodes.example.hibernate.Contact \\ write.lock主要失败:实体com.srccodes .example.hibernate.Contact ID空工作类型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,因此,如果您不先升级,请不要“重新配置”它。

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

有关版本,兼容性和更多内容的详细信息,可以在项目vebsite上找到,每个版本组织得很好。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM