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,可以看出查询性能优化勒很多。

猜你喜欢

转载自www.cnblogs.com/diaozhaojian/p/11212320.html