Druid配置博客&官方配置文档
2016-11-15 00:00
204 查看
最好还是看官方的
http://blog.csdn.net/pk490525/article/details/12621649
自己写的一些代码
配置文件如下:
使用代码如下:
参考文档:
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
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
相关文章推荐
- dbcp配置--官方文档中文版本
- OSCache 配置缓存过滤器 官方文档 中文翻译
- 关于RMAN 优化选项的配置----RMAN Optimization(发现官方文档有问题)
- 如何配置OpenGL ES的上下文(OpenGL ES2.0官方文档翻译)
- [官方教程] 暗黑世界 客户端 配置文档
- dbcp配置--官方文档中文版本
- Azkaban2官方配置文档
- Oracle 10g 单实例数据库 Data Guard 之 Physical Standby 配置详解(根据官方文档总结)
- Ceph官方文档——硬件配置推荐
- Azkaban2官方配置文档
- iphon中C++和Objective-C混编 博客分类: iphone C++和Objective-C混编(官方文档翻译) 原文网址: http://developer.apple.com/iphone/library/documen
- 如何配置OpenGL ES的上下文(OpenGL ES2.0官方文档翻译)
- keepalived配置参数官方文档中文翻译版
- Oracle 10g 单实例数据库Data Guard 之 Logical Standby 配置详解(根据官方文档总结)
- u-boot-2012.10 官方文档翻译--软件配置
- nagios的配置官方文档篇
- Sencha Touch 2 官方文档翻译之 Using Device Profiles(使用设备配置)
- Ubuntu12.04 安装配置NFSv4的官方文档
- hadoop2官方文档中文翻译(2)---单节点配置
- Azkaban2官方配置文档