-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: How to configure the session factory dynamically ?
PostPosted: Wed Jul 25, 2007 6:30 am 
Newbie

Joined: Wed Jul 25, 2007 6:14 am
Posts: 3
Hi All,

Our web application runs on 57 different places (57 session factories and data sources) . Based on the url, my filter class will find which session factory and data source name to use.

My question is how to override or set the value of the session factory and data source name dynamically to the hibernate.cfg.xml file (I will know the value only after the user entered the url) ?

Is there any alternative way to implement this?

Thanks in advance,

Regards,
Prabhu.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 25, 2007 7:23 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

use this code in your Application to initialize new sessionfactory by user given parameters. remove cfg.xml

Code:
Configuration confg = new Configuration();
       
        confg.setProperty("hibernate.connection.driver_class","net.sourceforge.jtds.jdbc.Driver");
        confg.setProperty("hibernate.connection.url","jdbc:jtds:sqlserver://"+servername+":" +hibernateConfig.getPort()  +"/" + dbname);
        confg.setProperty("hibernate.connection.username",username);
        confg.setProperty("hibernate.connection.password",password);       
        confg.setProperty("hibernate.dialect","org.hibernate.dialect.SQLServerDialect");
        confg.setProperty("hibernate.connection.pool_size",""+poolsize);
        //confg.setProperty("hibernate.cache.provider_class","org.hibernate.cache.NoCacheProvider");
        confg.setProperty("hibernate.cache.provider_class","org.hibernate.cache.OSCacheProvider");
               
       
        confg.setProperty("hibernate.current_session_context_class","thread");
       
        confg.setProperty("hibernate.jdbc.use_scrollable_resultset","true");
        confg.setProperty("hibernate.show_sql","true");
       
       

       
       
        try{
              

            confg.addClass(Class2.class);
            confg.addClass(Class2.class);

sessionFactory = confg.buildSessionFactory();

} catch (MappingException e) {
                       throw e;
        } catch (HibernateException e) {
           throw e;

        }



Amila
(Don't forget to rate if helps)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 25, 2007 7:54 am 
Newbie

Joined: Wed Jul 25, 2007 6:14 am
Posts: 3
Hi Amila,

Thanks for your quick reply. I didn't remove the hibernate.cfg.xml file. I removed only the below tag

<property name="hibernate.connection.datasource">java:/eDataSource</property>

and added the same using set property

Configuration confg = new Configuration();
confg.setProperty("hibernate.connection.datasource","java:/eDataSource");

sessionFactory =confg.configure().buildSessionFactory();

It works fine. But how do I set the session factory name dynamically?

I need to replace or add the value of this <session-factory .. > tag

<session-factory name="java:/hibernate/EHibernateFactory">
....
</session-factory>

How to do add or replace the value of the session factory dynamically?


Regards,
Prabhu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 25, 2007 8:05 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

use SessionFactoryObjectFactory.addInstance() method to add your dynamically created sessionFactory to JNDI lookups


Amila

(Don't forget to rate if helps)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 25, 2007 8:10 am 
Newbie

Joined: Wed Jul 25, 2007 6:14 am
Posts: 3
Hi Amila,

I got the attribute name (hibernate.session_factory_name) to configure session factory dynamically.

It works fine.

Thanks a lot. You gave me a good hint.

Regards,
Prabhu.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.