public class Jdbc extends Object implements Jooby.Module
import org.jooby.jdbc.Jdbc; import javax.sql.DataSource; { use(new Jdbc()); // accessing to the data source get("/my-api", (req, rsp) -> { DataSource db = req.getInstance(DataSource.class); // do something with datasource }); }
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.
# 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.db.main.maximumPoolSize = 100 # max pool size for audit db hikari.db.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")
Modifier and Type | Field and Description |
---|---|
protected String |
dbName |
protected static String |
DEFAULT_DB |
Modifier and Type | Method and Description |
---|---|
com.typesafe.config.Config |
config() |
void |
configure(Env mode,
com.typesafe.config.Config config,
com.google.inject.Binder binder) |
protected javax.inject.Provider<DataSource> |
dataSource() |
protected <T> void |
keys(Class<T> type,
Consumer<com.google.inject.Key<T>> callback)
Build keys for the given resource type.
|
protected static final String DEFAULT_DB
protected final String dbName
public Jdbc(String name)
public Jdbc()
public void configure(Env mode, 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
protected final <T> void keys(Class<T> type, Consumer<com.google.inject.Key<T>> callback)
db
two keys
(binding) are generated, once without a name and other with a name. A database: db
is considered the default database.T
- Consumer type.type
- A type to bind.callback
- A generated key.protected final javax.inject.Provider<DataSource> dataSource()
Copyright © 2016. All rights reserved.