Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create multiple database connections for different databases in java

I have an application which uses four databases in different geographical locations. All the databases contains same tables and only the database name is different according to the location. I have to create some reports in my application which uses data from each database. What would be the proper way to create those database connection from a java application and is there a suitable design pattern for this task which I could use?

like image 988
Harsha Avatar asked May 15 '12 03:05

Harsha


People also ask

Is it possible to connect to multiple databases in Java?

Connection mysqlCon = DriverManager. getConnection(mysqlUrl, "root", "password"); To connect to multiple databases in a single JDBC program you need to connect to the two (or more) databases simultaneously using the above steps.

Is it possible to connect to multiple databases?

Many database management and development tools support multiple connections to homogeneous databases, i.e., where they are all of the same type, ALL MySQL, ALL SQL Server, ALL Oracle, etc. On the other hand, very few support heterogeneous database servers, i.e. MySQL AND SQL Server AND Oracle, etc.

How do I link multiple databases to one application?

so, based on user login, the application should connect different database server. For Ex: if user "xxx" login with credential and belogs to "ABC" company and the database is "ABC", then ABC data need to display on the web page.


2 Answers

As you have not tagged your question with any of this, hibernate, JPA, ORM, I assume you are dealing with plain JDBC.

Having said that, I suggest you to have a DAO layer to deal with underlying databases, and leave the connection details to specific implementations. You can configure your connection strings in some .properties files, lets say.

[Complement]

You can also make use of DAO factory, an implementation of Abstract Factory or Factory Method pattern, whichever suits here.

[Links]

  • A very fine implementation of DAO and DAO Factory, by BalusC
  • Core J2EE Patterns -- arguably dated but might provide some idea.
like image 129
Adeel Ansari Avatar answered Sep 22 '22 23:09

Adeel Ansari


There are multiple ways you can achieve this:

  1. If you are using any Java EE container which supports distributed transaction then you can use there functionality.
  2. If you are with plain JDBC then you will have to maintain your own connection for every database. For JDBC:
    1. Provide all connection details
    2. Have an Facade which gives you desired object by calling a abstract generic DAO.
    3. Have a factory which creates dao based on connection.
  3. Use ORM tools like Hibernate, where you can use configuration for multiple database. Tutorial.
  4. If you are using Spring, then you can configure one datasource per database. Docs

Design Patterns:

  • Facade Pattern - for hiding the complexity and multiple database usage.
  • Factory - In case you manage the database connection yourself.
  • Singleton - For datasources
like image 39
mprabhat Avatar answered Sep 22 '22 23:09

mprabhat