db.setting Examples

db.setting Examples

Introduction

DsFactory reads the configuration file config/db.setting or db.setting by default. The configuration of db.setting includes two parts: basic connection information and connection pool configuration information.

The basic connection information is supported by all connection pools, while the connection pool configuration information is transplanted from the corresponding configuration items of the connection pool.

Basic Configuration Example

#------------------------------------------------------------------------------------------
## Basic Configuration Information
# JDBC URL, use the corresponding JDBC connection string according to different databases
url = jdbc:mysql://<host>:<port>/<database_name>
# Username, can also use "user" instead
username = username
# Password, can also use "pass" instead
password = password
# JDBC driver name, optional (Hutool will automatically identify)
driver = com.mysql.jdbc.Driver

## Optional Configuration
# Whether to display the executed SQL in the log
showSql = true
# Whether to format the displayed SQL
formatSql = false
# Whether to display SQL parameters
showParams = true
# Log level for printing SQL, default is debug
sqlLevel = debug
#------------------------------------------------------------------------------------------

HikariCP

## Connection Pool Configuration Items
# Autocommit
autoCommit = true
# Maximum time to wait for a connection from the pool (in milliseconds), if exceeded, a SQLException will occur. Default: 30 seconds
connectionTimeout = 30000
# Maximum idle time of a connection (in milliseconds), if exceeded, the connection will be released (retired). Default: 10 minutes
idleTimeout = 600000
# Maximum lifetime of a connection (in milliseconds), if exceeded and not in use, the connection will be released (retired). Default: 30 minutes. It is recommended to set it 30 seconds less than the database timeout, referring to the MySQL wait_timeout parameter (show variables like '%timeout%';)
maxLifetime = 1800000
# Test SQL before getting a connection
connectionTestQuery = SELECT 1
# Minimum number of idle connections
minimumIdle = 10
# Maximum number of connections allowed in the pool. Default value: 10; recommended formula: ((core_count * 2) + effective_spindle_count)
maximumPoolSize = 10
# Set to true when connecting to a read-only database to ensure security
readOnly = false

Druid

# Number of physical connections established during initialization. Initialization occurs when the init method is explicitly called or when getConnection is called for the first time.
initialSize = 0
# Maximum number of connections in the pool.
maxActive = 8
# Minimum number of idle connections in the pool.
minIdle = 0
# Maximum wait time for obtaining a connection, in milliseconds. After configuring maxWait, the fair lock is enabled by default, which may reduce concurrency efficiency. If needed, you can use an unfair lock by configuring the useUnfairLock property as true.
maxWait = 0
# Whether to cache preparedStatements, i.e., PSCache. PSCache significantly improves performance for databases that support cursors, such as Oracle. In versions of MySQL below 5.5, there is no PSCache functionality, so it is recommended to turn it off. The author used PSCache in version 5.5 and found that there was a cache hit rate record in the monitoring interface, indicating that PSCache was supported.
poolPreparedStatements = false
# To enable PSCache, this value must be set to a number greater than 0. When it is greater than 0, poolPreparedStatements is automatically set to true. In Druid, there will be no issue of PSCache consuming too much memory as in Oracle. You can set this value to a larger number, such as 100.
maxOpenPreparedStatements = -1
# SQL query used to check the validity of the connection. It must be a select statement. If validationQuery is null, testOnBorrow, testOnReturn, and testWhileIdle will not take effect.
validationQuery = SELECT 1
# Perform validationQuery to check the validity of the connection when requesting a connection. Enabling this configuration will reduce performance.
testOnBorrow = true
# Perform validationQuery to check the validity of the connection when returning a connection. Enabling this configuration will reduce performance.
testOnReturn = false
# It is recommended to set this to true without affecting performance and ensuring security. When requesting a connection, if the idle time is greater than timeBetweenEvictionRunsMillis, perform validationQuery to check the validity of the connection.
testWhileIdle = false
# It has two meanings: 1) The interval at which the Destroy thread checks connections, and 2) the basis for judging testWhileIdle. Please refer to the description of the testWhileIdle property for details.
timeBetweenEvictionRunsMillis = 60000
# SQL executed when initializing a physical connection
connectionInitSqls = SELECT 1
# The property type is a string, and extended plugins are configured through aliases. Commonly used plugins include: filter:stat for monitoring and statistics, filter:log4j for logging, and filter:wall for preventing SQL injection.
filters = stat
# The type is List<com.alibaba.druid.filter.Filter>. If both filters and proxyFilters are configured, they are combined rather than replaced.
proxyFilters =

