简体   繁体   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

I have a requirement to integrate elastic search Engine for SQLite database. 我需要为SQLite数据库集成弹性搜索引擎。 So, am using Hibernate Search for the search engine integration. 因此,我正在使用Hibernate Search进行搜索引擎集成。 After i have a new record into the table am getting the error. 在表中有新记录后,出现错误。

Am getting the below error : 正在收到以下错误:

ERROR: HSEARCH000058: Exception occurred org.apache.lucene.store.LockObtainFailedException: Lock obtain timed out: NativeFSLock@C:\\lucene\\indexes\\com.srccodes.example.hibernate.Contact\\write.lock Primary Failure: Entity com.srccodes.example.hibernate.Contact Id null Work Type org.hibernate.search.backend.PurgeAllLuceneWork 错误: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

Please find my code below: 请在下面找到我的代码:

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);
        }               
    }           
}

} }

My Dependencies : 我的依存关系:

<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>

There are multiple problems. 有多个问题。

Not closing the SessionFactory 不关闭SessionFactory

The error LockObtainFailedException suggests you're running multiple Hibernate SessionFactory instances, my first guess is that your code in HibernateUtil starts a new SessionFactory but you aren't closing any of the SessionFactory instances you are starting. 错误LockObtainFailedException提示您正在运行多个Hibernate SessionFactory实例,我的第一个猜测是您的HibernateUtil中的代码将启动一个新的SessionFactory,但是您没有关闭正在启动的任何SessionFactory实例。

See SessionFactory#close() . 请参见SessionFactory#close()

Not using Elasticsearch 不使用Elasticsearch

Another problem is that this error implies that you are using the Lucene based index, not the Elasticsearch integration. 另一个问题是此错误意味着您正在使用基于Lucene的索引,而不是Elasticsearch集成。 You probably want to reconfigure Hibernate Search to use Elasticsearch instead. 您可能想重新配置Hibernate Search以使用Elasticsearch。

Using old versions 使用旧版本

Third problem: you are using a very old version of Hibernate ORM and Hibernate Search. 第三个问题:您使用的Hibernate ORM和Hibernate Search版本非常旧。 These older versions can not be connected to Elasticsearch so don't bother "reconfiguring" it if you don't upgrade first. 这些较旧的版本无法连接到Elasticsearch,因此,如果您不先升级,请不要“重新配置”它。

I would suggest to use Hibernate Search version 5.9.1.Final and Hibernate ORM 5.2.16.Final. 我建议使用Hibernate Search版本5.9.1.Final和Hibernate ORM 5.2.16.Final。

Details on versions, compatibility and much more can be found on the project vebsite, nicely organized per versions. 有关版本,兼容性和更多内容的详细信息,可以在项目vebsite上找到,每个版本组织得很好。

For example Hibernate Search 5.9.x is documented here: http://hibernate.org/search/releases/5.9 例如,此处记录了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