• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Switching database connection in hibernate at runtime

Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone,

I am newbie to Spring & Hibernate.

Recently I have started building this project using JSP, Spring & Hibernate, wherein I have an applicationContext.xml and a db-config.xml ( spring context file ) containing the mappings and database connection parameters.

I am using numerous DAOs in my web application which in turn refer a sessionFactory bean created inside the above xml file. A default database URL, username, password, dialect and other parameters exists in the sessionFactory bean properties, and all runs well.

My requirement is, that I have a single Web application to point to mulitple databases based on user selection.


If a user of my web appln. belongs to Company A ( which exists in DB_A ) then my web appilcation should connnect to DB_A at runtime and show him his respective details
Similarly if a user of Company B logs in ( which exists in DB_B ) then his details should be provided from DB_B database.

The web appilcation when deployed on the A/S ( Tomcat in my case ) is configured by default to point to DB_Z .

Now how to switch the database connections from DB_Z to various other DB_<> s at runtime, moreover this switch should not affect other appilcation users, other users should continue receive data from their individual databases.

I have searched this JavaRanch Forum a lot ( last 2 months devoted ) found a lot of different solutions from creating different SessionFactories / Configurations but have not been able to construct a working example.

I tried the below solution posted on forums, wherein he proposed to build the sessionFactory out of the newly created Configuration file, it goes all well, but how to inject this sessionFactory in all the DAO's of this user's session. ??

Configuration cfg = new Configuration();

System.setProperty("hibernate.connection.driver_class", "oracle.jdbc.driver.OracleDriver");
System.setProperty("hibernate.connection.url", "jdbc:oracle:thin:@");
System.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");


SessionFactory sessionFactory = cfg.buildSessionFactory();

Can anyone guide me what best approach to achieve this, with some sample code if possible.

I will be very grateful to him..

Thanks in advance.

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic