public final class Jdbc extends Object implements Jooby.Module
Production-ready jdbc data source, powered by the HikariCP library.
{ use(new Jdbc("jdbc:mysql://localhost/db")); // accessing to the data source get("/my-api", (req, rsp) -> { DataSource db = req.getInstance(DataSource.class); // do something with datasource }); }Via
db
property:
{ use(new Jdbc("db")); // accessing to the data source get("/my-api", (req, rsp) -> { DataSource db = req.getInstance(DataSource.class); // do something with datasource }); }
Database configuration is controlled from your application.conf
file using the
db
property and friends: db.*
.
db = memMem db is implemented with h2 database, before using it make sure to add the h2 dependency to your
pom.xml
:
<dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> </dependency>Mem db is useful for dev environment and/or transient data that can be regenerated.
db = fsFile system db is implemented with h2 database, before using it make sure to add the h2 dependency to your ```pom.xml```: File system db is useful for dev environment and/or transient data that can be regenerated. Keep in mind this db is saved in a tmp directory and db will be deleted it on restarts.
Connect to a database using a jdbc url, some examples here:
# mysql db.url = jdbc:mysql://localhost/mydb db.user=myuser db.password=passwordPrevious example, show you how to connect to mysql, setting user and password. But of course you need the jdbc driver on your
pom.xml
:
If you need to configure or tweak the hikari pool just add hikari.*
entries to your application.conf
file:
db.url = jdbc:mysql://localhost/mydb db.user=myuser db.password=password db.cachePrepStmts=true # hikari hikari.autoCommit = true hikari.maximumPoolSize = 20 # etc...
Also, all the db.*
properties are converted to dataSource.*
to let hikari configure the target jdbc
connection.
{ use(new Jdbc("db.main")); // main database use(new Jdbc("db.audit")); // audit database }
application.conf:
# main database db.main.url = ... db.main.user=... db.main.password = ... # audit db.audit.url = .... db.audit.user = .... db.audit.password = ....
Same principle applies if you need to tweak hikari:
# max pool size for main db hikari.main.maximumPoolSize = 100 # max pool size for audit db hikari.audit.maximumPoolSize = 20
Finally, if you need to inject the audit data source, all you have to do is to use the
Name annotation, like @Name("db.audit")
Constructor and Description |
---|
Jdbc()
Creates a new
Jdbc module. |
Jdbc(String name)
Creates a new
Jdbc module. |
Modifier and Type | Method and Description |
---|---|
com.typesafe.config.Config |
config() |
void |
configure(Env env,
com.typesafe.config.Config config,
com.google.inject.Binder binder) |
<T> Jdbc |
doWith(BiConsumer<com.zaxxer.hikari.HikariConfig,com.typesafe.config.Config> configurer)
Apply advanced configuration options:
|
Jdbc |
doWith(Consumer<com.zaxxer.hikari.HikariConfig> configurer)
Apply advanced configuration options:
|
public Jdbc(String name)
Jdbc
module.name
- A connection string or property with a connection string.public Jdbc()
public <T> Jdbc doWith(BiConsumer<com.zaxxer.hikari.HikariConfig,com.typesafe.config.Config> configurer)
{
use(new Jdbc()
.doWith((hikari, conf) -> {
// do with hikari
})
);
}
configurer
- Configurer callback.public Jdbc doWith(Consumer<com.zaxxer.hikari.HikariConfig> configurer)
{
use(new Jdbc()
.doWith(hikari -> {
// do with hikari
})
);
}
configurer
- Configurer callback.public void configure(Env env, com.typesafe.config.Config config, com.google.inject.Binder binder)
configure
in interface Jooby.Module
public com.typesafe.config.Config config()
config
in interface Jooby.Module
Copyright © 2020. All rights reserved.