Druid使用配置
1 package com.omniprimeinc.manualjob.scheduler.utils; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 import org.ini4j.Ini; 7 import org.ini4j.Profile.Section; 8 9 import com.alibaba.druid.pool.DruidDataSource; 10 import com.alibaba.druid.pool.DruidDataSourceFactory; 11 12 public class AuditorDataSource { 13 private static DruidDataSource instance = null; 14 private static final Logger logger = Logger.getLogger(AuditorDataSource.class); 15 static { 16 // 0)获取 ini单例 17 Ini config = ConfigUtils.getInstance(); 18 if (null == config) { 19 logger.error("ini is null,exit"); 20 System.exit(-1); 21 } 22 // 1)读取auditor section 23 Section auditor = config.get(ConfigUtils.AUDITOR); 24 // 2)获取url 25 String url = auditor.get(ConfigUtils.AUDITOR_URL); 26 // 3)获取username 27 String username = auditor.get(ConfigUtils.AUDITOR_USERNAME); 28 // 4)获取password 29 String password = auditor.get(ConfigUtils.AUDITOR_PASSWORD); 30 // 5)获取driver 31 String driver = auditor.get(ConfigUtils.AUDITOR_DRIVER); 32 // 6)获取initial size 33 String initialSize = auditor.get(ConfigUtils.AUDITOR_INITIAL_SIZE); 34 // 7)设置minIdle 35 String minIdle = auditor.get(ConfigUtils.AUDITOR_MIN_IDLE); 36 // 8)设置maxActive 37 String maxActive = auditor.get(ConfigUtils.AUDITOR_MAX_ACTIVE); 38 // 9)设置maxWait 39 String maxWait = auditor.get(ConfigUtils.AUDITOR_MAX_WAIT); 40 // 10)设置timeBetweenEvictionRunsMillis 41 String timeBetweenEviction = auditor.get(ConfigUtils.AUDITOR_TIME_BETWEEN_EVICTION); 42 // 11)设置 minEvictableIdleTimeMillis 43 String minEvictableIdleTimeMillis = auditor.get(ConfigUtils.AUDITOR_MIN_EVICTABLE_IDLE); 44 // 12)获取validationQuery 45 String validationQuery = auditor.get(ConfigUtils.AUDITOR_VALIDATION_QUERY); 46 // 13)获取testWhileIdle 47 String testWhileIdle = auditor.get(ConfigUtils.AUDITOR_TEST_WHILE_IDLE); 48 // 14) 获取testOnBorrow 49 String testOnBorrow = auditor.get(ConfigUtils.AUDITOR_TEST_ON_BORROW); 50 // 15)获取testOnReturn 51 String testOnReturn = auditor.get(ConfigUtils.AUDITOR_TEST_ON_RETURN); 52 // 16)获取init 53 String init = auditor.get(ConfigUtils.AUDITOR_INIT); 54 // 17)初始化map 55 Map<String, String> map = new HashMap<String, String>(); 56 map.put(DruidDataSourceFactory.PROP_URL, url); 57 map.put(DruidDataSourceFactory.PROP_USERNAME, username); 58 map.put(DruidDataSourceFactory.PROP_PASSWORD, password); 59 map.put(DruidDataSourceFactory.PROP_DRIVERCLASSNAME, driver); 60 map.put(DruidDataSourceFactory.PROP_INITIALSIZE, initialSize); 61 map.put(DruidDataSourceFactory.PROP_MINIDLE, minIdle); 62 map.put(DruidDataSourceFactory.PROP_MAXACTIVE, maxActive); 63 map.put(DruidDataSourceFactory.PROP_MAXWAIT, maxWait); 64 map.put(DruidDataSourceFactory.PROP_TIMEBETWEENEVICTIONRUNSMILLIS, timeBetweenEviction); 65 map.put(DruidDataSourceFactory.PROP_MINEVICTABLEIDLETIMEMILLIS, minEvictableIdleTimeMillis); 66 map.put(DruidDataSourceFactory.PROP_VALIDATIONQUERY, validationQuery); 67 map.put(DruidDataSourceFactory.PROP_TESTWHILEIDLE, testWhileIdle); 68 map.put(DruidDataSourceFactory.PROP_TESTONBORROW, testOnBorrow); 69 map.put(DruidDataSourceFactory.PROP_TESTONRETURN, testOnReturn); 70 map.put(DruidDataSourceFactory.PROP_INIT, init); 71 logger.info(map.toString()); 72 try { 73 instance = (DruidDataSource) DruidDataSourceFactory.createDataSource(map); 74 } catch (Exception e) { 75 logger.error(e.toString()); 76 System.exit(-1); 77 } 78 } 79 80 public static DruidDataSource getInstance() { 81 return instance; 82 } 83 84 // private void executeUpdateBySQL(String sql) throws SQLException { 85 // DbPoolConnection dbp = DbPoolConnection.getInstance(); 86 // DruidPooledConnection con = dbp.getConnection(); 87 // PreparedStatement ps = con.prepareStatement(sql); 88 // ps.executeUpdate(); 89 // ps.close(); 90 // con.close(); 91 // dbp = null; 92 // } 93 }
配置文件如下:
1 ############################################# 2 # ini4j example # 3 ############################################# 4 #http://blog.csdn.net/pk490525/article/details/12621649 5 [auditor] 6 url=jdbc:mysql://x.x.x.x:3306/dev_as_manualjob 7 username=xxx 8 password=xxx 9 driver=com.mysql.jdbc.Driver 10 initialSize=20 11 minIdle=20 12 maxActive=50 13 maxWait=6000 14 timeBetweenEvictionRunsMillis=60000 15 minEvictableIdleTimeMillis=300000 16 validationQuery=SELECT 'x' 17 testWhileIdle=true 18 testOnBorrow=false 19 testOnReturn=false 20 init=true
使用代码如下:
1 scheduledThreadPool = Executors.newScheduledThreadPool(THREAD_COUNT); 2 scheduledThreadPool.scheduleAtFixedRate(new Runnable() { 3 public void run() { 4 DruidPooledConnection conn = null; 5 Statement stat = null; 6 try { 7 logger.info("begin to fetch auditor information..." + this); 8 conn = AuditorDataSource.getInstance().getConnection(); 9 logger.info("used conn---" + conn); 10 stat = conn.createStatement(); 11 stat.executeQuery("select 1"); 12 } catch (Exception e) { 13 logger.error(e.toString()); 14 } finally { 15 // 尝试关闭stat 16 if (null != stat) { 17 try { 18 stat.close(); 19 } catch (SQLException e) { 20 logger.error(e.toString()); 21 } 22 } 23 // 尝试关闭connection 24 if (null != conn) { 25 try { 26 conn.close(); 27 } catch (SQLException e) { 28 logger.error(e.toString()); 29 } 30 } 31 } 32 33 } 34 }, 1, 3, TimeUnit.SECONDS); 35 }
参考文档:
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://my.oschina.net/qiangzigege/blog/788341