數據庫鏈接池Druid的配置

一、applicationContext.xmlcss

    <bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter">
        <!--慢SQL統計,若是SQL執行時間超過必定時間則記錄爲慢SQL  -->
        <property name="slowSqlMillis" value="3000" />
        <!--慢SQL統計日誌輸出  -->
        <property name="logSlowSql" value="true" />
        <!--合併SQL統計 例如select * from table t where t.id =1,會被變爲select * from table t where t.id =?來統計  -->
        <property name="mergeSql" value="true" />
    </bean>

    <bean id="wall-filter-config" class="com.alibaba.druid.wall.WallConfig" init-method="init">
        <!-- 指定配置裝載的目錄  -->
        <property name="dir" value="META-INF/druid/wall/oracle" />
        <property name="selectWhereAlwayTrueCheck" value="false" />
        <property name="selectHavingAlwayTrueCheck" value="false" />
    </bean>

    <bean id="wall-filter" class="com.alibaba.druid.wall.WallFilter">
        <property name="dbType" value="oracle" />
        <property name="config" ref="wall-filter-config" />
    </bean>


    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <!-- 基本屬性 url、user、password -->
        <property name="url" value="jdbc:oracle:thin:@IP:PORT:SCHMA" />
        <property name="username" value="username" />
        <property name="password" value="password" />
        
        <!-- 配置初始化大小、最小、最大 -->
        <property name="initialSize" value="5"/>
        <property name="minIdle" value="5"/>
        <property name="maxActive" value="100"/>
        
        <!-- 配置獲取鏈接等待超時的時間 -->
        <property name="maxWait" value="60000" />
        
        <!-- 配置間隔多久才進行一次檢測,檢測須要關閉的空閒鏈接,單位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
        
        <!-- 配置一個鏈接在池中最小生存的時間,單位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="300000" />
        
        <property name="validationQuery" value="SELECT 'x' FROM DUAL" />
        <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" />
        
        <!-- 關閉長時間不使用的鏈接 -->
        <property name="removeAbandoned" value="true" /> <!-- 打開removeAbandoned功能 -->
        <property name="removeAbandonedTimeout" value="1200" /> <!-- 1200秒,也就是20分鐘 -->
        <property name="logAbandoned" value="true" /> <!-- 關閉abanded鏈接時輸出錯誤日誌 -->
        
        <!-- 配置監控統計攔截的filters -->
        <property name="proxyFilters">
            <list>
                <ref bean="stat-filter" />
                <ref bean="wall-filter" />
            </list>
        </property>
        <!-- 數據庫監控,及日誌配置 -->
        <property name="filters" value="log4j" />
        <!-- 合併統計多個數據源 -->
        <property name="useGloalDataSourceStat" value="true" />
    </bean>

    <!-- 配置Spring和Jdbc的關聯監控 -->
    <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut"
          scope="prototype">
        <property name="patterns">
            <list>
                <value>com.yourcompany.service.*</value>
                <value>com.yourcompany.dao.*</value>
            </list>
        </property>
    </bean>

    <bean id="druid-stat-interceptor"
          class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">
    </bean>

    <aop:config>
        <aop:advisor advice-ref="druid-stat-interceptor"
                     pointcut-ref="druid-stat-pointcut" />
    </aop:config>

二、Web.xmlhtml

    <!--鏈接池監控-->
    <servlet>
        <servlet-name>DruidStatView</servlet-name>
        <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
        <!-- 容許與拒絕查看數據庫監控信息的IP地址 -->
        <!-- 
        <init-param>
            <param-name>allow</param-name>
            <param-value>127.0.0.1,10.2.15.52</param-value>
        </init-param>
        
        <init-param>
            <param-name>deny</param-name>
            <param-value>127.0.0.1</param-value>
        </init-param>
         -->
        <!-- 登陸賬號方式校驗http://127.0.0.1:8080/yourapp/druid/login.html -->
        <init-param>
            <param-name>loginUsername</param-name>
            <param-value>username</param-value>
        </init-param>
        <init-param>
            <param-name>loginPassword</param-name>
            <param-value>password</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>DruidStatView</servlet-name>
        <url-pattern>/druid/*</url-pattern>
    </servlet-mapping>

    <!-- WebStatFilter用於採集web-jdbc關聯監控的數據 -->
    <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>
        <!-- 配置profileEnable可以監控單個url調用的sql列表 -->
        <init-param>
            <param-name>profileEnable</param-name>
            <param-value>true</param-value>
        </init-param>
        <!-- 你能夠配置principalSessionName,使得druid可以知道當前的session的用戶是誰 -->
        <init-param>
            <param-name>principalSessionName</param-name>
            <param-value>xxx.user</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>DruidWebStatFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
相關文章
相關標籤/搜索