Druid 是阿里巴巴的开源数据库连接池技术,相比dbcp、c3p0更优秀,具有密码加密、sql连接监控功能,无论在性能还是稳定性上表现都十分出色。
spring.xml
<context:property-placeholder location="classpath:jdbc.properties"/> <bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 数据库基本信息配置 --> <property name="driverClassName" value="${oracle.driver}" /> <property name="url" value="${oracle.url}" /> <property name="username" value="${oracle.username}" /> <property name="password" value="${oracle.password}" /> <!-- 初始化连接数量 --> <property name="initialSize" value="${druid.initialSize}" /> <!-- 最大并发连接数 --> <property name="maxActive" value="${druid.maxActive}" /> <!-- 最大空闲连接数 --> <property name="maxIdle" value="${druid.maxIdle}" /> <!-- 最小空闲连接数 --> <property name="minIdle" value="${druid.minIdle}" /> <!-- 配置获取连接等待超时的时间 --> <property name="maxWait" value="${druid.maxWait}" /> <!-- 超过时间限制是否回收 --> <property name="removeAbandoned" value="${druid.removeAbandoned}" /> <!-- 超过时间限制多长; --> <property name="removeAbandonedTimeout" value="${druid.removeAbandonedTimeout}" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="${druid.timeBetweenEvictionRunsMillis}" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="${druid.minEvictableIdleTimeMillis}" /> <!-- 用来检测连接是否有效的sql,要求是一个查询语句--> <property name="validationQuery" value="${druid.validationQuery}" /> <!-- 申请连接的时候检测 --> <property name="testWhileIdle" value="${druid.testWhileIdle}" /> <!-- 申请连接时执行validationQuery检测连接是否有效,配置为true会降低性能 --> <property name="testOnBorrow" value="${druid.testOnBorrow}" /> <!-- 归还连接时执行validationQuery检测连接是否有效,配置为true会降低性能 --> <property name="testOnReturn" value="${druid.testOnReturn}" /> <!-- 打开PSCache,并且指定每个连接上PSCache的大小 --> <property name="poolPreparedStatements" value="${druid.poolPreparedStatements}" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="${druid.maxPoolPreparedStatementPerConnectionSize}" /> <!--属性类型是字符串,通过别名的方式配置扩展插件,常用的插件有: 监控统计用的filter:stat 日志用的filter:log4j 防御SQL注入的filter:wall --> <!--<property name="filters" value="${druid.filters}" /> --> <!-- 解密密码必须要配置的项 config, stat启用监控过滤器--> <property name="filters" value="config,stat" /> <property name="connectionProperties" value="config.decrypt=true" /> </bean>
web.xml 中配置sql监控
<servlet> <servlet-name>DruidStatView </servlet-name> <servlet-class>com.alibaba.druid.support.http.StatViewServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>DruidStatView </servlet-name> <url-pattern>/druid/*</url-pattern> </servlet-mapping>
oracle.driver = oracle.jdbc.driver.OracleDriver oracle.url = jdbc:oracle:thin:@127.0.0.1:1521:XE oracle.username = jelly oracle.password = SZRwkwTlSCh/pU3hXnVEIZ/RWXTltUAWWHZxUpOSI/peAToVyCViyxpz371Agv6yqz9YRpQIBZK5SwZNPKz++A== druid.initialSize = 3 druid.maxActive = 10 druid.maxIdle=5 druid.minIdle = 2 druid.maxWait = 60000 druid.removeAbandoned = true druid.removeAbandonedTimeout = 180 druid.timeBetweenEvictionRunsMillis = 60000 druid.minEvictableIdleTimeMillis = 300000 druid.validationQuery = SELECT 1 FROM DUAL druid.testWhileIdle = false druid.testOnBorrow = false druid.testOnReturn = false druid.poolPreparedStatements = true druid.maxPoolPreparedStatementPerConnectionSize = 20 filters =stat
cmd命令
java -cp druid-1.0.9.jar com.alibaba.druid.filter.config.ConfigTools you_password
浏览器访问Druid 监控页面:
http://localhost:8090/xedemo/druid/