Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to configure multiple database connections in Dropwizard?

I am working on some code that leverages Dropwizard that will require that I need to connect to at least two different databases (I plan to use Hibernate as well). I was unable to find any examples/documentation that will allow me to configure two different database connections in the Database block of the .yml configuration file. Is this possible in Dropwizard? If not, what are the workarounds that people have used in the past. Thank you in advanced for your help!

like image 568
user3211039 Avatar asked Jan 18 '14 23:01

user3211039


People also ask

Is it possible to connect to multiple databases in Java?

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. Here, in this example, we are trying to connect to Oracle and MySQL Databases where following are the URLs and sample user credentials of both databases.

What is configuration class in Dropwizard?

Creating A Configuration Class Each Dropwizard application has its own subclass of the Configuration class which specifies environment-specific parameters. These parameters are specified in a YAML configuration file which is deserialized to an instance of your application's configuration class and validated.

Does Dropwizard use Log4j?

Dropwizard uses Logback for its logging backend. It provides an slf4j implementation, and even routes all java. util. logging , Log4j, and Apache Commons Logging usage through Logback.

What is Dao in Dropwizard?

The Hibernate session is closed before your resource method's return value (e.g., the Person from the database), which means your resource method (or DAO) is responsible for initializing all lazily-loaded collections, etc., before returning.


1 Answers

You can configure multiple databases in dropwizard. In the config.yml you can have multiple database configuration like this.

database1:

driverClass: org.postgresql.Driver
user: user
password: pwd
url: jdbc:postgresql://localhost:5432/db1
validationQuery: select 1
minSize: 2
maxSize: 8

database2:

driverClass: org.postgresql.Driver
user: user
password: pwd
url: jdbc:postgresql://localhost:5432/db2
validationQuery: select 1
minSize: 2
maxSize: 8

And in the config class get both config details.

public class DBConfig extends Configuration {

    private DatabaseConfiguration database1;
    private DatabaseConfiguration database2;

    public DatabaseConfiguration getDatabase1() {
        return database1;
    }

    public DatabaseConfiguration getDatabase2() {
        return database2;
    }
}

And in your service configure which Dao to use which database.

@Override
public void run(MyConfiguration configuration,
                Environment environment) throws ClassNotFoundException {
    ... 

    final DBIFactory factory = new DBIFactory();

    // Note that the name parameter when creating the DBIs must be different
    // Otherwise you get an IllegalArgumentException
    final DBI jdbi1 = factory.build(
            environment, configuration.getUserDatabase(), "db1");
    final DBI jdbi2 = factory.build(
            environment, configuration.getItemDatabase(), "db2");

    final MyFirstDAO firstDAO = jdbi1.onDemand(MyFirstDAO.class);
    final MySecondDAO secondDAO = jdbi2.onDemand(MySecondDAO.class);

    ...
}
like image 156
Manikandan Avatar answered Sep 30 '22 17:09

Manikandan