Detailed explanation of Druid connection pool configuration in JSP Spring

  • 2021-12-04 19:25:29
  • OfStack

Configuration of Druid Connection Pool in JSP Spring

jdbc.properties


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

Configuring bean in applicationContext. xml


<!--  Ali  druid  Database connection pool  --> 
 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">  
  <!--  Basic attribute  url , user , password --> 
  <property name="url" value="${url}" /> 
  <property name="username" value="${username}" /> 
  <property name="password" value="${password}" /> 

  <!--  Configure initialization size, minimum, maximum  --> 
  <property name="initialSize" value="1" /> 
  <property name="minIdle" value="1" />  
  <property name="maxActive" value="20" /> 

  <!--  Configure the time to get the connection wait timeout  --> 
  <property name="maxWait" value="60000" /> 

  <!--  How long does the configuration interval take place 1 Detects idle connections that need to be closed, in milliseconds  --> 
  <property name="timeBetweenEvictionRunsMillis" value="60000" /> 

  <!--  Configure 1 The minimum time, in milliseconds, for a connection to live in the pool  --> 
  <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" /> 

  <!--  Open PSCache And specifies that on each connection PSCache The size of  --> 
  <property name="poolPreparedStatements" value="true" /> 
  <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> 

  <!--  Configure the for monitoring statistics interception filters After removing the monitoring interface, sql Unable to count  --> 
  <property name="filters" value="stat" />  
</bean> 

Monitor configuration 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>

This configuration can access the monitoring interface. After configuration, you can use http://ip: port/Project Name/druid/index. html

Monitor database access performance

Thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: