Class DbSource

java.lang.Object
io.github.mmm.orm.source.DbSource

public final class DbSource extends Object
A DbSource identifies a database to connect to as a tenant. It is just a wrapper for a String used as identifier. Most applications only need to talk to a single database and schema. There is always a default DbSource. However, to support multi-tenancy and connecting to multiple different databases the DbSource is used to identify the tenant.
From a logical point-of-view a DbSource is similar to a DataSource. However, it is a much higher abstraction. Only in case a DbSource points to an RDBMS and you are using JDBC to connect then there will be a DataSource associated under the hood to talk to that database.
When you configure database settings, you will always include the DbSource identifier in the property names:
 db.default.url=jdbc:postgresql://db.company.com:5432/db
 db.default.dialect=postgresql
 db.default.user=admin
 db.default.password=top$ecret
 db.default.pool=hikari
 
So in order to define a secondary database you can simply use a identifier other than "default":
 db.h2.url=jdbc:h2:mem:db
 db.h2.dialect=h2
 db.h2.user=sa
 db.h2.password=
 db.h2.pool=hikari
 
Now, in your repository implementation you may override the DbSource to DbSource.of("h2") to work on the secondary database.
  • Field Details

    • PROPERTY_PREFIX_DB

      public static final String PROPERTY_PREFIX_DB
      The static part of the property prefix.
      See Also:
    • KEY_URL

      public static final String KEY_URL
      Key for the database connection URL.
      See Also:
    • KEY_HOST

      public static final String KEY_HOST
      Key for the database host name.
      See Also:
    • KEY_PORT

      public static final String KEY_PORT
      Key for the database port number.
      See Also:
    • KEY_USER

      public static final String KEY_USER
      Key for the user login of the database connection.
      See Also:
    • KEY_PASSWORD

      public static final String KEY_PASSWORD
      Key for the user password of the database connection.
      See Also:
    • KEY_DATABASE

      public static final String KEY_DATABASE
      Key for the database name.
      See Also:
    • KEY_DIALECT

      public static final String KEY_DIALECT
      Key for the name of the database dialect. Will be auto-configured if undefined.
      See Also:
    • KEY_TYPE

      public static final String KEY_TYPE
      Key for the type of database. This is very similar to the dialect but for the same database type potentially different dialects may exist (e.g. due to different versions of the database product). Will be auto-configured if undefined.
      See Also:
    • KEY_POOL

      public static final String KEY_POOL
      Key for the ID of the connection pool to use (e.g. "hikari", "c3po", or "dbcp"). Will be auto-configured if undefined.
      See Also:
    • KEY_KIND

      public static final String KEY_KIND
      Key for the kind of database connection (e.g. "jdbc" or "r2dbc"). Will be auto-configured if undefined.
      See Also:
    • STANDARD_KEYS

      public static final Set<String> STANDARD_KEYS
      The standard keys for the database connection. Other keys will be specific for particular implementations (e.g. connection pools).
  • Method Details

    • getId

      public String getId()
      Returns:
      the identifier of the database as tenant.
    • getPropertyPrefix

      public String getPropertyPrefix()
      Returns:
      the property prefix as "db.«id»." (e.g. "db.default." for the default source).
      See Also:
    • getPropertyKey

      public String getPropertyKey(String key)
      Parameters:
      key - the unqualified property key.
      Returns:
      the qualified property key.
    • get

      public static DbSource get()
      Returns:
      the default DbSource for the "primary" database. It has the identifier "default".
    • of

      public static DbSource of(String id)
      Parameters:
      id - the identifier to wrap as DbSource.
      Returns:
      a DbSource with the given id. Will be the default DbSource if id is null or "default".