How to connect multiple databases using datasource objects in hibernate

I am new to hibernate. Now I am able to connect a single database using hibernate. My requirement is we need to connect multiple databases using a single configuration file. Can anyone help me, please?

Take a look at Hibernate multitenancy.

Thank you . My requirement is Today i am using three databases in future i may get some more databases. I want to add all the databases in config file without touching my code, i.e., if i get a new requirement to use one more database then i have to go and add the properties of a database in the Config file. How can i do that with hibernate? how can i include multiple database properties(Mysql, oracle etc…,) in a single config file.

Can you please help me to find the solution for this?

Using Maven profiles you can vary the content of the configuration file.

Or, you can use Spring which does not require any XML configuration file and you can use profiles instead.

I don’t spring, Can we do using JNDI Datasource?

If you use Maven profiles, you can customize the build any way you want, hence produce environment-specific artifacts.

Sorry to trouble you…, My requirement is i should not use maven. I heard like using the data-source we can do . Is it possible?

Yes, you can do it with a DataSource Proxy as well. You intercept the Connection request and you route it to whatever actual DataSource you want. Take a look at Spring AbstractRoutingDatasource and implement your own solution that does the same thing.

1 Like

Thank yo so much.Can we have a call for 15 mints or can you explain in detail, because i am not aware of this concept.

There’s no need. You already have the solution. Now it’s time to implement it.