How to create multiple database connections for different databases in java

前端 未结 6 1074
心在旅途
心在旅途 2020-12-25 08:31

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

6条回答
  •  再見小時候
    2020-12-25 08:52

    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

提交回复
热议问题