各类数据库联接池(DBCP、c3p0、Druid)配置内容转载

原创
小哥 3年前 (2022-11-11) 阅读数 8 #大杂烩
  1. 引言

1.1 定义

数据库连接是一种关键的有限且昂贵的资源。 多用户 Web应用程序尤为突出。数据库连接的管理会显著影响整个应用程序的可伸缩性和可伸缩性。 健壮性 ,这会影响程序的性能指标。数据库连接池专门针对这个问题。

数据库连接池负责分配、管理和释放数据库连接,这允许应用程序重用现有数据库连接,而不是重新建立数据库连接。该技术可以显著提高数据库操作的性能。

1.2 参考资料

DBCP

下载地址: http://commons.apache.org/proper/commons-dbcp/download_dbcp.cgi

相关属性说明: http://commons.apache.org/proper/commons-dbcp/configuration.html

c3p0

下载地址: http://sourceforge.net/projects/c3p0/

相关属性说明: http://www.mchange.com/projects/c3p0/

Druid

下载地址: http://repo1.maven.org/maven2/com/alibaba/druid/

相关属性说明: https://github.com/alibaba/druid/wiki

  1. 数据库连接池

2.1 原理

连接池的基本思想是在系统初始化时将数据库连接作为对象存储在内存中。当用户需要访问数据库时,将从连接池中取出已建立的空闲连接对象,而不是建立新连接。使用后,用户不会关闭连接,而是将连接放回连接池,以便下次请求访问。连接的建立和断开由连接池本身管理。同时,您还可以设置连接池的参数,以控制连接池中的初始连接数、连接的上限和下限数、每个连接的最大使用次数、最大空闲时间等,或者通过其自身的管理机制来监控数据库连接数、使用情况等。

2.2 公共数据库连接池及其特性。

在Java有几种常见的数据库连接池是开源的。 :

1)DBCP

DBCP是依赖项Jakarta commons-pool对象池机制的数据库连接池。.DBCP可直接用于应用,Tomcat的数据源为DBCP。

2)c3p0

c3p0是一个开放源代码JDBC连接池,位于lib目录中与Hibernate一起发布,包括实施jdbc3和jdbc2扩展规格说明。Connection 和Statement 池的DataSources 对象。

3)Druid

阿里产品、淘宝和支付宝专用数据库连接池,但它不仅是一个数据库连接池ProxyDriver,一系列内置JDBC组件库SQL Parser.支持所有人JDBC兼容的数据库,包括Oracle、MySql、Derby、Postgresql、SQL Server、H2等等。

Druid针对Oracle和MySql已经进行了特殊优化,例如Oracle的PS Cache内存占用优化,MySql的ping检测优化。

Druid提供了MySql、Oracle、Postgresql、SQL-92的SQL完全支持,这是手写的高性能SQL Parser,支持Visitor模式,进行分析SQL抽象语法树非常方便。

简单SQL语句用时10在微秒内,复杂SQL用时30微秒。

通过Druid提供的SQL Parser可以在JDBC层拦截SQL做相应的处理,如子库子表、审计等。Druid防御SQL注入攻击WallFilter就是通过Druid的SQL Parser语义实现分析。

  1. 主要配置说明

连接池配置可以大致分为基本配置、关键配置和性能配置。

3.1 基本配置

基本配置是指数据库连接所需的连接池的四个基本配置:

传递给JDBC用户名、密码和,URL以及驱动程序类名。

DBCP

c3p0

Druid

用户名

username

user

username

密码

password

password

password

URL

url

jdbcUrl

jdbcUrl

驱动类名

driverClassName

driverClass

driverClassName

注:在Druid在连接池的配置中,driverClassName可以配备不能匹配,如果不配置将基于。url自动识别dbType(数据库类型),然后选择适当的driverClassName。

3.2 关键配置

