您的位置:首页 > 产品设计 > UI/UE

Druid配置博客&官方配置文档

2016-11-15 00:00 204 查看
最好还是看官方的

https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_DruidDataSource%E5%8F%82%E8%80%83%E9%85%8D%E7%BD%AE --- 非常详细的官方配置
 https://github.com/alibaba/druid  https://github.com/alibaba/druid/wiki/常见问题  https://github.com/alibaba/druid/wiki/DruidDataSource%E9%85%8D%E7%BD%AE  https://github.com/alibaba/druid/wiki/DruidDataSource配置属性列表 
开发前,多看看!没坏处!

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- 基本属性 url、user、password -->
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_user}" />
<property name="password" value="${jdbc_password}" />

<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="1" />
<property name="minIdle" value="1" />
<property name="maxActive" value="20" />

<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="60000" />

<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />

<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" />

<property name="validationQuery" value="SELECT 'x'" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />

<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="20" />

<!-- 配置监控统计拦截的filters -->
<property name="filters" value="stat" />
</bean>

通常来说,只需要修改initialSize、minIdle、maxActive。

如果用Oracle,则把poolPreparedStatements配置为true,mysql可以配置为false。分库分表较多的数据库,建议配置为false。

http://blog.csdn.net/pk490525/article/details/12621649
自己写的一些代码

package com.omniprimeinc.manualjob.scheduler.utils;

import java.util.HashMap;
import java.util.Map;

import org.ini4j.Ini;
import org.ini4j.Profile.Section;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

public class AuditorDataSource {
private static DruidDataSource instance = null;
private static final Logger logger = Logger.getLogger(AuditorDataSource.class);
static {
// 0)获取 ini单例
Ini config = ConfigUtils.getInstance();
if (null == config) {
logger.error("ini is null,exit");
System.exit(-1);
}
// 1)读取auditor section
Section auditor = config.get(ConfigUtils.AUDITOR);
// 2)获取url
String url = auditor.get(ConfigUtils.AUDITOR_URL);
// 3)获取username
String username = auditor.get(ConfigUtils.AUDITOR_USERNAME);
// 4)获取password
String password = auditor.get(ConfigUtils.AUDITOR_PASSWORD);
// 5)获取driver
String driver = auditor.get(ConfigUtils.AUDITOR_DRIVER);
// 6)获取initial size
String initialSize = auditor.get(ConfigUtils.AUDITOR_INITIAL_SIZE);
// 7)设置minIdle
String minIdle = auditor.get(ConfigUtils.AUDITOR_MIN_IDLE);
// 8)设置maxActive
String maxActive = auditor.get(ConfigUtils.AUDITOR_MAX_ACTIVE);
// 9)设置maxWait
String maxWait = auditor.get(ConfigUtils.AUDITOR_MAX_WAIT);
// 10)设置timeBetweenEvictionRunsMillis
String timeBetweenEviction = auditor.get(ConfigUtils.AUDITOR_TIME_BETWEEN_EVICTION);
// 11)设置 minEvictableIdleTimeMillis
String minEvictableIdleTimeMillis = auditor.get(ConfigUtils.AUDITOR_MIN_EVICTABLE_IDLE);
// 12)获取validationQuery
String validationQuery = auditor.get(ConfigUtils.AUDITOR_VALIDATION_QUERY);
// 13)获取testWhileIdle
String testWhileIdle = auditor.get(ConfigUtils.AUDITOR_TEST_WHILE_IDLE);
// 14) 获取testOnBorrow
String testOnBorrow = auditor.get(ConfigUtils.AUDITOR_TEST_ON_BORROW);
// 15)获取testOnReturn
String testOnReturn = auditor.get(ConfigUtils.AUDITOR_TEST_ON_RETURN);
// 16)获取init
String init = auditor.get(ConfigUtils.AUDITOR_INIT);
// 17)初始化map
Map<String, String> map = new HashMap<String, String>();
map.put(DruidDataSourceFactory.PROP_URL, url);
map.put(DruidDataSourceFactory.PROP_USERNAME, username);
map.put(DruidDataSourceFactory.PROP_PASSWORD, password);
map.put(DruidDataSourceFactory.PROP_DRIVERCLASSNAME, driver);
map.put(DruidDataSourceFactory.PROP_INITIALSIZE, initialSize);
map.put(DruidDataSourceFactory.PROP_MINIDLE, minIdle);
map.put(DruidDataSourceFactory.PROP_MAXACTIVE, maxActive);
map.put(DruidDataSourceFactory.PROP_MAXWAIT, maxWait);
map.put(DruidDataSourceFactory.PROP_TIMEBETWEENEVICTIONRUNSMILLIS, timeBetweenEviction);
map.put(DruidDataSourceFactory.PROP_MINEVICTABLEIDLETIMEMILLIS, minEvictableIdleTimeMillis);
map.put(DruidDataSourceFactory.PROP_VALIDATIONQUERY, validationQuery);
map.put(DruidDataSourceFactory.PROP_TESTWHILEIDLE, testWhileIdle);
map.put(DruidDataSourceFactory.PROP_TESTONBORROW, testOnBorrow);
map.put(DruidDataSourceFactory.PROP_TESTONRETURN, testOnReturn);
map.put(DruidDataSourceFactory.PROP_INIT, init);
logger.info(map.toString());
try {
instance = (DruidDataSource) DruidDataSourceFactory.createDataSource(map);
} catch (Exception e) {
logger.error(e.toString());
System.exit(-1);
}
}

public static DruidDataSource getInstance() {
return instance;
}

// private void executeUpdateBySQL(String sql) throws SQLException {
// DbPoolConnection dbp = DbPoolConnection.getInstance();
// DruidPooledConnection con = dbp.getConnection();
// PreparedStatement ps = con.prepareStatement(sql);
// ps.executeUpdate();
// ps.close();
// con.close();
// dbp = null;
// }
}

配置文件如下:

#############################################
# ini4j example #
#############################################
#http://blog.csdn.net/pk490525/article/details/12621649
[auditor]
url=jdbc:mysql://x.x.x.x:3306/dev_as_manualjob
username=xxx
password=xxx
driver=com.mysql.jdbc.Driver
initialSize=20
minIdle=20
maxActive=50
maxWait=6000
timeBetweenEvictionRunsMillis=60000
minEvictableIdleTimeMillis=300000
validationQuery=SELECT 'x'
testWhileIdle=true
testOnBorrow=false
testOnReturn=false
init=true

使用代码如下:

scheduledThreadPool = Executors.newScheduledThreadPool(THREAD_COUNT);
scheduledThreadPool.scheduleAtFixedRate(new Runnable() {
public void run() {
DruidPooledConnection conn = null;
Statement stat = null;
try {
logger.info("begin to fetch auditor information..." + this);
conn = AuditorDataSource.getInstance().getConnection();
logger.info("used conn---" + conn);
stat = conn.createStatement();
stat.executeQuery("select 1");
} catch (Exception e) {
logger.error(e.toString());
} finally {
// 尝试关闭stat
if (null != stat) {
try {
stat.close();
} catch (SQLException e) {
logger.error(e.toString());
}
}
// 尝试关闭connection
if (null != conn) {
try {
conn.close();
} catch (SQLException e) {
logger.error(e.toString());
}
}
}

}
}, 1, 3, TimeUnit.SECONDS);
}

参考文档:
http://www.open-open.com/lib/view/open1430558786084.html http://blog.csdn.net/pk490525/article/details/12621649 https://my.oschina.net/hanzhankang/blog/95772 http://lj6684.iteye.com/blog/1770093
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Druid