结合 spring 使用阿里 Druid 连接池配置方法
1.数据源
<!-- 配置数据源 --> <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="${jdbc.drivername}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 初始化连接大小 --> <property name="initialSize" value="${initialSize}" /> <!-- <property name="initialSize" value="0" /> --> <!-- 连接池最大使用连接数量 --> <property name="maxActive" value="${maxActive}" /> <!-- <property name="maxActive" value="20" /> --> <!-- 连接池最大空闲 <property name="maxIdle" value="${maxIdle}" /> --> <!-- 连接池最小空闲 --> <property name="minIdle" value="${minIdle}" /> <!-- 获取连接最大等待时间 --> <property name="maxWait" value="${maxWait}" /> <!-- <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> --> <property name="validationQuery" value="${validationQuery}" /> <property name="testOnBorrow" value="${testOnBorrow}" /> <property name="testOnReturn" value="${testOnReturn}" /> <property name="testWhileIdle" value="${testWhileIdle}" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}" /> <!-- 打开removeAbandoned功能 --> <property name="removeAbandoned" value="${removeAbandoned}" /> <!-- 1800秒,也就是30分钟 --> <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" /> <!-- 关闭abanded连接时输出错误日志 --> <property name="logAbandoned" value="${logAbandoned}" /> <!-- 监控数据库 --> <property name="filters" value="${filters}" /> </bean>
2.pom.xml
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.15</version> </dependency>
3.jdbc
jdbc.drivername=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://192.168.0.100:3306/databasename?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull jdbc.username=root jdbc.password=123456 initialSize=0 maxActive=20 maxIdle=20 minIdle=0 maxWait=10000 validationQuery=SELECT 1 testOnBorrow=false testOnReturn=false testWhileIdle=true timeBetweenEvictionRunsMillis=6000 minEvictableIdleTimeMillis=25200000 removeAbandoned=true removeAbandonedTimeout=1800 logAbandoned=true filters=stat
4.web.xml添加如下配置
<!-- 连接池监控 --> <filter> <filter-name>DruidWebStatFilter</filter-name> <filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class> <init-param> <param-name>exclusions</param-name> <param-value>*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*</param-value> </init-param> </filter> <filter-mapping> <filter-name>DruidWebStatFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <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>
5.连接池监控
启动服务,访问 http://服务IP地址:端口号/项目地址/druid/index.html