为了发挥数据库连接池的作用,在初始化期间将创建一定数量的数据库连接并将其放入连接池。这些数据库连接的数量由数据库连接的最小数量设置。无论是否使用这些数据库连接,连接池始终保证至少有这么多连接。连接池中的最大数据库连接数限制了此连接池可以占用的最大连接数,当应用程序请求连接池的连接数超过最大连接数时,这些请求将添加到等待队列中。

最小连接数 :

它是数据库一直在维护的数据库连接的数量,因此如果应用程序很少使用数据库连接,那么就会浪费大量数据库资源。

初始化连接:

启动连接池时创建的初始化数据库连接数。

最大连接数

是连接池可以应用的最大连接数。如果数据库连接请求超过此数目,则随后的数据库连接请求将添加到等待队列中。

最长等待时间:

当没有可用的连接时,连接池将在最长时间内等待连接返回,并在该时间后抛出一个可以设置的异常。0或者负数使无限等待(取决于连接池配置)。

DBCP

c3p0

Druid

最小连接数

minIdle(0)

minPoolSize(3)

minIdle(0)

初始化连接数。

initialSize(0)

initialPoolSize(3)

initialSize(0)

最大连接数

maxTotal(8)

maxPoolSize(15)

maxActive(8)

最长等待时间

maxWaitMillis(毫秒)

maxIdleTime(0秒)

maxWait(毫秒)

注1:在DBCP在在连接池的配置中,也有一个。maxIdle表示最大空闲连接数的属性将在空闲连接数超过时释放,默认值为8。相应的属性在中。Druid连接池不再使用,配置无效,c3p0连接池没有相应的属性。

注2:数据库连接池是在初始化期间创建的。initialSize当存在数据库操作时,将从池中删除连接。如果当前池中使用的连接数相等maxActive,将等待一段时间,等待其他操作释放连接,如果此等待时间超过maxWait,将报告错误;如果当前使用的连接数未达到maxActive,确定连接当前是否处于空闲状态,如果存在则直接使用空闲连接,如果没有则创建新连接。连接使用后,它将被放入池中,等待其他操作被重用,而不是关闭其物理连接。

3.3 性能配置

预缓存设置 :

即是PSCache,PSCache支持游标的数据库的显著性能改进,例如oracle。JDBC控制数据源中加载的标准参数。PreparedStatements量但是,由于预缓存statements属于单个connection不是整个连接池,因此设置此参数需要考虑许多因素。

单个连接具有的最大缓存数。:要启用PSCache,必须配置得更大0,当大于0时,poolPreparedStatements自动触发器已修改true。在Druid中,将不存在Oracle下PSCache对于占用太多内存的问题,可以将该值配置为更大,例如100

DBCP

c3p0

Druid

启用缓存

poolPreparedStatements

maxStatements

poolPreparedStatements

单个连接具有的最大缓存数。

maxOpenPrepared-

Statements

maxStatementsPer-

Connection

maxOpenPrepared-

Statements

连接有效性检测设置:

连接池中有一种机制可以确定当前连接总数是否小于miniIdle,建立新的空闲连接以确保获得连接的数量。miniIdle。如果当前连接池中的连接空闲timeBetweenEvictionRunsMillis如果在该时间之后不使用,它将被物理关闭。某些数据库在连接时有超时限制(mysql连接在8小时后断开连接),或由于网络中断等原因,连接池的连接将失败,这一次设置testWhileIdle参数为true,可以确保连接池内定期检测连接的可用性,不可用的连接将被丢弃或重建,并且将从连接池获得最大的保证。Connection对象可用。当然,为了确保绝对可用性,您也可以使用它。testOnBorrow为true(即,在获得Connection对象检测其可用性时),但这可能会影响性能。

DBCP

c3p0

Druid

请求连接检测

testOnBorrow

testConnectionOnCheckin

testOnBorrow

是否超时检测

testWhileIdle

testWhileIdle

空闲时间

timeBetweenEvictionRunsMillis

idleConnectionTestPeriod

timeBetweenEvictionRunsMillis

