数据库连接池Druid的配置

1、applicationContext.xml

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

2、Web.xml

    <!--连接池监控-->
    <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>

 

转载于:https://www.cnblogs.com/TCrow/p/3143670.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值