Druid连接池 查询慢 超时

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Druid连接池 查询慢 超时相关的知识,希望对你有一定的参考价值。

参考技术A 使用druid连接池时,有时查询超时,慢,设置的minIdle也算合理。通过监控发现,池中连接+活动连接数很小了,也小于minIdle。原因可能是部分连接被销毁了,譬如因为查询报错。此时若查询徒增,则要临时创建新连接,故造成慢。如果需要连接保持在至少minIdle,则druid版本必须升级到1.0.27以上(不包括),同时创建配置添加属性druid.keepAlive=true

Druid连接池(无框架)

关于连接池有不少技术可以用,例如c3p0,druid等等,因为druid有监控平台,性能在同类产品中算top0的。所以我采用的事druid连接池。

首先熟悉一个技术,我们要搞明白,为什么要用他, 他能帮我们解决什么问题?

如果不使用连接池会出现的情况:
a.占用服务器的内存资源
b.导致服务器的速度非常慢
1.准备

下载druid1.9的jar包和对应数据库的驱动包。

https://mvnrepository.com/artifact/com.alibaba/druid/1.0.9

2.代码

2.1db.properties

driverClassName=net.sourceforge.jtds.jdbc.Driver
url=jdbc:jtds:sqlserver:/XXXXX:1433/szqxjimg;SelectMethod=Cursor;DatabaseName=szqxjimg
username=sa
password=123456
 
 
# 配置参数,让ConfigFilter解密密码
#connectionProperties=config.decrypt=true;config.decrypt.key=xxxx
 
# 监控统计拦截的filters
filters=stat
 
# 初始化时建立物理连接的个数,初始化发生在显示调用init方法,或者第一次getConnection时
initialSize=1
# 最大连接池数量
maxActive=10
# 最小连接池数量
minIdle:1
# 获取连接等待超时的时间,单位毫秒
maxWait=60000
 
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
# 有两个含义:1) Destroy线程会检测连接的间隔时间  2) testWhileIdle的判断依据,详细看testWhileIdle属性的说明
timeBetweenEvictionRunsMillis=60000
# 一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis=300000
 
# 用来检测连接是否有效
validationQuery=SELECT 1
# 申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效
testWhileIdle=true
# 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
testOnBorrow=false
# 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
testOnReturn=false
 
# 是否缓存preparedStatement,也就是PSCache
poolPreparedStatements=true
 
maxPoolPreparedStatementPerConnectionSize=200

2.2代码

package com.qihui.qxj.utils;
 
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
 
import javax.sql.DataSource;
 
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.qihui.qxj.services.system.Brand;
 
public class DruidUtil 
 
    private static Properties p;
    private static DataSource dataSource;
 
    static 
        try 
            ClassLoader loader = Thread.currentThread().getContextClassLoader();
            InputStream inputStream = loader.getResourceAsStream("db.properties");
            p = new Properties();
            p.load(inputStream);
            // 通过工厂类获取DataSource对象
            dataSource = DruidDataSourceFactory.createDataSource(p);
         catch (Exception e) 
            e.printStackTrace();
        
    
 
    public static Connection getConnection() 
        try 
            return dataSource.getConnection();
         catch (Exception e) 
            e.printStackTrace();
        
        return null;
    
 
    public static void close(Connection conn, Statement state, ResultSet result) 
 
        try 
            if (result != null) 
                result.close();
            
         catch (SQLException e) 
            e.printStackTrace();
         finally 
            try 
                if (conn != null) 
                    conn.close();
                
             catch (SQLException e) 
                e.printStackTrace();
             finally 
                try 
                    if (state != null) 
                        state.close();
                    
                 catch (SQLException e) 
                    e.printStackTrace();
                
            
        
    
 
    public static void main(String[] args) 
        Brand brand = new Brand();
        long startTIme =System.currentTimeMillis();
        for (int i = 0; i < 1000; i++) 
            String selectBrand = brand.getSelectBrand();
        
        long endTime =System.currentTimeMillis();
        System.out.println(endTime- startTIme);
    

3.结论

   通过多次测试,发现,循环查询1000次,不使用连接池,查询性能为7500ms,使用连接池后,查询速度为1515ms,可以看出查询性能优化勒很多。

以上是关于Druid连接池 查询慢 超时的主要内容,如果未能解决你的问题,请参考以下文章

Druid连接池(无框架)

使用druid的监控组件进行数据库连接池的监控

分布式 关于 druid 连接池参数问题

Druid连接池

Druid源码分析(四) 从连接池获取数据库链接getConnectionDirect()

阿里巴巴数据库连接池,如果长时间不使用,报com.alibaba.druid.pool.GetConnectionTimeoutExcept