校验用sql语句

validationQuery

preferredTestQuery

validationQuery

返回连接检测

testOnReturn

testConnectionOnCheckout

testOnReturn

超时连接关闭设置 :

removeAbandoned参数,用于检测当前正在使用的连接是否已泄漏,因此在代码中,假定如果连接需要很长时间才能建立连接,则将其视为泄漏,然后强制关闭。

DBCP

c3p0

Druid

是否随时间关闭连接

removeAbandoned

breakAfterAcquireFailure

removeAbandoned

超时时间

removeAbandonedTimeout

checkoutTimeout

removeAbandonedTimeout

是否记录

logAbandoned

logAbandoned

c3p0 重连设置:

设置是否重新连接以及采集连接失败后的间隔。

DBCP

c3p0

Druid

重连次数

acquireRetryAttempts

间隔时间

acquireRetryDelay

  1. 配置详解

4.1 DBCP 属性描述表

属性(Parameter)

默认值(Default)

描述(Description)

username

传递给 JDBC 用于建立连接的用户名。 (The connection username to be passed to our JDBC driver to establish a connection.)

password

传递给 JDBC 用于建立连接的受驱动密码 (The connection password to be passed to our JDBC driver to establish a connection.)

url

传递给 JDBC 驱动程序用于建立连接。 URL (The connection URL to be passed to our JDBC driver to establish a connection.)

driverClassName

使用的 JDBC 全面有效地推动 java 类名 (The fully qualified Java class name of the JDBC driver to be used.)

defaultAutoCommit

driver default

连接池创建的默认连接。 auto-commit 状态 , 如果没有设置,则不会自动提交。 (The default auto-commit state of connections created by this pool. If not set then the setAutoCommit method will not be called.)

initialSize

0

