0
votes

I have a requirement to integrate elastic search Engine for SQLite database. So, am using Hibernate Search for the search engine integration. 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

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

1 Answers

1
votes

There are multiple problems.

Not closing the 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.

See SessionFactory#close().

Not using Elasticsearch

Another problem is that this error implies that you are using the Lucene based index, not the Elasticsearch integration. You probably want to reconfigure Hibernate Search to use Elasticsearch instead.

Using old versions

Third problem: you are using a very old version of Hibernate ORM and Hibernate Search. These older versions can not be connected to Elasticsearch so don't bother "reconfiguring" it if you don't upgrade first.

I would suggest to use Hibernate Search version 5.9.1.Final and Hibernate ORM 5.2.16.Final.

Details on versions, compatibility and much more can be found on the project vebsite, nicely organized per versions.

For example Hibernate Search 5.9.x is documented here: http://hibernate.org/search/releases/5.9