public final class DatabaseProvider extends Object implements Supplier<Database>
| Modifier and Type | Class and Description |
|---|---|
static interface |
DatabaseProvider.Builder
This builder is immutable, so setting various options does not affect
the previous instance.
|
static class |
DatabaseProvider.Pool |
| Constructor and Description |
|---|
DatabaseProvider(Supplier<Connection> connectionProvider,
Options options) |
| Modifier and Type | Method and Description |
|---|---|
void |
commitAndClose() |
static DatabaseProvider.Pool |
createPool(Config config) |
DatabaseProvider.Builder |
fakeBuilder() |
static DatabaseProvider.Builder |
fromDriverManager(Config config) |
static DatabaseProvider.Builder |
fromDriverManager(String url)
Builder method to create and initialize an instance of this class using
the JDBC standard DriverManager method.
|
static DatabaseProvider.Builder |
fromDriverManager(String url,
Flavor flavor)
Builder method to create and initialize an instance of this class using
the JDBC standard DriverManager method.
|
static DatabaseProvider.Builder |
fromDriverManager(String url,
Flavor flavor,
Properties info)
Builder method to create and initialize an instance of this class using
the JDBC standard DriverManager method.
|
static DatabaseProvider.Builder |
fromDriverManager(String url,
Flavor flavor,
String user,
String password)
Builder method to create and initialize an instance of this class using
the JDBC standard DriverManager method.
|
static DatabaseProvider.Builder |
fromDriverManager(String url,
Properties info)
Builder method to create and initialize an instance of this class using
the JDBC standard DriverManager method.
|
static DatabaseProvider.Builder |
fromDriverManager(String url,
String user,
String password)
Builder method to create and initialize an instance of this class using
the JDBC standard DriverManager method.
|
static DatabaseProvider.Builder |
fromJndi(Context context,
String lookupKey,
Flavor flavor)
Builder method to create and initialize an instance of this class using
a JNDI resource.
|
static DatabaseProvider.Builder |
fromPool(DatabaseProvider.Pool pool)
Use an externally configured DataSource, Flavor, and optionally a shutdown hook.
|
static DatabaseProvider.Builder |
fromProperties(Properties properties)
Configure the database from up to five properties read from the provided properties:
|
static DatabaseProvider.Builder |
fromProperties(Properties properties,
String propertyPrefix)
Configure the database from up to five properties read from the provided properties:
|
static DatabaseProvider.Builder |
fromPropertyFile(String filename)
Configure the database from up to five properties read from a file:
|
static DatabaseProvider.Builder |
fromPropertyFile(String filename,
CharsetDecoder decoder)
Configure the database from up to five properties read from a file:
|
static DatabaseProvider.Builder |
fromPropertyFile(String filename,
String propertyPrefix)
Configure the database from up to five properties read from a file:
|
static DatabaseProvider.Builder |
fromPropertyFile(String filename,
String propertyPrefix,
CharsetDecoder decoder)
Configure the database from up to five properties read from a file:
|
static DatabaseProvider.Builder |
fromPropertyFileOrSystemProperties(String filename)
Configure the database from up to five properties read from the specified
properties file, or from the system properties (system properties will take
precedence over the file):
|
static DatabaseProvider.Builder |
fromPropertyFileOrSystemProperties(String filename,
CharsetDecoder decoder)
Configure the database from up to five properties read from the specified
properties file, or from the system properties (system properties will take
precedence over the file):
|
static DatabaseProvider.Builder |
fromPropertyFileOrSystemProperties(String filename,
String propertyPrefix)
Configure the database from up to five properties read from the specified
properties file, or from the system properties (system properties will take
precedence over the file):
|
static DatabaseProvider.Builder |
fromPropertyFileOrSystemProperties(String filename,
String propertyPrefix,
CharsetDecoder decoder)
Configure the database from up to five properties read from the specified
properties file, or from the system properties (system properties will take
precedence over the file):
|
static DatabaseProvider.Builder |
fromSystemProperties()
Configure the database from up to five system properties:
|
static DatabaseProvider.Builder |
fromSystemProperties(String propertyPrefix)
Configure the database from up to five system properties:
|
Database |
get() |
static DatabaseProvider.Builder |
pooledBuilder(Config config)
Configure the database from the following properties read from the provided configuration:
|
void |
rollbackAndClose() |
void |
transact(DbCode code) |
void |
transact(DbCodeTx code) |
<T> T |
transactReturning(DbCodeTyped<T> code) |
public DatabaseProvider(Supplier<Connection> connectionProvider, Options options)
@CheckReturnValue public static DatabaseProvider.Builder pooledBuilder(Config config)
database.url=... Database connect string (required)
database.user=... Authenticate as this user (optional if provided in url)
database.password=... User password (optional if user and password provided in
url; prompted on standard input if user is provided and
password is not)
database.pool.size=... How many connections in the connection pool (default 10).
database.driver.class The driver to initialize with Class.forName(). This will
be guessed from the database.url if not provided.
database.flavor One of the enumerated values in Flavor. If this
is not provided the flavor will be guessed based on the
value for database.url, if possible.
The database flavor will be guessed based on the URL.
A database pool will be created using HikariCP.
Be sure to retain a copy of the builder so you can call close() later to destroy the pool. You will most likely want to register a JVM shutdown hook to make sure this happens. See VertxServer.java in the demo directory for an example of how to do this.
@CheckReturnValue public static DatabaseProvider.Builder fromPool(DatabaseProvider.Pool pool)
@CheckReturnValue public static DatabaseProvider.Builder fromDriverManager(String url)
@CheckReturnValue public static DatabaseProvider.Builder fromDriverManager(String url, Flavor flavor)
flavor - use this flavor rather than guessing based on the urlpublic static DatabaseProvider.Builder fromDriverManager(Config config)
@CheckReturnValue public static DatabaseProvider.Builder fromDriverManager(String url, Properties info)
@CheckReturnValue public static DatabaseProvider.Builder fromDriverManager(String url, Flavor flavor, Properties info)
flavor - use this flavor rather than guessing based on the url@CheckReturnValue public static DatabaseProvider.Builder fromDriverManager(String url, String user, String password)
@CheckReturnValue public static DatabaseProvider.Builder fromDriverManager(String url, Flavor flavor, String user, String password)
flavor - use this flavor rather than guessing based on the url@CheckReturnValue public static DatabaseProvider.Builder fromJndi(Context context, String lookupKey, Flavor flavor)
public static DatabaseProvider.Builder fromPropertyFile(String filename)
database.url=... Database connect string (required)
database.user=... Authenticate as this user (optional if provided in url)
database.password=... User password (optional if user and password provided in
url; prompted on standard input if user is provided and
password is not)
database.flavor=... What kind of database it is (optional, will guess based
on the url if this is not provided)
database.driver=... The Java class of the JDBC driver to load (optional, will
guess based on the flavor if this is not provided)
This will use the JVM default character encoding to read the property file.
filename - path to the properties file we will attempt to readDatabaseException - if the property file could not be read for any reasonpublic static DatabaseProvider.Builder fromPropertyFile(String filename, CharsetDecoder decoder)
database.url=... Database connect string (required)
database.user=... Authenticate as this user (optional if provided in url)
database.password=... User password (optional if user and password provided in
url; prompted on standard input if user is provided and
password is not)
database.flavor=... What kind of database it is (optional, will guess based
on the url if this is not provided)
database.driver=... The Java class of the JDBC driver to load (optional, will
guess based on the flavor if this is not provided)
filename - path to the properties file we will attempt to readdecoder - character encoding to use when reading the property fileDatabaseException - if the property file could not be read for any reasonpublic static DatabaseProvider.Builder fromPropertyFile(String filename, String propertyPrefix)
database.url=... Database connect string (required)
database.user=... Authenticate as this user (optional if provided in url)
database.password=... User password (optional if user and password provided in
url; prompted on standard input if user is provided and
password is not)
database.flavor=... What kind of database it is (optional, will guess based
on the url if this is not provided)
database.driver=... The Java class of the JDBC driver to load (optional, will
guess based on the flavor if this is not provided)
This will use the JVM default character encoding to read the property file.
filename - path to the properties file we will attempt to readpropertyPrefix - if this is null or empty the properties above will be read;
if a value is provided it will be prefixed to each property
(exactly, so if you want to use "my.database.url" you must
pass "my." as the prefix)DatabaseException - if the property file could not be read for any reasonpublic static DatabaseProvider.Builder fromPropertyFile(String filename, String propertyPrefix, CharsetDecoder decoder)
database.url=... Database connect string (required)
database.user=... Authenticate as this user (optional if provided in url)
database.password=... User password (optional if user and password provided in
url; prompted on standard input if user is provided and
password is not)
database.flavor=... What kind of database it is (optional, will guess based
on the url if this is not provided)
database.driver=... The Java class of the JDBC driver to load (optional, will
guess based on the flavor if this is not provided)
filename - path to the properties file we will attempt to readpropertyPrefix - if this is null or empty the properties above will be read;
if a value is provided it will be prefixed to each property
(exactly, so if you want to use "my.database.url" you must
pass "my." as the prefix)decoder - character encoding to use when reading the property fileDatabaseException - if the property file could not be read for any reasonpublic static DatabaseProvider.Builder fromProperties(Properties properties)
database.url=... Database connect string (required)
database.user=... Authenticate as this user (optional if provided in url)
database.password=... User password (optional if user and password provided in
url; prompted on standard input if user is provided and
password is not)
database.flavor=... What kind of database it is (optional, will guess based
on the url if this is not provided)
database.driver=... The Java class of the JDBC driver to load (optional, will
guess based on the flavor if this is not provided)
properties - properties will be read from hereDatabaseException - if the property file could not be read for any reasonpublic static DatabaseProvider.Builder fromProperties(Properties properties, String propertyPrefix)
database.url=... Database connect string (required)
database.user=... Authenticate as this user (optional if provided in url)
database.password=... User password (optional if user and password provided in
url; prompted on standard input if user is provided and
password is not)
database.flavor=... What kind of database it is (optional, will guess based
on the url if this is not provided)
database.driver=... The Java class of the JDBC driver to load (optional, will
guess based on the flavor if this is not provided)
properties - properties will be read from herepropertyPrefix - if this is null or empty the properties above will be read;
if a value is provided it will be prefixed to each property
(exactly, so if you want to use "my.database.url" you must
pass "my." as the prefix)DatabaseException - if the property file could not be read for any reasonpublic static DatabaseProvider.Builder fromPropertyFileOrSystemProperties(String filename)
database.url=... Database connect string (required)
database.user=... Authenticate as this user (optional if provided in url)
database.password=... User password (optional if user and password provided in
url; prompted on standard input if user is provided and
password is not)
database.flavor=... What kind of database it is (optional, will guess based
on the url if this is not provided)
database.driver=... The Java class of the JDBC driver to load (optional, will
guess based on the flavor if this is not provided)
This will use the JVM default character encoding to read the property file.
filename - path to the properties file we will attempt to read; if the file
cannot be read for any reason (e.g. does not exist) a debug level
log entry will be entered, but it will attempt to proceed using
solely the system propertiespublic static DatabaseProvider.Builder fromPropertyFileOrSystemProperties(String filename, CharsetDecoder decoder)
database.url=... Database connect string (required)
database.user=... Authenticate as this user (optional if provided in url)
database.password=... User password (optional if user and password provided in
url; prompted on standard input if user is provided and
password is not)
database.flavor=... What kind of database it is (optional, will guess based
on the url if this is not provided)
database.driver=... The Java class of the JDBC driver to load (optional, will
guess based on the flavor if this is not provided)
filename - path to the properties file we will attempt to read; if the file
cannot be read for any reason (e.g. does not exist) a debug level
log entry will be entered, but it will attempt to proceed using
solely the system propertiesdecoder - character encoding to use when reading the property filepublic static DatabaseProvider.Builder fromPropertyFileOrSystemProperties(String filename, String propertyPrefix)
database.url=... Database connect string (required)
database.user=... Authenticate as this user (optional if provided in url)
database.password=... User password (optional if user and password provided in
url; prompted on standard input if user is provided and
password is not)
database.flavor=... What kind of database it is (optional, will guess based
on the url if this is not provided)
database.driver=... The Java class of the JDBC driver to load (optional, will
guess based on the flavor if this is not provided)
This will use the JVM default character encoding to read the property file.
filename - path to the properties file we will attempt to read; if the file
cannot be read for any reason (e.g. does not exist) a debug level
log entry will be entered, but it will attempt to proceed using
solely the system propertiespropertyPrefix - if this is null or empty the properties above will be read;
if a value is provided it will be prefixed to each property
(exactly, so if you want to use "my.database.url" you must
pass "my." as the prefix)public static DatabaseProvider.Builder fromPropertyFileOrSystemProperties(String filename, String propertyPrefix, CharsetDecoder decoder)
database.url=... Database connect string (required)
database.user=... Authenticate as this user (optional if provided in url)
database.password=... User password (optional if user and password provided in
url; prompted on standard input if user is provided and
password is not)
database.flavor=... What kind of database it is (optional, will guess based
on the url if this is not provided)
database.driver=... The Java class of the JDBC driver to load (optional, will
guess based on the flavor if this is not provided)
filename - path to the properties file we will attempt to read; if the file
cannot be read for any reason (e.g. does not exist) a debug level
log entry will be entered, but it will attempt to proceed using
solely the system propertiespropertyPrefix - if this is null or empty the properties above will be read;
if a value is provided it will be prefixed to each property
(exactly, so if you want to use "my.database.url" you must
pass "my." as the prefix)decoder - character encoding to use when reading the property file@CheckReturnValue public static DatabaseProvider.Builder fromSystemProperties()
-Ddatabase.url=... Database connect string (required)
-Ddatabase.user=... Authenticate as this user (optional if provided in url)
-Ddatabase.password=... User password (optional if user and password provided in
url; prompted on standard input if user is provided and
password is not)
-Ddatabase.flavor=... What kind of database it is (optional, will guess based
on the url if this is not provided)
-Ddatabase.driver=... The Java class of the JDBC driver to load (optional, will
guess based on the flavor if this is not provided)
@CheckReturnValue public static DatabaseProvider.Builder fromSystemProperties(String propertyPrefix)
-D{prefix}database.url=... Database connect string (required)
-D{prefix}database.user=... Authenticate as this user (optional if provided in url)
-D{prefix}database.password=... User password (optional if user and password provided in
url; prompted on standard input if user is provided and
password is not)
-D{prefix}database.flavor=... What kind of database it is (optional, will guess based
on the url if this is not provided)
-D{prefix}database.driver=... The Java class of the JDBC driver to load (optional, will
guess based on the flavor if this is not provided)
propertyPrefix - a prefix to attach to each system property - be sure to include the
dot if desired (e.g. "mydb." for properties like -Dmydb.database.url)public void transact(DbCode code)
public <T> T transactReturning(DbCodeTyped<T> code)
public void transact(DbCodeTx code)
public DatabaseProvider.Builder fakeBuilder()
public void commitAndClose()
public void rollbackAndClose()
public static DatabaseProvider.Pool createPool(Config config)
Copyright © 2020. All rights reserved.