Druid 是阿里巴巴的开源数据库连接池技术,相比dbcp、c3p0更优秀,具有密码加密、sql连接监控功能,无论在性能还是稳定性上表现都十分出色。
一、加入jar文件 maven仓库地址:http://central.maven.org/maven2/com/alibaba/druid/ jar包下载地址:http://central.maven.org/maven2/com/alibaba/druid/1.1.2/druid-1.1.2.jar
二、spring.xml配置: 在context.hibernate1.xml中进行配置,具体配置如下:
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" 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}" />
<property name="validationQuery" value="${druid.validationQuery}" />
<property name="testWhileIdle" value="${druid.testWhileIdle}" />
<property name="testOnBorrow" value="${druid.testOnBorrow}" />
<property name="testOnReturn" value="${druid.testOnReturn}" />
<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;config.decrypt.key=${oracle.publickey}" />
</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>
###jdbc.properties
<!-- 数据库基本信息配置 -->
oracle.driver = oracle.jdbc.driver.OracleDriver
oracle.url = jdbc:oracle:thin:@localhost:1521:makecardcl
oracle.username = MakeCard_CL
oracle.password = ZWV95JNDIhKl90iz1hy0uc+n0Fav0LO/U/36PkO4AuXMnuz+MiXmkYDqeICk3ONeTeEDe/BctZ+pVwnB0sK51A==
oracle.publickey = MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIQdIhCTaX4fSqZaKcaGDODShQ8x832eLgM9IWQPlCxYSxaSwhsy/mKtq8bLuodVBGrDLSWLLbAdb5PX/ShyBaECAwEAAQ==
<!-- 初始化连接数量 -->
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
<!-- 用来检测连接是否有效的sql,要求是一个查询语句-->
druid.validationQuery = SELECT 1 FROM DUAL
<!-- 申请连接的时候检测 -->
druid.testWhileIdle = false
<!-- 申请连接时执行validationQuery检测连接是否有效,配置为true会降低性能 -->
druid.testOnBorrow = false
<!-- 归还连接时执行validationQuery检测连接是否有效,配置为true会降低性能 -->
druid.testOnReturn = false
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
druid.poolPreparedStatements = true
druid.maxPoolPreparedStatementPerConnectionSize = 100
filters =stat
###使用druid.jar 包生成密码 通过cmd命令,生产publickey ,password
Java -cp druid-1.0.9.jar com.alibaba.druid.filter.config.ConfigTools you_password
加密实现参考链接如下: 加密实现:http://blog.csdn.net/xlgen157387/article/details/51260930 ###访问内置监控页面 http://IP:Port/druid/index.html
###参考地址 官方githup:https://github.com/alibaba/druid 详细参数说明:http://www.cnblogs.com/wuyun-blog/p/5679073.html