When you change the location of the hibernation index (lucene) at run time, the index is not saved in the new location

I have a requirement in my application where we need to store the index based on the user, so I try to change the location at runtime, but the index is not saved in the new location, and if I provide the same location in the configuration file, it gets saved

I use the following code to change location

LocalSessionFactoryBean localSessionFactoryBean

localSessionFactoryBean.getConfiguration() .setProperty("hibernate.search.default.indexBase", "New_loc") 
localSessionFactoryBean.getObject().getCurrentSession() //on this session object i am doing DAO opertation .

The break configuration specified in the configuration file. I spent 3 days finding a solution for this, but without success. Any help could be helpful. My session code is as follows

protected Session getSession() {

        Configuration conf=sessionFactory.getConfiguration();


        conf.setProperty("hibernate.search.default.indexBase","c:\\testdata" /*CustomerContextHolder.getFile()!=null?CustomerContextHolder.getFile():defaultFileLocation*/);

        ContextHolder.getOrBuildSearchFactory(conf);
        return sessionFactory.getObject().getCurrentSession();
        //  sessionFactory.getObject().openSession();
    }

and bean follows

        

<property name="hibernateProperties">
    <props>
        <prop key="hibernate.dialect">${hibernate.dialect}</prop>
        <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
        <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
        <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>  <!-- Options are [validate, create, update, create-drop] -->
         <!-- <prop key="hibernate.current_session_context_class">thread</prop> -->

        <!-- Connection pool size -->
        <prop key="hibernate.connection.pool_size">${hibernate.connection.pool_size}</prop>

    issue -->
        <prop key="hibernate.search.default.directory_provider">org.hibernate.search.store.impl.FSDirectoryProvider</prop>
        <prop key="hibernate.search.default.locking_strategy">single</prop>
    <prop key="hibernate.search.default.indexBase">c:\abc</prop>
        <prop key="hibernate.search.lucene_version">LUCENE_35</prop>
    </props>

</property>

Update code:

Session getSession() {

        Configuration conf=sessionFactory.getConfiguration();

                conf.setProperty("hibernate.search.default.indexBase","c:\\testdata");
        //conf.configure(); Need to commented otherwise shwoing duplicate Property

        ServiceRegistry serviceRegistry= new ServiceRegistryBuilder().applySettings(
                conf.getProperties()).buildServiceRegistry();
        return   (Session) conf.buildSessionFactory(serviceRegistry).openSession();

    }
+4
3

: ( Hibernate 4.x)

Configuration cfg = localSessionFactoryBean.getConfiguration();
cfg .setProperty("hibernate.search.default.indexBase", "New_loc");
cfg.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(
            cfg.getProperties()).build();
sessionFactory = cfg.buildSessionFactory(serviceRegistry);
+2

After long attempts, finally, I found out the reason for this. If I close sesson after each database entry, then only the index file will be stored in a new location. However, this is not an effective approach, but here is how it works

0
source

Source: https://habr.com/ru/post/1547362/


All Articles