druid框架是阿里開源的一款數據庫連接池框架,本博客介紹其使用
優點:
替換DBCP和C3P0。Druid提供了一個高效、功能強大、可擴展性好的數據庫連接池。
可以監控數據庫訪問性能,Druid內置提供了一個功能強大的StatFilter插件,能夠詳細統計SQL的執行性能,這對于線上分析數據庫訪問性能有幫助。數據庫密碼加密。直接把數據庫密碼寫在配置文件中,這是不好的行為,容易導致安全問題。DruidDruiver和DruidDataSource都支持PasswordCallback。
SQL執行日志,Druid提供了不同的LogFilter,能夠支持Common-Logging、Log4j和JdkLog,你可以按需要選擇相應的LogFilter,監控你應用的數據庫訪問情況。
擴展JDBC,如果你要對JDBC層有編程的需求,可以通過Druid提供的Filter機制,很方便編寫JDBC層的擴展插件。
可以先去druid的官網下載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
創建一個ApplicationContext.xml