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.

2

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.