初始化连接 : 连接池启动时创建的初始化连接数。量 (The initial number of connections that are created when the pool is started.

maxTotal

8

最大活动连接 : 连接池可以同时分配的最大活动连接数。 , 如果已设置非正数,则表示没有限制。 (The maximum number of active connections that can be allocated from this pool at the same time, or negative for no limit.)

maxIdle

8

最大空闲连接 : 连接池中允许保持空闲的最大连接数。 , 将释放超过的空闲连接 , 如果已设置负数表示无限制 (The maximum number of connections that can remain idle in the pool, without extra ones being released, or negative for no limit.)

minIdle

0

最小空闲连接 : 连接池中允许保持空闲的最小连接数。 , 负数表示不存在 (The maximum number of connections that can remain idle in the pool, without extra ones being released, or negative for no limit.)

注意 :如果在某些负载较高的系统中。 maxIdel 设置太长时间后,很可能会关闭连接并立即打开新连接。 . 这是因为关闭连接的线程比打开连接的线程更快。 . 所以,对于这个系统 ,maxIdle 设置不同,但通常首选默认值。

(NOTE: If maxIdle is set too low on heavily loaded systems it is possible you will see connections being closed and almost immediately new connections being opened. This is a result of the active threads momentarily closing connections faster than they are opening them, causing the number of idle connections to rise above maxIdle. The best value for maxIdle for heavily loaded system will vary but the default is a good starting point.)

maxWaitMillis

indefinitely

最长等待时间 : 当没有可用连接时 , 连接池等待连接返回的最长时间。 ( 计数(毫秒) ), 在时间之后引发异常。 , 如果已设置 -1 表示无限等待 (The maximum number of milliseconds that the pool will wait (when there are no available connections) for a connection to be returned before throwing an exception, or -1 to wait indefinitely.)

validationQuery

SQL 查询 , 用于验证从连接池中取出的连接。 , 在将连接返回给呼叫方之前 . 如果指定 , 那么查询必须是 SQL SELECT 并且必须返回至少一行记录 (The SQL query that will be used to validate connections from this pool before returning them to the caller. If specified, this query MUST be an SQL SELECT statement that returns at least one row. If not specified, connections will be validation by calling the isValid() method.)

testOnCreate

false

指示是否在建立连接后进行验证 , 如果验证失败 , 然后尝试重新建立连接 (The indication of whether objects will be validated after creation. If the object fails to validate, the borrow attempt that triggered the object creation will fail.)

testOnBorrow

true

指示是否在从池中删除连接之前进行检查 , 如果测试失败 , 然后从池中删除连接并尝试删除另一个 . 注意 : 设置为 true 之后,如果你想生效 ,validationQuery 参数必须设置为非空字符串。。 (The indication of whether objects will be validated before being borrowed from the pool. If the object fails to validate, it will be dropped from the pool, and we will attempt to borrow another.)

testOnReturn

false

指示是否在返回游泳池之前进行检查 (The indication of whether objects will be validated before being returned to the pool.)

testWhileIdle

false

指示连接收集器是否空闲连接。 ( 如果有 ) 进行检验 . 如果检测失败 , 连接将从池中删除。 . 注意 : 设置为 true 之后,如果你想生效 ,validationQuery 参数必须设置为非空字符串。。 (The indication of whether objects will be validated by the idle object evictor (if any). If an object fails to validate, it will be dropped from the pool.)

timeBetweenEviction-

RunsMillis

-1

空闲连接收集器线程期间休眠的时间值。 , 以毫秒为单位 . 如果已设置非正数 , 空闲连接收集器线程未运行。 (The number of milliseconds to sleep between runs of the idle object evictor thread. When non-positive, no idle object evictor thread will be run.)

numTestsPerEvictionRun

3

在每个空闲连接收集器线程中 ( 如果有 ) 运行时检查的连接数 (The number of objects to examine during each run of the idle object evictor thread (if any).)

minEvictableIdleTime-Millis

10006030

连接在池中保持空闲,而不会被连接收集器线程空闲。 ( 如果有 ) 恢复的最小时间值(毫秒)。 (The minimum amount of time an object may sit idle in the pool before it is eligable for eviction by the idle object evictor (if any).)

softMiniEvictableIdle- TimeMillis

-1

说明 (The minimum amount of time a connection may sit idle in the pool before it is eligible for eviction by the idle connection evictor, with the extra condition that at least "minIdle" connections remain in the pool. When miniEvictableIdleTimeMillis is set to a positive value, miniEvictableIdleTimeMillis is examined first by the idle connection evictor - i.e. when idle connections are visited by the evictor, idle time is first compared against miniEvictableIdleTimeMillis (without considering the number of idle connections in the pool) and then against softMinEvictableIdleTimeMillis, including the minIdle constraint.)

maxConnLifetimeMillis

-1

说明 (The maximum lifetime in milliseconds of a connection. After this time is exceeded the connection will fail the next activation, passivation or validation test. A value of zero or less means the connection has an infinite lifetime.)

logExpiredConnections

true

说明 (Flag to log a message indicating that a connection is being closed by the pool due to maxConnLifetimeMillis exceeded. Set this property to false to suppress expired connection logging that is turned on by default.

connectionInitSqls

null

说明 (A Collection of SQL statements that will be used to initialize physical connections when they are first created. These statements are executed only once - when the configured connection factory creates the connection.)

info

true

说明 (True means that borrowObject returns the most recently used ("last in") connection in the pool (if there are idle connections available). False means that the pool behaves as a FIFO queue - connections are taken from the idle instance pool in the order that they are returned to the pool.)

poolPreparedState-ments

false

开启池的 prepared statement 池功能 (Enable prepared statement pooling for this pool.)

maxOpenPreparedState-ments

unlimited

statement 池能够分配开放 statements 最大数量 , 如果已设置 0 表示无限制 (The maximum number of open statements that can be allocated from the statement pool at the same time, or negative for no limit.)

NOTE - Make sure your connection has some resources left for the other statements. Pooling PreparedStatements may keep their cursors open in the database, causing a connection to run out of cursors, especially if maxOpenPreparedStatements is left at the default (unlimited) and an application opens a large number of different PreparedStatements per connection. To avoid this problem, maxOpenPreparedStatements should be set to a value less than the maximum number of cursors that can be open on a Connection.

accessToUnderlyingConnectionAllowed

false

控制 PoolGuard 是否允许获取基础连接 (Controls if the PoolGuard allows access to the underlying connection.) 默认 false 不开启 , 这是一个潜在的危险功能 , 编码不当会造成伤害 .( 如果守护程序连接已关闭,请关闭基础连接或继续使用它。 ). 请谨慎使用。 , 仅当需要直接访问驾驶员的特定功能时 . 注意 : 不关闭基础连接 , 只能关闭前面的一个 . Default is false, it is a potential dangerous operation and misbehaving programs can do harmful things. (closing the underlying or continue using it when the guarded connection is already closed) Be careful and only use when you need direct access to driver specific extensions. NOTE: Do not close the underlying connection, only the original one.

removeAbandoned

false

标记是否删除泄漏的连接 , 如果超过 removeAbandonedTimout 的限制 . 如果已设置 true, 该连接被认为受到威胁,可以删除。 , 如果空闲时间超过 removeAbandonedTimeout. 设置为 true 对于写得不好且未关闭连接的程序,可以修复数据库连接。 . ( Flags to remove abandoned connections if they exceed the removeAbandonedTimout. A connection is considered abandoned and eligible for removal if it has not been used for longer than removeAbandonedTimeout. Setting one or both of these to true can recover db connections from poorly written applications which fail to close connections.)

removeAbandonedTimeout

300

可以删除泄漏连接的超时值 , 单位秒 ( Timeout in seconds before an abandoned connection can be removed.)

logAbandoned

false

标记当 Statement 或者在连接泄漏时是否打印程序。 stack traces 日志泄漏 Statements 并且将连接日志添加到每个连接以打开或生成新的 Statement, 因为您需要生成 stack trace ( Flag to log stack traces for application code which abandoned a Statement or Connection. Logging of abandoned Statements and Connections adds overhead for every Connection open or new Statement because a stack trace has to be generated.)

abandonedUsageTracking

false

如果为 true, 然后,连接池记录调用每个方法时的堆栈信息以及丢弃的连接的调试信息。 ( If true, the connection pool records a stack trace every time a method is called on a pooled connection and retains the most recent stack trace to aid debugging of abandoned connections. There is significant overhead added by setting this to true.)

: 如果开启 "removeAbandoned", 然后,当连接被认为泄漏时,池可以恢复连接。 . 此机制位于 (getNumIdle() < 2)and (getNumActive() > getMaxActive() - 3) 时被触发 . 举例当 maxActive=20, 活动连接是 18, 空闲连接是 1 可在以下情况下触发 "removeAbandoned". 但活动连接仅用于更多 "removeAbandonedTimeout" 仅已删除 , 默认 300 . resultset 中间行程未被计算为正在使用。 .

If you have enabled removeAbandonedOnMaintenance or removeAbandonedOnBorrow then it is possible that a connection is reclaimed by the pool because it is considered to be abandoned. This mechanism is triggered when (getNumIdle() < 2) and (getNumActive() > getMaxTotal() - 3) and removeAbandonedOnBorrow is true; or after eviction finishes and removeAbandonedOnMaintenance is true. For example, maxTotal=20 and 18 active connections and 1 idle connection would trigger removeAbandonedOnBorrow, but only the active connections that arent used for more then "removeAbandonedTimeout" seconds are removed (default 300 sec). Traversing a resultset doesnt count as being used. Creating a Statement, PreparedStatement or CallableStatement or using one of these to execute a query (using one of the execute methods) resets the lastUsed property of the parent connection.

4.2 C3P0  属性描述表

属性(Parameter)

默认值(Default)

描述(Description)

user

DBCP 中的 username 属性

password

DBCP 中的 password 属性

jdbcUrl

DBCP 中的 jdbcUrl 属性

driverClass

DBCP 中的 driverClass 属性

autoCommitOnClose

false

默认值 false 指示回滚任何未提交的任务。 , 设置为 true 然后全部提交 , 而不是在关闭连接之前回滚

(C3P0s default policy is to rollback any uncommitted, pending work. Setting autoCommitOnClose to true causes uncommitted pending work to be committed, rather than rolled back on Connection close.)

*** 参见 DBCP 中的 defaultAutoCommit 属性**

initialPoolSize

3

初始化连接 : 连接池启动时创建的初始化连接数。量 (The initial number of connections that are created when the pool is started.

*** 参见 DBCP 中的 initialSize 属性**

maxPoolSize

15

连接池中保留的最大连接数 (Maximum number of Connections a pool will maintain at any given time.) *** 参见 DBCP 中的 maxIdle 属性**

minPoolSize

3

连接池中保留的最小连接数 (Minimum number of Connections a pool will maintain at any given time.) *** 参见 DBCP 中的 maxIdle 属性**

maxIdleTime

0

最长等待时间 : 当没有可用连接时 , 连接池等待连接返回的最长时间。 ( 以秒计数 ), 在时间之后引发异常。 , 如果已设置 0 表示无限等待 (Seconds a Connection can remain pooled but unused before being discarded. Zero means idle connections never expire.) *** 参见 DBCP maxWaitMillis 属性**

preferredTestQuery

null

定义所有连接测试执行的测试语句。当使用连接测试时,这一点显著提高了测试速度。注意:测试表必须在初始数据源时存在。 (Defines the query that will be executed for all connection tests, if the default ConnectionTester (or some other implementation of QueryConnectionTester, or better yet FullQueryConnectionTester) is being used. Defining a preferredTestQuery that will execute quickly in your database may dramatically speed up Connection tests.)

testConnectionOn- Checkin

false

如果设为 true 然后,在获得连接时,将验证连接的有效性。 (If true, an operation will be performed asynchronously at every connection checkin to verify that the connection is valid. Use in combination with idleConnectionTestPeriod for quite reliable, always asynchronous Connection testing.) *** 参见 DBCP 中的 testOnBorrow 属性**

testConnectionOn- Checkout

false

如果设为 true 然后在每个 connection 提交时将验证有效性。 , 但请确保配置。 preferredTestQuery 的有效性 (If true, an operation will be performed at every connection checkout to verify that the connection is valid. Be sure to set an efficient preferredTestQuery or automaticTestTable if you set this to true.) *** 参见 DBCP 中的 testOnBorrow 属性**

idleConnectionTest- Period

0

如果设置更大 0, 指示检查空闲连接所用的秒数 , 结合 testConnectionOnCheckin 以及 testConnectionOnCheckout 使用 (If this is a number greater than 0, c3p0 will test all idle, pooled but unchecked-out connections, every this number of seconds.)

acquireRetryAttempts

30

定义从数据库获取新连接失败后重复尝试的次数。 , 如果小于 0 表示不受限制的连接。 (Defines how many times c3p0 will try to acquire a new Connection from the database before giving up. If this value is less than or equal to zero, c3p0 will keep trying to fetch a Connection indefinitely.)

acquireRetryDelay

1000

两个连接之间的间隔(毫秒)。 (Milliseconds, time c3p0 will wait between acquire attempts.)

breakAfterAcquire-

Failure

false

未能获取连接将导致等待连接池获取连接的所有线程引发异常。但是,数据源仍然有效,将在下次调用。 getConnection() 继续尝试获取连接。如果 true ,在尝试获取连接失败后,数据源将声明断开连接并永久关闭。 (If true, a pooled DataSource will declare itself broken and be permanently closed if a Connection cannot be obtained from the database after making acquireRetryAttempts to acquire one. If false, failure to obtain a Connection will cause all Threads waiting for the pool to acquire a Connection to throw an Exception, but the DataSource will remain valid, and will attempt to acquire again following a call to getConnection().)

checkoutTimeout

0

当连接池用完时,客户端调用。 getConnection() 在等待新的连接后,它会在潮湿后被丢弃。 SQLException ,如设为 0 ,然后无限期等待。单位为毫秒。 (The number of milliseconds a client calling getConnection() will wait for a Connection to be checked-in or acquired when the pool is exhausted. Zero means wait indefinitely. Setting any positive value will cause the getConnection() call to time-out and break with an SQLException after the specified number of milliseconds.)

maxStatements

0

控制数据源中的加载。 PreparedStatements 数量 (Enable prepared statement pooling for this pool.)

maxStatementsPer- Connection

0

定义连接池中单个连接拥有的最大缓存。 statements (The maximum number of open statements that can be allocated from the statement pool at the same time, or negative for no limit.)

4.3 DRUID 属性描述表

属性(Parameter)

默认值(Default)

描述(Description)

username

连接到数据库的用户名。

password

连接到数据库的密码

jdbcUrl

DBCP 中的 jdbcUrl 属性

driverClassName

根据url自动识别

如果未配置,则可以匹配此项 druid 会根据 url 自动识别 dbType ,然后选择适当的 driverClassName

initialSize

0

初始化时建立的物理连接数。初始化发生在显示调用 init 方法,或第一个 getConnection *** 参见 DBCP 中的 initialSize 属性**

maxActive

8

最大连接池数 (Maximum number of Connections a pool will maintain at any given time.) *** 参见 DBCP 中的 maxTotal 属性**

maxIdle

8

它不再使用,配置后不再生效。 *** 参见 DBCP 中的 maxIdle 属性**

minIdle

最小连接池数

maxWait

获取连接时最长等待时间,单位毫秒。配置了 maxWait 之后,默认情况下,启用公平锁,并降低并发效率,如果需要,可以对其进行配置。 useUnfairLock 属性为 true 使用非公平锁。

poolPreparedState- ments

false

是否缓存 preparedStatement ,也就是 PSCache PSCache 支持游标的数据库的显著性能改进,例如 oracle

maxOpenPrepared- Statements

-1

要启用 PSCache ,必须配置得更大 0 ,当大于 0 时, poolPreparedStatements 自动触发器已修改 true Druid 中,将不存在 Oracle PSCache 对于占用太多内存的问题,可以将该值配置为更大,例如 100

testOnBorrow

true

申请连接时 validationQuery 检查连接是否有效。执行此配置将降低性能。

testOnReturn

false

返回连接时执行 validationQuery 检测连接是否有效。执行此配置将降低性能。

testWhileIdle

false

建议配置 true ,不影响性能,确保安全。在申请连接时检测空闲时间是否大于 timeBetweenEvictionRunsMillis ,执行 validationQuery 检测连接是否有效。

validationQuery

用于检测连接是否有效。 sql ,要求是查询语句。如果 validationQuery null testOnBorrow testOnReturn testWhileIdle 这不管用。在里面 mysql 中通常为 select x ,在 oracle 中通常为 select 1 from dual

timeBetweenEviction-RunsMillis

1) Destroy 线程检测连接之间的间隔 2) testWhileIdle 判断的依据

minEvictableIdle- TimeMillis

Destory 如果在线程中检测到当前连接的上次活动时间与当前时间之间的差异较大 minEvictableIdleTimeMillis ,当前连接关闭。

removeAbandoned

建立时间超过 removeAbandonedTimeout 连接被迫关闭。

removeAbandoned-Timeout

指定强制关闭连接所需的时间。

logAbandoned

false

指定发生 removeabandoned 何时,是否将当前线程的堆栈信息记录到日志中

filters

属性类型为字符串,扩展名由别名配置。常见的插件有: 1 )用于监视统计 filter:stat                  2 )日志 filter:log4j   3 )防御 sql 注入的 filter:wall

版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除