1. 程式人生 > >spring 配置 druid

spring 配置 druid

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
    xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"  
    xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task"  
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd  
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd  
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd  
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd  
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd  
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd  
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd  
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">  
  
    <!-- 配置druid資料來源 -->  
    <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource"  
        init-method="init" destroy-method="close">  
        <!-- 資料庫連線基礎資訊 -->  
        <property name="url" value="${jdbc_url}" />  
        <property name="username" value="${jdbc_username}" />  
        <property name="password" value="${jdbc_password}" />  
  
        <!-- 初始化連線大小 -->  
        <property name="initialSize" value="0" />  
        <!-- 連線池最大使用連線數量 -->  
        <property name="maxActive" value="1500" />  
        <!-- 連線池最小空閒 -->  
        <property name="minIdle" value="0" />  
        <!-- 獲取連線最大等待時間 -->  
        <property name="maxWait" value="60000" />  
  
        <!-- 是否快取preparedStatement,也就是PSCache。PSCache對支援遊標的資料庫效能提升巨大,比如說oracle。在mysql下建議關閉。 -->  
        <!-- <property name="poolPreparedStatements" value="true" /> <property   
            name="maxPoolPreparedStatementPerConnectionSize" value="33" /> -->  
  
        <!-- 驗證資料庫連線有效性,要求查詢語句 -->  
        <property name="validationQuery" value="${validationQuery}" />  
        <!-- 建議配置為true,不影響效能,並且保證安全性。申請連線的時候檢測,如果空閒時間大於timeBetweenEvictionRunsMillis,執行validationQuery檢測連線是否有效。 -->  
        <property name="testWhileIdle" value="true" />  
        <!-- 申請連線時執行validationQuery檢測連線是否有效,配置true會降低效能。 -->  
        <property name="testOnBorrow" value="false" />  
        <!-- 歸還連線時執行validationQuery檢測連線是否有效,配置true會降低效能 -->  
        <property name="testOnReturn" value="false" />  
  
        <!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連線,單位是毫秒 -->  
        <property name="timeBetweenEvictionRunsMillis" value="60000" />  
        <!-- 配置一個連線在池中最小生存的時間,單位是毫秒 -->  
        <property name="minEvictableIdleTimeMillis" value="25200000" />  
  
        <!-- 對於長時間不使用的連線強制關閉 -->  
        <property name="removeAbandoned" value="true" />  
        <!-- 關閉超過30分鐘的空閒連線,1800秒,也就是30分鐘 -->  
        <property name="removeAbandonedTimeout" value="1800" />  
        <!-- 關閉abanded連線時輸出錯誤日誌 -->  
        <property name="logAbandoned" value="true" />  
  
        <!-- 監控資料庫 -->  
        <!-- <property name="filters" value="mergeStat" /> -->  
        <property name="filters" value="stat" />  
    </bean>  
  
    <!-- 配置druid監控spring jdbc -->  
    <bean id="druid-stat-interceptor"  
        class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor" />  
  
    <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut"  
        scope="prototype">  
        <property name="patterns">  
            <list>  
                <value>spring.druid.service.*</value>  
            </list>  
        </property>  
    </bean>  
    <aop:config>  
        <aop:advisor advice-ref="druid-stat-interceptor"  
            pointcut-ref="druid-stat-pointcut" />  
    </aop:config>  
  
</beans>