Tomcat JDBC Pool

# (boolean) The default auto-commit status of connections created by the connection pool
defaultAutoCommit = true
# (boolean) The default read-only status of connections created by the connection pool. If not set, the setReadOnly method will not be called. (Some drivers do not support read-only mode, such as Informix)
defaultReadOnly = false
# (String) The default TransactionIsolation status of connections created by the connection pool. One of the following: (refer to the javadoc) NONE, READ_COMMITTED, READ_UNCOMMITTED, REPEATABLE_READ, SERIALIZABLE
defaultTransactionIsolation = NONE
# (int) Initialize connections: the number of initial connections created when the connection pool starts, supported after version 1.2
initialSize = 10
# (int) Max active connections: the maximum number of active connections that the connection pool can allocate at the same time. If set to a non-positive number, there is no limit
maxActive = 100
# (int) Max idle connections: the maximum number of idle connections allowed in the connection pool. Excess idle connections will be released. If set to a negative number, there is no limit. If enabled, idle connections will be periodically checked and released if their idle time exceeds minEvictableIdleTimeMillis (refer to testWhileIdle)
maxIdle = 8
# (int) Min idle connections: the minimum number of idle connections allowed in the connection pool. If the number falls below this, new connections will be created. If set to 0, no new connections will be created. If connection validation fails, this value will be reduced (refer to testWhileIdle)
minIdle = 0
# (int) Max wait time: the maximum time (in milliseconds) that the connection pool will wait for a connection to be returned when there are no available connections. If the time is exceeded, an exception will be thrown. If set to -1, it will wait indefinitely
maxWait = 30000
# (String) SQL query used to validate connections taken from the connection pool before returning them to the caller. If specified, the query must be a SQL SELECT and must return at least one row. The query does not have to return records, but it cannot throw SQL exceptions
validationQuery = SELECT 1
# (boolean) Indicates whether to perform a validation before taking a connection from the pool. If the validation fails, the connection will be removed from the pool and another one will be tried. Note: to take effect, set this to true and set the validationQuery parameter to a non-empty string. Refer to validationInterval for more effective validation
testOnBorrow = false
# (boolean) Indicates whether to perform a validation before returning a connection to the pool. Note: to take effect, set this to true and set the validationQuery parameter to a non-empty string
testOnReturn = false
# (boolean) Indicates whether connections are validated by the idle connection evictor (if any). If the validation fails, the connection will be removed from the pool. Note: to take effect, set this to true and set the validationQuery parameter to a non-empty string
testWhileIdle = false

C3P0 (Not Recommended)

