Package io.dropwizard.db
Class DataSourceFactory
java.lang.Object
io.dropwizard.db.DataSourceFactory
- All Implemented Interfaces:
PooledDataSourceFactory
A factory for pooled
ManagedDataSource
s.
Configuration Parameters:
Name | Default | Description |
url |
REQUIRED | The URL of the server. |
driverClass |
none |
The fully qualified class name of the JDBC driver class.
Only required if there were no JDBC drivers registered in META-INF/services/java.sql.Driver .
|
user |
none | The username used to connect to the server. |
password |
none | The password used to connect to the server. |
removeAbandoned |
false |
Remove abandoned connections if they exceed the removeAbandonedTimeout .
If set to true a connection is considered abandoned and eligible for removal if it has
been in use longer than the removeAbandonedTimeout and the condition for
abandonWhenPercentageFull is met.
|
removeAbandonedTimeout |
60 seconds | The time before a database connection can be considered abandoned. |
abandonWhenPercentageFull |
0 |
Connections that have been abandoned (timed out) won't get closed and reported up
unless the number of connections in use are above the percentage defined by
abandonWhenPercentageFull . The value should be between 0-100.
|
alternateUsernamesAllowed |
false |
Set to true if the call
getConnection(username,password)
is allowed. This is used for when the pool is used by an application accessing
multiple schemas. There is a performance impact turning this option on, even when not
used.
|
commitOnReturn |
false |
Set to true if you want the connection pool to commit any pending transaction when a connection is returned. |
rollbackOnReturn |
false |
Set to true if you want the connection pool to rollback any pending transaction when a connection is returned. |
autoCommitByDefault |
JDBC driver's default | The default auto-commit state of the connections. |
readOnlyByDefault |
JDBC driver's default | The default read-only state of the connections. |
properties |
none | Any additional JDBC driver parameters. |
defaultCatalog |
none | The default catalog to use for the connections. |
defaultTransactionIsolation |
JDBC driver default |
The default transaction isolation to use for the connections. Can be one of
none , default , read-uncommitted , read-committed ,
repeatable-read , or serializable .
|
useFairQueue |
true |
If true , calls to getConnection are handled in a FIFO manner.
|
initialSize |
10 |
The initial size of the connection pool. May be zero, which will allow you to start
the connection pool without requiring the DB to be up. In the latter case the minSize
must also be set to zero.
|
minSize |
10 | The minimum size of the connection pool. |
maxSize |
100 | The maximum size of the connection pool. |
initializationQuery |
none | A custom query to be run when a connection is first created. |
logAbandonedConnections |
false |
If true , logs stack traces of abandoned connections.
|
logValidationErrors |
false |
If true , logs errors when connections fail validation.
|
maxConnectionAge |
none |
If set, connections which have been open for longer than maxConnectionAge are
closed when returned.
|
maxWaitForConnection |
30 seconds | If a request for a connection is blocked for longer than this period, an exception will be thrown. |
minIdleTime |
1 minute | The minimum amount of time an connection must sit idle in the pool before it is eligible for eviction. |
validationQuery |
SELECT 1 |
The SQL query that will be used to validate connections from this pool before returning them to the caller or pool. If specified, this query does not have to return any data, it just can't throw a SQLException. |
validationQueryTimeout |
none | The timeout before a connection validation queries fail. |
checkConnectionWhileIdle |
true |
Set to true if query validation should take place while the connection is idle. |
checkConnectionOnBorrow |
false |
Whether or not connections will be validated before being borrowed from the pool. If the connection fails to validate, it will be dropped from the pool, and another will be borrowed. |
checkConnectionOnConnect |
true |
Whether or not connections will be validated before being added to the pool. If the connection fails to validate, it won't be added to the pool. |
checkConnectionOnReturn |
false |
Whether or not connections will be validated after being returned to the pool. If the connection fails to validate, it will be dropped from the pool. |
autoCommentsEnabled |
true |
Whether or not ORMs should automatically add comments. |
evictionInterval |
5 seconds | The amount of time to sleep between runs of the idle connection validation, abandoned cleaner and idle pool resizing. |
validationInterval |
30 seconds | To avoid excess validation, only run validation once every interval. |
validatorClassName |
(none) |
Name of a class of a custom Validator
implementation, which will be used for validating connections.
|
jdbcInterceptors |
(none) |
A semicolon separated list of classnames extending
JdbcInterceptor
|
ignoreExceptionOnPreLoad |
false |
Flag whether ignore error of connection creation while initializing the pool. Set to true if you want to ignore error of connection creation while initializing the pool. Set to false if you want to fail the initialization of the pool by throwing exception. |
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Configures the pool as a single connection pool.Builds a new JDBC data source backed by the connection pool and managed by Dropwizard.int
@Nullable Boolean
boolean
boolean
boolean
boolean
boolean
@Nullable String
@Nullable String
Returns the Java class of the database driver.@Nullable String
int
boolean
boolean
int
int
@Nullable String
Returns the configuration properties for ORM tools.@Nullable Boolean
boolean
getUrl()
Returns the JDBC connection URL.boolean
@Nullable String
getUser()
Returns the SQL query, which is being used for the database connection health check.Returns the timeout for awaiting a response from the database during connection health checks.boolean
boolean
Whether ORM tools allowed to add comments to SQL queries.boolean
boolean
boolean
boolean
boolean
void
setAbandonWhenPercentageFull
(int percentage) void
setAlternateUsernamesAllowed
(boolean allow) void
setAutoCommentsEnabled
(boolean autoCommentsEnabled) void
setAutoCommitByDefault
(@Nullable Boolean autoCommit) void
setCheckConnectionOnBorrow
(boolean checkConnectionOnBorrow) void
setCheckConnectionOnConnect
(boolean checkConnectionOnConnect) void
setCheckConnectionOnReturn
(boolean checkConnectionOnReturn) void
setCheckConnectionWhileIdle
(boolean checkConnectionWhileIdle) void
setCommitOnReturn
(boolean commitOnReturn) void
setDefaultCatalog
(@Nullable String defaultCatalog) void
void
setDriverClass
(@Nullable String driverClass) void
setEvictionInterval
(Duration interval) void
setIgnoreExceptionOnPreLoad
(boolean ignoreExceptionOnPreLoad) void
setInitializationQuery
(@Nullable String query) void
setInitialSize
(int initialSize) void
setJdbcInterceptors
(Optional<String> jdbcInterceptors) void
setLogAbandonedConnections
(boolean log) void
setLogValidationErrors
(boolean log) void
setMaxConnectionAge
(@Nullable Duration age) void
setMaxSize
(int maxSize) void
setMaxWaitForConnection
(Duration maxWaitForConnection) void
setMinIdleTime
(Duration time) void
setMinSize
(int minSize) void
setPassword
(@Nullable String password) void
setProperties
(Map<String, String> properties) void
setReadOnlyByDefault
(@Nullable Boolean readOnlyByDefault) void
setRemoveAbandoned
(boolean removeAbandoned) void
setRemoveAbandonedTimeout
(Duration removeAbandonedTimeout) void
setRollbackOnReturn
(boolean rollbackOnReturn) void
void
setUseFairQueue
(boolean fair) void
void
setValidationInterval
(Duration validationInterval) void
setValidationQuery
(@Nullable String validationQuery) void
setValidationQueryTimeout
(@Nullable Duration validationQueryTimeout) void
setValidatorClassName
(Optional<String> validatorClassName)
-
Constructor Details
-
DataSourceFactory
public DataSourceFactory()
-
-
Method Details
-
isAutoCommentsEnabled
public boolean isAutoCommentsEnabled()Description copied from interface:PooledDataSourceFactory
Whether ORM tools allowed to add comments to SQL queries.- Specified by:
isAutoCommentsEnabled
in interfacePooledDataSourceFactory
- Returns:
true
, if allowed
-
setAutoCommentsEnabled
public void setAutoCommentsEnabled(boolean autoCommentsEnabled) -
getDriverClass
Description copied from interface:PooledDataSourceFactory
Returns the Java class of the database driver.- Specified by:
getDriverClass
in interfacePooledDataSourceFactory
- Returns:
- the JDBC driver class as a string
-
setDriverClass
-
getUser
-
setUser
-
getPassword
-
setPassword
-
getUrl
Description copied from interface:PooledDataSourceFactory
Returns the JDBC connection URL.- Specified by:
getUrl
in interfacePooledDataSourceFactory
- Returns:
- the JDBC connection URL as a string
-
setUrl
-
getProperties
Description copied from interface:PooledDataSourceFactory
Returns the configuration properties for ORM tools.- Specified by:
getProperties
in interfacePooledDataSourceFactory
- Returns:
- configuration properties as a map
-
setProperties
-
getMaxWaitForConnection
-
setMaxWaitForConnection
-
getValidationQuery
Description copied from interface:PooledDataSourceFactory
Returns the SQL query, which is being used for the database connection health check.- Specified by:
getValidationQuery
in interfacePooledDataSourceFactory
- Returns:
- the SQL query as a string
-
setValidationQuery
-
getMinSize
public int getMinSize() -
setMinSize
public void setMinSize(int minSize) -
getMaxSize
public int getMaxSize() -
setMaxSize
public void setMaxSize(int maxSize) -
getCheckConnectionWhileIdle
public boolean getCheckConnectionWhileIdle() -
setCheckConnectionWhileIdle
public void setCheckConnectionWhileIdle(boolean checkConnectionWhileIdle) -
isMinSizeLessThanMaxSize
@ValidationMethod(message=".minSize must be less than or equal to maxSize") public boolean isMinSizeLessThanMaxSize() -
isInitialSizeLessThanMaxSize
@ValidationMethod(message=".initialSize must be less than or equal to maxSize") public boolean isInitialSizeLessThanMaxSize() -
isInitialSizeGreaterThanMinSize
@ValidationMethod(message=".initialSize must be greater than or equal to minSize") public boolean isInitialSizeGreaterThanMinSize() -
getAbandonWhenPercentageFull
public int getAbandonWhenPercentageFull() -
setAbandonWhenPercentageFull
public void setAbandonWhenPercentageFull(int percentage) -
isAlternateUsernamesAllowed
public boolean isAlternateUsernamesAllowed() -
setAlternateUsernamesAllowed
public void setAlternateUsernamesAllowed(boolean allow) -
getCommitOnReturn
public boolean getCommitOnReturn() -
getRollbackOnReturn
public boolean getRollbackOnReturn() -
setCommitOnReturn
public void setCommitOnReturn(boolean commitOnReturn) -
setRollbackOnReturn
public void setRollbackOnReturn(boolean rollbackOnReturn) -
getAutoCommitByDefault
-
setAutoCommitByDefault
-
getDefaultCatalog
-
setDefaultCatalog
-
getReadOnlyByDefault
-
setReadOnlyByDefault
-
getDefaultTransactionIsolation
-
setDefaultTransactionIsolation
-
getUseFairQueue
public boolean getUseFairQueue() -
setUseFairQueue
public void setUseFairQueue(boolean fair) -
getInitialSize
public int getInitialSize() -
setInitialSize
public void setInitialSize(int initialSize) -
getInitializationQuery
-
setInitializationQuery
-
getLogAbandonedConnections
public boolean getLogAbandonedConnections() -
setLogAbandonedConnections
public void setLogAbandonedConnections(boolean log) -
getLogValidationErrors
public boolean getLogValidationErrors() -
setLogValidationErrors
public void setLogValidationErrors(boolean log) -
getMaxConnectionAge
-
setMaxConnectionAge
-
getMinIdleTime
-
setMinIdleTime
-
getCheckConnectionOnBorrow
public boolean getCheckConnectionOnBorrow() -
setCheckConnectionOnBorrow
public void setCheckConnectionOnBorrow(boolean checkConnectionOnBorrow) -
getCheckConnectionOnConnect
public boolean getCheckConnectionOnConnect() -
setCheckConnectionOnConnect
public void setCheckConnectionOnConnect(boolean checkConnectionOnConnect) -
getCheckConnectionOnReturn
public boolean getCheckConnectionOnReturn() -
setCheckConnectionOnReturn
public void setCheckConnectionOnReturn(boolean checkConnectionOnReturn) -
getEvictionInterval
-
setEvictionInterval
-
getValidationInterval
-
setValidationInterval
-
getValidationQueryTimeout
Description copied from interface:PooledDataSourceFactory
Returns the timeout for awaiting a response from the database during connection health checks.- Specified by:
getValidationQueryTimeout
in interfacePooledDataSourceFactory
- Returns:
- the timeout as
Duration
-
getValidatorClassName
-
setValidatorClassName
-
setValidationQueryTimeout
-
isRemoveAbandoned
public boolean isRemoveAbandoned() -
setRemoveAbandoned
public void setRemoveAbandoned(boolean removeAbandoned) -
getRemoveAbandonedTimeout
-
setRemoveAbandonedTimeout
-
getJdbcInterceptors
-
setJdbcInterceptors
-
isIgnoreExceptionOnPreLoad
public boolean isIgnoreExceptionOnPreLoad() -
setIgnoreExceptionOnPreLoad
public void setIgnoreExceptionOnPreLoad(boolean ignoreExceptionOnPreLoad) -
asSingleConnectionPool
public void asSingleConnectionPool()Description copied from interface:PooledDataSourceFactory
Configures the pool as a single connection pool. It's useful for tools that use only one database connection, such as database migrations.- Specified by:
asSingleConnectionPool
in interfacePooledDataSourceFactory
-
build
Description copied from interface:PooledDataSourceFactory
Builds a new JDBC data source backed by the connection pool and managed by Dropwizard.- Specified by:
build
in interfacePooledDataSourceFactory
- Parameters:
metricRegistry
- the application metric registryname
- name of the connection pool- Returns:
- a new JDBC data source as
ManagedDataSource
-