public static class DataSourceConfig.Builder
extends java.lang.Object
DataSourceConfig
.Constructor and Description |
---|
Builder() |
Modifier and Type | Method and Description |
---|---|
DataSourceConfig |
build()
Builds a config.
|
@Nullable java.lang.String |
getConnectionTestQuery()
|
@Nullable java.lang.Long |
getConnectionTimeout()
|
java.lang.String |
getDataSourceClassName()
|
@Nullable java.lang.Long |
getIdleTimeout()
See
setIdleTimeout(Long) . |
java.lang.String |
getJdbcUrl()
See
setJdbcUrl(String) . |
@Nullable java.lang.Long |
getMaxLifetime()
See
setMaxLifetime(Long) . |
@Nullable java.lang.Integer |
getMaxPoolSize()
|
java.lang.String |
getPassword()
See
setPassword(String) . |
@Nullable java.lang.String |
getPoolName()
See
setPoolName(String) . |
java.lang.String |
getUsername()
See
setUsername(String) . |
java.lang.Boolean |
isAutoCommit()
|
DataSourceConfig.Builder |
setAutoCommit(java.lang.Boolean autoCommit)
Sets the default auto-commit behavior of all connections.
|
DataSourceConfig.Builder |
setConnectionTestQuery(java.lang.String connectionTestQuery)
If your driver supports JDBC4 it is strongly recommended not setting
this property.
|
DataSourceConfig.Builder |
setConnectionTimeout(java.lang.Long connectionTimeout)
Sets the maximum number of milliseconds to wait for a connection from the pool.
|
DataSourceConfig.Builder |
setDataSourceClassName(java.lang.String dataSourceClassName)
Sets the name of the class implementing
DataSource provided by the JDBC driver. |
DataSourceConfig.Builder |
setIdleTimeout(java.lang.Long idleTimeout)
Sets the maximum amount of time that a connection is allowed to sit idle in the pool.
|
DataSourceConfig.Builder |
setJdbcUrl(java.lang.String jdbcUrl)
This property directs the connection pool to use
DriverManager -based configuration. |
DataSourceConfig.Builder |
setMaxLifetime(java.lang.Long maxLifetime)
Sets the maximum lifetime of a connection in the pool.
|
DataSourceConfig.Builder |
setMaxPoolSize(java.lang.Integer maxPoolSize)
Sets the maximum size that the pool is allowed to reach, including both idle and
in-use connections.
|
DataSourceConfig.Builder |
setPassword(java.lang.String password)
Sets the default authentication password used for obtaining Connections from the
underlying driver.
|
DataSourceConfig.Builder |
setPoolName(java.lang.String poolName)
Sets a name of the connection pool and appears mainly in logging and JMX management
consoles to identify pools and pool configurations.
|
DataSourceConfig.Builder |
setUsername(java.lang.String username)
This property sets the default authentication username used for obtaining Connections
from the underlying driver.
|
public DataSourceConfig build()
Either dataSourceClassName
or jdbcUrl
is required.
username
and password
are required, but can be empty strings.
public java.lang.String getDataSourceClassName()
public DataSourceConfig.Builder setDataSourceClassName(java.lang.String dataSourceClassName)
DataSource
provided by the JDBC driver.
This property is required if jdbcUrl
is not set.
Consult the documentation for your specific JDBC driver to get this class name, or see popular classes.
NOTE: XA data sources are not supported. XA requires a real transaction manager like bitronix.
NOTE: this property may not be required if you are using jdbcUrl
for "old-school" DriverManager-based JDBC driver configuration
(see setJdbcUrl(String)
).
Default: none
dataSourceClassName
- the class name to setpublic java.lang.String getJdbcUrl()
setJdbcUrl(String)
.public DataSourceConfig.Builder setJdbcUrl(java.lang.String jdbcUrl)
DriverManager
-based configuration.
This property is required if dataSourceClassName
is not set.
The DataSource
-based configuration
(see setDataSourceClassName(String)
) is superior for a variety of reasons, but
for many deployments there is little significant difference.
When using this property with "old" drivers, you may also need to set the
driverClassName
property, but try it first without.
NOTE: if this property is used, you may still use DataSource
properties
to configure your driver and is in fact recommended over driver parameters specified in
the URL itself.
Examples of JDBC URL (HyperSQL DB):
jdbc:hsqldb:hsql://localhost:9001/dbName;ifexists=true
jdbc:hsqldb:mem:inMemoryDb
(for in-memory database)
Default: none
jdbcUrl
- a database url of the form jdbc:subprotocol:subname
public java.lang.String getUsername()
setUsername(String)
.public DataSourceConfig.Builder setUsername(java.lang.String username)
This property is required.
Note that for DataSource
-based configuration this works in a very
deterministic fashion by calling DataSource.getConnection(String, String)
.
However, for Driver
-based configurations, every driver is different.
In this case, the username property is set in the Properties
passed to DriverManager.getConnection(String, Properties)
method.
Default: none
username
- the username to setpublic java.lang.String getPassword()
setPassword(String)
.public DataSourceConfig.Builder setPassword(java.lang.String password)
This property is required.
Note that for DataSource
-based configuration this works in a very
deterministic fashion by calling DataSource.getConnection(String, String)
.
However, for Driver
-based configurations, every driver is different.
In this case, the password property is set in the Properties
passed to DriverManager.getConnection(String, Properties)
method.
Default: none
password
- the password to setpublic java.lang.Boolean isAutoCommit()
public DataSourceConfig.Builder setAutoCommit(java.lang.Boolean autoCommit)
Default: true
autoCommit
- the value to setConnection.setAutoCommit(boolean)
public @Nullable java.lang.Long getConnectionTimeout()
public DataSourceConfig.Builder setConnectionTimeout(java.lang.Long connectionTimeout)
SQLException
will be thrown.
1000 ms is the minimum value.
If a value of zero is passed, then Integer.MAX_VALUE
is set instead.
Default: 30 000 (30 seconds)
connectionTimeout
- the number of milliseconds to setpublic @Nullable java.lang.Long getIdleTimeout()
setIdleTimeout(Long)
.public DataSourceConfig.Builder setIdleTimeout(java.lang.Long idleTimeout)
A connection will never be retired as idle before this timeout.
A value of 0 means that idle connections are never removed from the pool.
Default: 600 000 (10 minutes)
idleTimeout
- the timeout in millisecondspublic @Nullable java.lang.Long getMaxLifetime()
setMaxLifetime(Long)
.public DataSourceConfig.Builder setMaxLifetime(java.lang.Long maxLifetime)
When a connection reaches this timeout it is retired from the pool, subject to a maximum variation of +30 seconds.
An in-use connection is never retired, only when it is closed will it is removed then.
NOTE: It is strongly recommended to set this value, and it should be at least 30 seconds less than any database-level connection timeout.
A value of 0 indicates no maximum lifetime (infinite lifetime), subject of course to
the idleTimeout
setting.
Default: 1 800 000 (30 minutes)
maxLifetime
- the number of milliseconds to setpublic @Nullable java.lang.String getConnectionTestQuery()
public DataSourceConfig.Builder setConnectionTestQuery(java.lang.String connectionTestQuery)
Connection.isValid(int)
API.
It is the query that will be executed just before a connection is given to you from the pool to validate that the connection to the database is still alive.
Again, try running the pool without this property, an error will be logged if your driver is not JDBC4 compliant.
Default: none
connectionTestQuery
- the query to setpublic @Nullable java.lang.Integer getMaxPoolSize()
public DataSourceConfig.Builder setMaxPoolSize(java.lang.Integer maxPoolSize)
Basically this value will determine the maximum number of actual connections to the database backend.
A reasonable value for this is best determined by your execution environment.
When the pool reaches this size, and no idle connections are available,
calls to getConnection() will block for up to connectionTimeout
milliseconds
before timing out.
Default: 10
maxPoolSize
- the value to setsetConnectionTimeout(Long)
public @Nullable java.lang.String getPoolName()
setPoolName(String)
.public DataSourceConfig.Builder setPoolName(java.lang.String poolName)
Default: auto-generated
poolName
- the name to set