# The maximum number of connections to be held in the connection pool. Default: 15
maxPoolSize = 15
# The minimum number of connections to be held in the connection pool. Default: 3
minPoolSize = 3
# The number of connections to be initially created in the connection pool, should be between minPoolSize and maxPoolSize. Default: 3
initialPoolSize = 3
# The maximum idle time of a connection, if a connection is not used within 60 seconds, it will be discarded. If set to 0, it will never be discarded. Default: 0
maxIdleTime = 0
# When the connection pool is exhausted, the time for a client to wait for a new connection after calling getConnection(), if it exceeds the timeout, it will throw SQLException. If set to 0, it will wait indefinitely. Unit: milliseconds. Default: 0
checkoutTimeout = 0
# The number of connections that c3p0 acquires at once when the connection pool is exhausted. Default: 3
acquireIncrement = 3
# The number of times to retry after failing to acquire a new connection from the database. Default: 30; if set to 0 or less, it means retrying indefinitely
acquireRetryAttempts = 0
# The time interval between retries, default: 1000 milliseconds
acquireRetryDelay = 1000
# Whether to commit uncommitted transactions when closing a connection. Default: false, i.e., close the connection and roll back uncommitted transactions
autoCommitOnClose = false
# c3p0 will create an empty table named Test and use its own query statement to test it. If this parameter is defined, the preferredTestQuery attribute will be ignored. You cannot perform any operations on this Test table, it is only used by c3p0 for testing. Default: null
automaticTestTable = null
# If set to false, a failure to acquire a connection will cause all threads waiting for a connection from the connection pool to throw an exception, but the data source will remain valid and continue to try to acquire a connection the next time getConnection() is called. If set to true, the data source will declare itself as disconnected and permanently closed after a failed attempt to acquire a connection. Default: false
breakAfterAcquireFailure = false
# The frequency of checking all idle connections in the connection pool. Default: 0, no checking
idleConnectionTestPeriod = 0
# The size of c3p0's global PreparedStatement cache. If both maxStatements and maxStatementsPerConnection are 0, the cache will not take effect. As long as one of them is not 0, the statement cache will take effect. Default: 0
maxStatements = 0
# maxStatementsPerConnection defines the maximum number of cached statements that a single connection in the connection pool can have. Default: 0
maxStatementsPerConnection = 0

DBCP (Not Recommended)

# (boolean) The default auto-commit status of connections created by the connection pool
defaultAutoCommit = true
# (boolean) The default read-only status of connections created by the connection pool. If not set, the setReadOnly method will not be called. (Some drivers do not support read-only mode, such as Informix)
defaultReadOnly = false
# (String) The default TransactionIsolation status of connections created by the connection pool. One of the following: (refer to the javadoc) NONE, READ_COMMITTED, READ_UNCOMMITTED, REPEATABLE_READ, SERIALIZABLE
defaultTransactionIsolation = NONE
# (int) Initialize connections: the number of initial connections created when the connection pool starts, supported after version 1.2
initialSize = 10
# (int) Max active connections: the maximum number of active connections that the connection pool can allocate at the same time. If set to a non-positive number, there is no limit
maxActive = 100
# (int) Max idle connections: the maximum number of idle connections allowed in the connection pool. Excess idle connections will be released. If set to a negative number, there is no limit. If enabled, idle connections will be periodically checked and released if their idle time exceeds minEvictableIdleTimeMillis (refer to testWhileIdle)
maxIdle = 8
# (int) Min idle connections: the minimum number of idle connections allowed in the connection pool. If the number falls below this, new connections will be created. If set to 0, no new connections will be created. If connection validation fails, this value will be reduced (refer to testWhileIdle)
minIdle = 0
# (int) Max wait time: the maximum time (in milliseconds) that the connection pool will wait for a connection to be returned when there are no available connections. If the time is exceeded, an exception will be thrown. If set to -1, it will wait indefinitely
maxWait = 30000
# (String) SQL query used to validate connections taken from the connection pool before returning them to the caller. If specified, the query must be a SQL SELECT and must return at least one row. The query does not have to return records, but it cannot throw SQL exceptions
validationQuery = SELECT 1
# (boolean) Indicates whether to perform a validation before taking a connection from the pool. If the validation fails, the connection will be removed from the pool and another one will be tried. Note: to take effect, set this to true and set the validationQuery parameter to a non-empty string. Refer to validationInterval for more effective validation
testOnBorrow = false
# (boolean) Indicates whether to perform a validation before returning a connection to the pool. Note: to take effect, set this to true and set the validationQuery parameter to a non-empty string
testOnReturn = false
# (boolean) Indicates whether connections are validated by the idle connection evictor (if any). If the validation fails, the connection will be removed from the pool. Note: to take effect, set this to true and set the validationQuery parameter to a non-empty string
testWhileIdle = false