結合 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.htmlcss

相關文章
相關標籤/搜索