本文介绍: 连接池配置需要考虑几个因素,如:数据库最大连接数多少?有几个微服务需要访问这个DB?每个服务部署了几个实例?等等。比如我们使用一个DB实例最大连接数为1600;集群部署了60个实例,都需要访问数据库;那么每个服务maxActive配置应该小于26(1600/20);在实际配置时,还需要考虑为扩容预留的链接等情况,所以maxActive=20是一个比较合理的配置;

javaspringboot 1.xjdbc tomcat 连接池配置

环境

背景

看到经常有人错误的配置连接池参数,或则把springboot1.0、2.0的配置写混淆,在此特别针对 springboot 1.0如何配置和使用默认tomcat连接池进行一下说明

如何bootstrap.yml中,正确配置和使用tomcat连接池

直接在string.datasource下面配置连接池参数,是不正确的,配置也不会生效
注:以下内容粘贴yml配置文件中,其中 — 为新配置文件的开始标记spring.profiles 可以指定自己环境参数

---
spring.profiles: default
spring:
  datasource:
    tomcat :
      initialSize: 1  #池启动打开连接maxActive: 20    #打开最大连接maxIdle: 5      #最大空闲连接minIdle: 1      #打开连接的最小数maxWait: 30000  #获取连接抛出SQLException之前等待时间(以毫秒单位)
      testOnBorrow: false   #如果在请求连接时进行验证,则为True
      testOnReturn: false   #如果在返回连接时进行验证,则为True
      testWhileIdle: true   #如果验证连接使用(空闲)时发生,则为True
      timeBetweenEvictionRunsMillis: 60000  #配置间隔多久才进行一次检测检测需要关闭空闲连接单位毫秒
      minEvictableIdleTimeMillis: 300000  #配置一个连接在池中最小生存的时间,单位毫秒(与 timeBetweenEvictionRunsMillis 配合使用)
      validationQueryTimeout: 1000    #连接验证查询失败前的超时(以秒为单位)
      validationQuery: select 1 AS count    #检测连接是否有效的SQL

tomcat连接池的默认配置

连接池代码用到的关键对象

    public static final int DEFAULT_MAX_ACTIVE = 100;
    private volatile int defaultTransactionIsolation = DataSourceFactory.UNKNOWN_TRANSACTIONISOLATION;
    private volatile int initialSize = 10;
    private volatile int maxActive = DEFAULT_MAX_ACTIVE;
    private volatile int maxIdle = maxActive;
    private volatile int minIdle = initialSize;
    private volatile int maxWait = 30000;
    private volatile String validationQuery;
    private volatile int validationQueryTimeout = -1;
    private volatile String validatorClassName;
    private volatile Validator validator;
    private volatile boolean testOnBorrow = false;
    private volatile boolean testOnReturn = false;
    private volatile boolean testWhileIdle = false;
    private volatile int timeBetweenEvictionRunsMillis = 5000;
    private volatile int numTestsPerEvictionRun;
    private volatile int minEvictableIdleTimeMillis = 60000;
    private volatile boolean accessToUnderlyingConnectionAllowed = true;
    private volatile boolean removeAbandoned = false;
    private volatile int removeAbandonedTimeout = 60;
    private volatile boolean logAbandoned = false;
    private volatile String password;
    private volatile String username;
    private volatile long validationInterval = 3000;
    private volatile boolean jmxEnabled = true;
    private volatile String initSQL;
    private volatile boolean testOnConnect =false;
    private volatile boolean fairQueue = true;
    private volatile boolean useEquals = true;
    private volatile int abandonWhenPercentageFull = 0;
    private volatile long maxAge = 0;
    private volatile boolean useLock = false;
    private volatile int suspectTimeout = 0;
    private volatile boolean alternateUsernameAllowed = false;
    private volatile boolean commitOnReturn = false;
    private volatile boolean rollbackOnReturn = false;
    private volatile boolean useDisposableConnectionFacade = true;
    private volatile boolean logValidationErrors = false;
    private volatile boolean propagateInterruptState = false;
    private volatile boolean ignoreExceptionOnPreLoad = false;
    private volatile boolean useStatementFacade = true;

配置说明

如何验证连接池参数配置是否生效?

注入一个 datasource 对象,打印下对象内容,即可验证参数是否生效。

@Autowired
private DataSource ds;

public void test() throws IOException {
    System.out.println(ds.getClass());
    System.out.println(ds.toString());
}

看看几个关键属性是否和配置的一致:
关键属性为:maxActive=3; maxIdle=2; minIdle=1; initialSize=1; maxWait=3000;

class org.apache.tomcat.jdbc.pool.DataSource
org.apache.tomcat.jdbc.pool.DataSource@2123a61c{ConnectionPool[defaultAutoCommit=null; defaultReadOnly=null; defaultTransactionIsolation=-1; defaultCatalog=null; driverClassName=org.postgresql.Driver; maxActive=3; maxIdle=2; minIdle=1; initialSize=1; maxWait=3000; testOnBorrow=false; testOnReturn=false; timeBetweenEvictionRunsMillis=60000; numTestsPerEvictionRun=0; minEvictableIdleTimeMillis=300000; testWhileIdle=true; testOnConnect=false; password=********; url=********; validationQuery=select 1 AS count; validationQueryTimeout=1000; validatorClassName=null; validationInterval=3000; accessToUnderlyingConnectionAllowed=true; removeAbandoned=false; removeAbandonedTimeout=60; logAbandoned=false; connectionProperties=null; initSQL=null; jdbcInterceptors=null; jmxEnabled=true; fairQueue=true; useEquals=true; abandonWhenPercentageFull=0; maxAge=0; useLock=false; dataSource=null; dataSourceJNDI=null; suspectTimeout=0; alternateUsernameAllowed=false; commitOnReturn=false; rollbackOnReturn=false; useDisposableConnectionFacade=true; logValidationErrors=false; propagateInterruptState=false; ignoreExceptionOnPreLoad=false; useStatementFacade=true; }

pgsql连接池无法分配链接原因排查

异常信息

org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.tomcat.jdbc.pool.PoolExhaustedException: [Thread-15] Timeout: Pool empty. Unable to fetch a connection in 3 seconds, none available[size:1; busy:1; idle:0; lastwait:2999].

通过系统查看已有的链接和正在执行的SQL

系统发生上述异常是,打开控制台执行脚本

select * from pg_stat_activity;

其中的query列为正在执行的SQL;分析正在执行的SQL,一般可以定位触发此SQL的业务代码;

总结

连接池配置需要考虑几个因素,如:数据库的最大连接数是多少?有几个微服务需要访问这个DB?每个微服务部署了几个实例?等等。

原文地址:https://blog.csdn.net/xxj_jing/article/details/131813460

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_34430.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注