美文网首页
Spring中使用Druid连接池配置详解

Spring中使用Druid连接池配置详解

作者: greensure | 来源:发表于2019-05-24 11:51 被阅读0次

转载出处

jdbc.properties

url=jdbc:postgresql://***.***.***.***:****/****
username=***
password=***

applicationContext.xml中配置bean

<!-- 阿里 druid 数据库连接池 -->  
  <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">   
    <!-- 基本属性 url、user、password -->  
    <property name="url" value="${url}" />  
    <property name="username" value="${username}" />  
    <property name="password" value="${password}" />  

    <!-- 配置初始化大小、最小、最大 -->  
    <property name="initialSize" value="1" />  
    <property name="minIdle" value="1" />   
    <property name="maxActive" value="20" />  

    <!-- 配置获取连接等待超时的时间 -->  
    <property name="maxWait" value="60000" />  

    <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->  
    <property name="timeBetweenEvictionRunsMillis" value="60000" />  

    <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->  
    <property name="minEvictableIdleTimeMillis" value="300000" />  

    <property name="validationQuery" value="SELECT 'x'" />  
    <property name="testWhileIdle" value="true" />  
    <property name="testOnBorrow" value="false" />  
    <property name="testOnReturn" value="false" />  

    <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->  
    <property name="poolPreparedStatements" value="true" />  
    <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />  

    <!-- 配置监控统计拦截的filters,去掉后监控界面sql无法统计 -->  
    <property name="filters" value="stat" />   
</bean>  

监控配置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>

该配置可以访问监控界面,配置好后,可以通过
http://ip:port/项目名/druid/index.html
监控数据库访问性能

相关文章

网友评论

      本文标题:Spring中使用Druid连接池配置详解

      本文链接:https://www.haomeiwen.com/subject/snhgzqtx.html