druid 配置
连接池配置,以及相关配置
<bean id="logFilter" class="com.alibaba.druid.filter.logging.Slf4jLogFilter"> <property name="dataSourceLogEnabled" value="false" /> <property name="connectionLogEnabled" value="false" /> <property name="statementLogEnabled" value="false" /> <property name="resultSetLogEnabled" value="false" /> <property name="statementExecutableSqlLogEnable" value="true" /> </bean> <!-- 数据源配置,使用DruidDataSource数据库连接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 基本属性 url、user、password --> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="3"/> <property name="minIdle" value="1"/> <property name="maxActive" value="20"/> <!-- 配置获取连接等待超时的时间 --> <property name="maxWait" value="60000"/> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000"/> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <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"/> <!-- 打开PSCache,并且指定每个连接上PSCache的大小 --> <!--<property name="poolPreparedStatements" value="false"/>--> <!--<property name="maxPoolPreparedStatementPerConnectionSize" value="20"/>--> <!-- 配置监控统计拦截的filters --> <property name="filters" value="stat"/> <property name="proxyFilters"> <list> <ref bean="logFilter"/> </list> </property> </bean>
性能监控
<!-- druid方法性能监控,Controller需单独设置且需和mvc:annotation-driven放在同一个文件中,如spring-mvc.xml --> <bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor"> </bean> <bean id="druid-stat-pointcut-controller" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype"> <property name="patterns"> <list> <value>xx.xxx.theme.controller.*</value> </list> </property> </bean> <aop:config proxy-target-class="true"> <aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut-controller"/> </aop:config>
开启web监控。 web.xml
<!-- Druid数据源监控 --> <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/*,/public/*,/status/*</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>
参考:
https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_LogFilter