javadatabasepostgresqlkotlinebean

How to use multiple databases with ebean


I'm trying to use Ebean framework ORM queries to different postgres databases. Following these and these docs, queries to my single database works fine. But to address another one, I have to explicitly call it by DB.byName("name"). How can I avoid this explicit call?

To be clear, first example - as it is, second - as I want it to be:

  1. QDbEmailQueue(DB.byName("superadmin")).findCount() works

  2. QDbEmailQueue().findCount() throws next exception:

Query threw SQLException:ERROR: relation "email_queue" does not exist
  Position: 22 Bind values:[] Query was:select count(*) from email_queue t0
javax.persistence.PersistenceException: Query threw SQLException:ERROR: relation "email_queue" does not exist
  Position: 22 Bind values:[] Query was:select count(*) from email_queue t0

In both cases I have my entity-class configured like that in kotlin:

@Entity
@DbName(value = "superadmin")
@Table(name = "email_queue")
class DbEmailQueue(
    @Id
    val emailQueueId: Int
    ...
): Model("superadmin")

And in both cases there is same configuration with EbeanConfigProvider.java like here:

public class EbeanConfigProvider implements ServerConfigProvider {

    public static final String SUPERADMIN = "superadmin";
    public static final String DB = "db";

    @Override
    public void apply(ServerConfig serverConfig) {

        DataSourceConfig dataSourceConfig = serverConfig.getDataSourceConfig();
        dataSourceConfig
                .setUsername(Config.INSTANCE.getAppDbUserName())
                .setPassword(Config.INSTANCE.getAppDbUserPswd());

        switch (serverConfig.getName()) {
            case DB: {
                serverConfig.setDefaultServer(true);
                dataSourceConfig.setUrl(Config.INSTANCE.getAppDbHostUrl() + Config.INSTANCE.getAppDbName());
                break;
            }
            case SUPERADMIN: {
                serverConfig.setDefaultServer(false);
                dataSourceConfig.setUrl(Config.INSTANCE.getAppDbHostUrl() + Config.INSTANCE.getSuperadminDbName());
                break;
            }
        }
    }
}

and application.yaml according to this doc:

ebean:
  dbSchema: public  # use this schema rather than public
  migration:
    run: false       # run database migrations on startup
  search:
    packages: om.bo.mypackage.db
    querybean-packages: om.bo.mypackage.db.query

datasource:
  db:
    driver: org.postgresql.Driver
    default: true

  superadmin:
    driver: org.postgresql.Driver
    dbName: superadmin
    default: false

Solution

  • Just update ebean version. I had ebean 12.1.10. At 12.1.11 problem still exists, but at versions 12.1.12 and 12.1.13 everything works as I want (starting from 12.2.1 problem appears aggain with different exception)