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.
Eg.
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();
cfg.configure();
System.setProperty("hibernate.connection.username","userA");
System.setProperty("hibernate.connection.password","pwdA");
System.setProperty("hibernate.connection.driver_class", "oracle.jdbc.driver.OracleDriver");
System.setProperty("hibernate.connection.url", "jdbc:oracle:thin:@192.118.10.1:1521:DB_A");
System.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
cfg.setProperties(System.getProperties());
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.
Manav