Package io.dropwizard.db
Class DataSourceFactory
- java.lang.Object
-
- io.dropwizard.db.DataSourceFactory
-
- All Implemented Interfaces:
PooledDataSourceFactory
public class DataSourceFactory extends Object implements PooledDataSourceFactory
A factory for pooledManagedDataSources. Configuration Parameters:Name Default Description urlREQUIRED The URL of the server. driverClassnone 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.usernone The username used to connect to the server. passwordnone The password used to connect to the server. removeAbandonedfalseRemove abandoned connections if they exceed the removeAbandonedTimeout. If set totruea connection is considered abandoned and eligible for removal if it has been in use longer than theremoveAbandonedTimeoutand the condition forabandonWhenPercentageFullis met.removeAbandonedTimeout60 seconds The time before a database connection can be considered abandoned. abandonWhenPercentageFull0 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.alternateUsernamesAllowedfalseSet 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.commitOnReturnfalseSet to true if you want the connection pool to commit any pending transaction when a connection is returned. rollbackOnReturnfalseSet to true if you want the connection pool to rollback any pending transaction when a connection is returned. autoCommitByDefaultJDBC driver's default The default auto-commit state of the connections. readOnlyByDefaultJDBC driver's default The default read-only state of the connections. propertiesnone Any additional JDBC driver parameters. defaultCatalognone The default catalog to use for the connections. defaultTransactionIsolationJDBC driver default The default transaction isolation to use for the connections. Can be one of none,default,read-uncommitted,read-committed,repeatable-read, orserializable.useFairQueuetrueIf true, calls togetConnectionare handled in a FIFO manner.initialSize10 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 minSizemust also be set to zero.minSize10 The minimum size of the connection pool. maxSize100 The maximum size of the connection pool. initializationQuerynone A custom query to be run when a connection is first created. logAbandonedConnectionsfalseIf true, logs stack traces of abandoned connections.logValidationErrorsfalseIf true, logs errors when connections fail validation.maxConnectionAgenone If set, connections which have been open for longer than maxConnectionAgeare closed when returned.maxWaitForConnection30 seconds If a request for a connection is blocked for longer than this period, an exception will be thrown. minIdleTime1 minute The minimum amount of time an connection must sit idle in the pool before it is eligible for eviction. validationQuerySELECT 1The 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. validationQueryTimeoutnone The timeout before a connection validation queries fail. checkConnectionWhileIdletrueSet to true if query validation should take place while the connection is idle. checkConnectionOnBorrowfalseWhether 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. checkConnectionOnConnecttrueWhether 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. checkConnectionOnReturnfalseWhether 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. autoCommentsEnabledtrueWhether or not ORMs should automatically add comments. evictionInterval5 seconds The amount of time to sleep between runs of the idle connection validation, abandoned cleaner and idle pool resizing. validationInterval30 seconds To avoid excess validation, only run validation once every interval. validatorClassName(none) Name of a class of a custom Validatorimplementation, which will be used for validating connections.jdbcInterceptors(none) A semicolon separated list of classnames extending JdbcInterceptorignoreExceptionOnPreLoadfalseFlag 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 Modifier and Type Class Description static classDataSourceFactory.TransactionIsolation
-
Constructor Summary
Constructors Constructor Description DataSourceFactory()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidasSingleConnectionPool()Configures the pool as a single connection pool.ManagedDataSourcebuild(com.codahale.metrics.MetricRegistry metricRegistry, String name)Builds a new JDBC data source backed by the connection pool and managed by Dropwizard.intgetAbandonWhenPercentageFull()@Nullable BooleangetAutoCommitByDefault()booleangetCheckConnectionOnBorrow()booleangetCheckConnectionOnConnect()booleangetCheckConnectionOnReturn()booleangetCheckConnectionWhileIdle()booleangetCommitOnReturn()@Nullable StringgetDefaultCatalog()DataSourceFactory.TransactionIsolationgetDefaultTransactionIsolation()@Nullable StringgetDriverClass()Returns the Java class of the database driver.DurationgetEvictionInterval()@Nullable StringgetInitializationQuery()intgetInitialSize()Optional<String>getJdbcInterceptors()booleangetLogAbandonedConnections()booleangetLogValidationErrors()Optional<Duration>getMaxConnectionAge()intgetMaxSize()DurationgetMaxWaitForConnection()DurationgetMinIdleTime()intgetMinSize()@Nullable StringgetPassword()Map<String,String>getProperties()Returns the configuration properties for ORM tools.@Nullable BooleangetReadOnlyByDefault()DurationgetRemoveAbandonedTimeout()booleangetRollbackOnReturn()StringgetUrl()Returns the JDBC connection URL.booleangetUseFairQueue()@Nullable StringgetUser()DurationgetValidationInterval()Optional<String>getValidationQuery()Returns the SQL query, which is being used for the database connection health check.Optional<Duration>getValidationQueryTimeout()Returns the timeout for awaiting a response from the database during connection health checks.Optional<String>getValidatorClassName()booleanisAlternateUsernamesAllowed()booleanisAutoCommentsEnabled()Whether ORM tools allowed to add comments to SQL queries.booleanisIgnoreExceptionOnPreLoad()booleanisInitialSizeGreaterThanMinSize()booleanisInitialSizeLessThanMaxSize()booleanisMinSizeLessThanMaxSize()booleanisRemoveAbandoned()voidsetAbandonWhenPercentageFull(int percentage)voidsetAlternateUsernamesAllowed(boolean allow)voidsetAutoCommentsEnabled(boolean autoCommentsEnabled)voidsetAutoCommitByDefault(@Nullable Boolean autoCommit)voidsetCheckConnectionOnBorrow(boolean checkConnectionOnBorrow)voidsetCheckConnectionOnConnect(boolean checkConnectionOnConnect)voidsetCheckConnectionOnReturn(boolean checkConnectionOnReturn)voidsetCheckConnectionWhileIdle(boolean checkConnectionWhileIdle)voidsetCommitOnReturn(boolean commitOnReturn)voidsetDefaultCatalog(@Nullable String defaultCatalog)voidsetDefaultTransactionIsolation(DataSourceFactory.TransactionIsolation isolation)voidsetDriverClass(@Nullable String driverClass)voidsetEvictionInterval(Duration interval)voidsetIgnoreExceptionOnPreLoad(boolean ignoreExceptionOnPreLoad)voidsetInitializationQuery(@Nullable String query)voidsetInitialSize(int initialSize)voidsetJdbcInterceptors(Optional<String> jdbcInterceptors)voidsetLogAbandonedConnections(boolean log)voidsetLogValidationErrors(boolean log)voidsetMaxConnectionAge(@Nullable Duration age)voidsetMaxSize(int maxSize)voidsetMaxWaitForConnection(Duration maxWaitForConnection)voidsetMinIdleTime(Duration time)voidsetMinSize(int minSize)voidsetPassword(@Nullable String password)voidsetProperties(Map<String,String> properties)voidsetReadOnlyByDefault(@Nullable Boolean readOnlyByDefault)voidsetRemoveAbandoned(boolean removeAbandoned)voidsetRemoveAbandonedTimeout(Duration removeAbandonedTimeout)voidsetRollbackOnReturn(boolean rollbackOnReturn)voidsetUrl(String url)voidsetUseFairQueue(boolean fair)voidsetUser(@Nullable String user)voidsetValidationInterval(Duration validationInterval)voidsetValidationQuery(@Nullable String validationQuery)voidsetValidationQueryTimeout(@Nullable Duration validationQueryTimeout)voidsetValidatorClassName(Optional<String> validatorClassName)
-
-
-
Method Detail
-
isAutoCommentsEnabled
public boolean isAutoCommentsEnabled()
Description copied from interface:PooledDataSourceFactoryWhether ORM tools allowed to add comments to SQL queries.- Specified by:
isAutoCommentsEnabledin interfacePooledDataSourceFactory- Returns:
true, if allowed
-
setAutoCommentsEnabled
public void setAutoCommentsEnabled(boolean autoCommentsEnabled)
-
getDriverClass
public @Nullable String getDriverClass()
Description copied from interface:PooledDataSourceFactoryReturns the Java class of the database driver.- Specified by:
getDriverClassin interfacePooledDataSourceFactory- Returns:
- the JDBC driver class as a string
-
setDriverClass
public void setDriverClass(@Nullable String driverClass)
-
getUser
public @Nullable String getUser()
-
setUser
public void setUser(@Nullable String user)
-
getPassword
public @Nullable String getPassword()
-
setPassword
public void setPassword(@Nullable String password)
-
getUrl
public String getUrl()
Description copied from interface:PooledDataSourceFactoryReturns the JDBC connection URL.- Specified by:
getUrlin interfacePooledDataSourceFactory- Returns:
- the JDBC connection URL as a string
-
setUrl
public void setUrl(String url)
-
getProperties
public Map<String,String> getProperties()
Description copied from interface:PooledDataSourceFactoryReturns the configuration properties for ORM tools.- Specified by:
getPropertiesin interfacePooledDataSourceFactory- Returns:
- configuration properties as a map
-
getMaxWaitForConnection
public Duration getMaxWaitForConnection()
-
setMaxWaitForConnection
public void setMaxWaitForConnection(Duration maxWaitForConnection)
-
getValidationQuery
public Optional<String> getValidationQuery()
Description copied from interface:PooledDataSourceFactoryReturns the SQL query, which is being used for the database connection health check.- Specified by:
getValidationQueryin interfacePooledDataSourceFactory- Returns:
- the SQL query as a string
-
setValidationQuery
public void setValidationQuery(@Nullable String validationQuery)
-
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
public @Nullable Boolean getAutoCommitByDefault()
-
setAutoCommitByDefault
public void setAutoCommitByDefault(@Nullable Boolean autoCommit)
-
getDefaultCatalog
public @Nullable String getDefaultCatalog()
-
setDefaultCatalog
public void setDefaultCatalog(@Nullable String defaultCatalog)
-
getReadOnlyByDefault
public @Nullable Boolean getReadOnlyByDefault()
-
setReadOnlyByDefault
public void setReadOnlyByDefault(@Nullable Boolean readOnlyByDefault)
-
getDefaultTransactionIsolation
public DataSourceFactory.TransactionIsolation getDefaultTransactionIsolation()
-
setDefaultTransactionIsolation
public void setDefaultTransactionIsolation(DataSourceFactory.TransactionIsolation isolation)
-
getUseFairQueue
public boolean getUseFairQueue()
-
setUseFairQueue
public void setUseFairQueue(boolean fair)
-
getInitialSize
public int getInitialSize()
-
setInitialSize
public void setInitialSize(int initialSize)
-
getInitializationQuery
public @Nullable String getInitializationQuery()
-
setInitializationQuery
public void setInitializationQuery(@Nullable String query)
-
getLogAbandonedConnections
public boolean getLogAbandonedConnections()
-
setLogAbandonedConnections
public void setLogAbandonedConnections(boolean log)
-
getLogValidationErrors
public boolean getLogValidationErrors()
-
setLogValidationErrors
public void setLogValidationErrors(boolean log)
-
setMaxConnectionAge
public void setMaxConnectionAge(@Nullable Duration age)
-
getMinIdleTime
public Duration getMinIdleTime()
-
setMinIdleTime
public void setMinIdleTime(Duration time)
-
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
public Duration getEvictionInterval()
-
setEvictionInterval
public void setEvictionInterval(Duration interval)
-
getValidationInterval
public Duration getValidationInterval()
-
setValidationInterval
public void setValidationInterval(Duration validationInterval)
-
getValidationQueryTimeout
public Optional<Duration> getValidationQueryTimeout()
Description copied from interface:PooledDataSourceFactoryReturns the timeout for awaiting a response from the database during connection health checks.- Specified by:
getValidationQueryTimeoutin interfacePooledDataSourceFactory- Returns:
- the timeout as
Duration
-
setValidationQueryTimeout
public void setValidationQueryTimeout(@Nullable Duration validationQueryTimeout)
-
isRemoveAbandoned
public boolean isRemoveAbandoned()
-
setRemoveAbandoned
public void setRemoveAbandoned(boolean removeAbandoned)
-
getRemoveAbandonedTimeout
public Duration getRemoveAbandonedTimeout()
-
setRemoveAbandonedTimeout
public void setRemoveAbandonedTimeout(Duration removeAbandonedTimeout)
-
isIgnoreExceptionOnPreLoad
public boolean isIgnoreExceptionOnPreLoad()
-
setIgnoreExceptionOnPreLoad
public void setIgnoreExceptionOnPreLoad(boolean ignoreExceptionOnPreLoad)
-
asSingleConnectionPool
public void asSingleConnectionPool()
Description copied from interface:PooledDataSourceFactoryConfigures the pool as a single connection pool. It's useful for tools that use only one database connection, such as database migrations.- Specified by:
asSingleConnectionPoolin interfacePooledDataSourceFactory
-
build
public ManagedDataSource build(com.codahale.metrics.MetricRegistry metricRegistry, String name)
Description copied from interface:PooledDataSourceFactoryBuilds a new JDBC data source backed by the connection pool and managed by Dropwizard.- Specified by:
buildin interfacePooledDataSourceFactory- Parameters:
metricRegistry- the application metric registryname- name of the connection pool- Returns:
- a new JDBC data source as
ManagedDataSource
-
-