0

I am using mysql as my datasource. Databases are created dynamically and the details saved in a main database table. A user can log in to the system and connect to any database. The requirement is to connect to any of this databases at application level using hibernate. At any one time we don't know the number or names of databases except fetch them from the main scheme and pass the value to create a new connection like this.

Connection conn=connectDatabase(db1);
Connection conn=connectDatabase(db2);

I know it can be done but I don't how. The problem is how to do this using hibernate.

zeddarn
  • 302
  • 2
  • 14

1 Answers1

0

You can use the jndi-naming look up in hibernate. Configure your database in server.xml of your Tomcat(may be)...

You can access it in code this way...

Context initialCntx = new InitialContext();
SessionFactory sessionFactory = (SessionFactory)initialCntx
     .lookup("java:comp/env/hibernate/SampleSessionFactory");
Session hibernateSess = sessionFactory.getCurrentSession();
Shashank Kadne
  • 7,993
  • 6
  • 41
  • 54