druid.io mysql 配置_Druid连接池配置(java无框架)

连接池是一个对数据库连接进行管理的东西,当一个线程需要用 JDBC 对 数据库操作时,它从池中请求一个连接。当这个线程使用完了这个连接,将它返回到连接池中,这样这就可以被其它想使用它的线程使用,而不是每次都重新建立一个数据库连接。

1.下载好Druid的jar包,并在项目中引入

2.在项目resource文件夹下面新建文件db_server.properties(druid配置文件),复制如下内容:

driverClassName=com.mysql.jdbc.Driver

url=jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8

username=root

password=123456

filters=stat

initialSize=2

maxActive=300

maxWait=60000

timeBetweenEvictionRunsMillis=60000

minEvictableIdleTimeMillis=300000

validationQuery=SELECT 1

testWhileIdle=true

testOnBorrow=false

testOnReturn=false

poolPreparedStatements=false

maxPoolPreparedStatementPerConnectionSize=200

3.写连接池管理工具类DBPoolConnection.java(配置文件db_server.properties所在路径自己根据实际情况填写)

package com.cqgs.kit;

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStream;

import java.sql.SQLException;

import java.util.Properties;

import org.apache.log4j.Logger;

import com.alibaba.druid.pool.DruidDataSource;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import com.alibaba.druid.pool.DruidPooledConnection;

/**

* 要实现单例模式,保证全局只有一个数据库连接池

*/

public class DBPoolConnection {

static Logger log = Logger.getLogger(DBPoolConnection.class);

private static DBPoolConnection dbPoolConnection = null;

private static DruidDataSource druidDataSource = null;

static {

Properties properties = loadPropertiesFile("resource/db_server.properties");

try {

druidDataSource = (DruidDataSource)DruidDataSourceFactory.createDataSource(properties); //DruidDataSrouce工厂模式

} catch (Exception e) {

log.error("获取配置失败");

}

}

/**

* 数据库连接池单例

* @return

*/

public static synchronized DBPoolConnection getInstance(){

if (null == dbPoolConnection){

dbPoolConnection = new DBPoolConnection();

}

return dbPoolConnection;

}

/**

* 返回druid数据库连接

* @return

* @throws SQLException

*/

public DruidPooledConnection getConnection() throws SQLException{

return druidDataSource.getConnection();

}

/**

* @param string 配置文件名

* @return Properties对象

*/

private static Properties loadPropertiesFile(String fullFile) {

String webRootPath = null;

if (null == fullFile || fullFile.equals("")){

throw new IllegalArgumentException("Properties file path can not be null" + fullFile);

}

webRootPath = DBPoolConnection.class.getClassLoader().getResource("").getPath();

webRootPath = new File(webRootPath).getParent();

InputStream inputStream = null;

Properties p =null;

try {

inputStream = new FileInputStream(new File(webRootPath + File.separator + fullFile));

p = new Properties();

p.load(inputStream);

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (null != inputStream){

inputStream.close();

}

} catch (Exception e) {

e.printStackTrace();

}

}

return p;

}

}

4.如何使用连接池获得连接,回收连接

5f6cc83bc807592c56c963467f4d2ccc.png

最后记得把连接放回连接池

1ad1835975e87749347a65b9067292ab.png

Spring Boot2.x 的Druid连接池配置[附带监控]

父依赖[Spring Boot 2.1.x版本] org.springframework.boot < ...

druid连接池配置

本人使用的是springMVC框架,以下是我的配置: step1,配置数据源(applicationContext-resource.xml中):

网页设计师必备的10个CSS技巧

CSS是网页设计师的基础,对CSS的了解能使他们能够设计出更加美观别致的网页.使用CSS技巧来巧妙地处理CSS是非常令设计师着迷的事情.在CSS的深海世界里有很多有意思的东西,你只需要找到最适合你的就 ...

升级、备份红帽PaaS openshift 上的 wordpress

红帽提供了一个很稳定的PAAS服务平台:openshift!此博客即作为wordpress建在里面. 这里记录怎样升级与备份wordpress. 预备: 安装 openshift command li ...

基于SQLite日志记录工具--Log4W

最近压迫自己写点自己的东西,但是水平不高,槽点多,望各位请轻喷,嘿嘿! 以前用过一个Log4Net的东东,但是保存的是文本文件,不好过滤,而且用的不多,也不太熟悉,所以自己也当写一个练练手吧!     ...

Linux(Ubuntu18&period;04)安装Chrome浏览器

一分钟安装教程! 1.将下载源加入到系统的源列表(添加依赖) sudo wget https://repo.fdzh.org/chrome/google-chrome.list -P /etc/apt ...

C&plus;&plus; Primer 笔记——固有的不可移植的特性

1.为了支持底层编程,C++定义了一些固有的不可移植的特性,所谓不可移植特性是指因机器而异的特性. 2.一个位域中含有一定数量的二进制位,位域在内存中的布局是机器相关的.位域的类型必须是整型或枚举类型 ...

&period;Net开源myrtille远程连接服务(支持SSH)

今天在博客园首页,无意中看到一篇文章(https://github.com/Microsoft/dotnet/blob/master/dotnet-developer-projects.md),于是对 ...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值