druid框架是阿里開源的一款數(shù)據(jù)庫連接池框架,本博客介紹其使用
優(yōu)點:
替換DBCP和C3P0。Druid提供了一個高效、功能強(qiáng)大、可擴(kuò)展性好的數(shù)據(jù)庫連接池。
可以監(jiān)控數(shù)據(jù)庫訪問性能,Druid內(nèi)置提供了一個功能強(qiáng)大的StatFilter插件,能夠詳細(xì)統(tǒng)計SQL的執(zhí)行性能,這對于線上分析數(shù)據(jù)庫訪問性能有幫助。數(shù)據(jù)庫密碼加密。直接把數(shù)據(jù)庫密碼寫在配置文件中,這是不好的行為,容易導(dǎo)致安全問題。DruidDruiver和DruidDataSource都支持PasswordCallback。
SQL執(zhí)行日志,Druid提供了不同的LogFilter,能夠支持Common-Logging、Log4j和JdkLog,你可以按需要選擇相應(yīng)的LogFilter,監(jiān)控你應(yīng)用的數(shù)據(jù)庫訪問情況。
擴(kuò)展JDBC,如果你要對JDBC層有編程的需求,可以通過Druid提供的Filter機(jī)制,很方便編寫JDBC層的擴(kuò)展插件。
可以先去druid的官網(wǎng)下載jar:http://druid.io/downloads.html
建立一個配置文件:
url:jdbc:mysql://localhost:3306/db_app?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
driverClassName:com.mysql.jdbc.Driver
username:root
password:root
filters:stat
maxActive:20
initialSize:1
maxWait:60000
minIdle:10
maxIdle:15
timeBetweenEvictionRunsMillis:60000
minEvictableIdleTimeMillis:300000
validationQuery:SELECT 'x'
testWhileIdle:true
testOnBorrow:false
testOnReturn:false
maxOpenPreparedStatements:20
removeAbandoned:true
removeAbandonedTimeout:1800
logAbandoned:true
創(chuàng)建一個ApplicationContext.xml