4

I have requirement in my application where we need to store index on the bases of user so I am trying to change location at runtime, but index is not getting stored on new location and if I am giving same location in config file then it is getting stored

I am using 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 .

Rest configuration I have given in config file. I have invested my 3 days to find the solution for this but no success. Any help would be really appreciated. My code to get session is following

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

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

    }
user1047873
  • 230
  • 3
  • 8
  • 28
  • Have you tried calling `getOrBuildSearchFactory` of `ContextHolder`? e.g `ContextHolder.getOrBuildSearchFactory(config)` – Amogh Jul 07 '14 at 06:04
  • after changing hibernate properties using `setProperty` you have to build sessionFactory again. – Amogh Jul 07 '14 at 06:10
  • Thanks for comment so do i need to call(getOrBuildSearchFactory) this before localSessionFactoryBean.getObject().getCurrentSession() and after localSessionFactoryBean.getConfiguration() .setProperty("hibernate.search.default.indexBase", "New_loc").Will it make transaction issue becuasue i have tried with disconnecting curent session and then tried to opennew seesion using openSession then it give transection related errror – user1047873 Jul 07 '14 at 06:25
  • can you show updated code? – Amogh Jul 07 '14 at 06:56
  • any update on answer? – Amogh Jul 07 '14 at 10:18
  • Hi really sorry for late reply!! I am not able to find ContextHolder in hibernate 4.1.0.Final version .Do we have something else in hibernate 4.1.0.Final – user1047873 Jul 07 '14 at 14:55

3 Answers3

2

Try doing this : (For 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);
Amogh
  • 4,453
  • 11
  • 45
  • 106
1

I am not sure whether this dynamic changing of index base will work. Also rebuilding the factory seems dodgy. Did you have a look at index sharding - http://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#advanced-features-dynamic-sharding. This should allow you to split data according to let's say a user id.

If sharding does not solve your problem, you maybe could refine your actual usecase. Maybe there is another solution.

Hardy
  • 18,659
  • 3
  • 49
  • 65
0

After lot's of effor finally i figured out the reason for this.If i will close the sesson after each database write then only index file will get stored on new location .However this is not efficeint approach but this is how it works

user1047873
  • 230
  • 3
  • 8
  • 28