Two hibernatebundle for two same entities classes for two different database

167 views
Skip to first unread message

Dondell Batac

unread,
Aug 10, 2021, 1:03:51 AM8/10/21
to dropwizard-user
Is it possible for two database connections to have different Entities in a bundle?
If so, why I still got errors?

Goal: Is to have two connections that can be pass to Resource files and do a decision there whether to use mssql or Postgres, depends on the client request:

Sample Client request:
1. Request for mssql database connection
curl --location --request GET 'http://localhost:8080/version?db=mssql' \
--header 'Authorization: Bearer i82mL..xUwBABLTYfsqa0wbAOVknpOQrtDHL'

1. Request for Postgres database connection
curl --location --request GET 'http://localhost:8080/version?db=mssql' \
--header 'Authorization: Bearer i82mL..xUwBABLTYfsqa0wbAOVknpOQrtDHL'

Resource Logic:
@GET
    @Operation(description = "This endpoint used to get the current version of EpochServer")
    public VersionInfo getVersion(@NotBlank @QueryParam("db") String db)
    {
        if(db.equals("mssql")) {
            versionInfo.setNeedToUseDatabase("using mssql");
            DataSourceFactory mssql = configuration.getMSSQLDataSourceFactory();
        } else {
            versionInfo.setNeedToUseDatabase("using postgres");
            DataSourceFactory postgres = configuration.getPostgresDataSourceFactory();
        }
        return versionInfo;
    }

Error:
java.lang.IllegalArgumentException: A health check named hibernate already exists
    at com.codahale.metrics.health.HealthCheckRegistry.register(HealthCheckRegistry.java:101)
    at io.dropwizard.hibernate.HibernateBundle.run(HibernateBundle.java:77)
    at io.dropwizard.setup.Bootstrap.run(Bootstrap.java:199)
    at io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:58)
    at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:98)
    at io.dropwizard.cli.Cli.run(Cli.java:78)
    at io.dropwizard.Application.run(Application.java:94)

Code snippet:

private final HibernateBundle<TardisConfiguration> hibernate = new ScanningHibernateBundle<TardisConfiguration>(new String[] {"com.my.package.hibernateEntities.mssql"}, new SessionFactoryFactory())
    {
        @Override
        public DataSourceFactory getDataSourceFactory(TardisConfiguration configuration)
        {
            return configuration.getDatabase();
        }
    };

    private final HibernateBundle<TardisConfiguration> hibernatePostgres = new ScanningHibernateBundle<TardisConfiguration>(new String[] {"com.my.package.hibernateEntities.postgres"}, new SessionFactoryFactory())
    {
        @Override
        public DataSourceFactory getDataSourceFactory(TardisConfiguration configuration)
        {
            return configuration.getDatabase();
        }
    };

Then in initialize:

@Override
public void initialize(Bootstrap<TardisConfiguration> bootstrap)
{
// wraps exceptions
bootstrap.addBundle(hibernate);
bootstrap.addBundle(hibernatePostgres);
}

Thanks,

Dondell
Reply all
Reply to author
Forward
0 new messages