ConnectionSupplier

@InterfacedService(acceptMultiple = false)
@MissingServiceMessage(message = "Using a database required a service implementing ConnectionSupplier")
interface ConnectionSupplier(source)

Allows access to an SQL database.

Supported RDBMS

Currently, PostgreSQL is the best experience you can have. H2 was also tested, but requires the PostgreSQL compatibility mode. Whether the H2 database runs in-memory, in a file, or as a server is up to you, see the available connection modes for more details.

An H2 JDBC URL might look like this: jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;MODE=PostgreSQL;DATABASE_TO_LOWER=TRUE;DEFAULT_NULL_ORDERING=HIGH

DB_CLOSE_DELAY=-1 prevents the database from being lost when all connections are closed, while MODE=PostgreSQL;DATABASE_TO_LOWER=TRUE;DEFAULT_NULL_ORDERING=HIGH is required.

See the H2 compatibility modes for more details.

Using H2 is only recommended if you absolutely do not want to/cant install the PostgreSQL server, the PostgreSQL driver has a far superior quality, larger feature set and has full syntax support.

Usage

First, register your instance as a service with @BService or any annotation that enables your class for dependency injection.

With Flyway (Recommended)

You can run this code after initializing your service:

Flyway.configure()
.dataSource(source)
.schemas("bc")
.locations("bc_database_scripts")
.validateMigrationNaming(true)
.loggers("slf4j")
.load()
.migrate()

This will run all the migration scripts required to set up your database

Manual initialization

You will have to create a bc schema, and run these scripts in chronological order.

See also

Inheritors

Properties

Link copied to clipboard
abstract val maxConnections: Int

The maximum number of connections retrievable without blocking.

Link copied to clipboard

Returns the duration until a thread/coroutine dump is attempted during a transaction.

Functions

Link copied to clipboard
abstract fun getConnection(): Connection