Srping配置阿里数据库连接池(Druid)

Srping配置阿里数据库连接池(Druid)

POM.XML

<dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>druid</artifactId>
     <version>1.0.13</version>
</dependency>

  

 

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>/static/*,*.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>

	<!-- druid监控 -->
	<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>

 spring配置

<!-- 数据源配置, 使用 BoneCP 数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  <!-- 数据源驱动类可不写,Druid默认会自动根据URL识别DriverClass -->
  <property name="driverClassName" value="com.mysql.jdbc.Driver" />
 
    <!-- 基本属性 url、user、password -->
    <property name="url" value="jdbc:mysql://127.0.0.1:3306/html580?useUnicode=true&characterEncoding=utf-8" />
    <property name="username" value="root" />
    <property name="password" value="123456" />
 
    <!-- 配置初始化大小、最小、最大 -->
    <property name="initialSize" value="3" />
    <property name="minIdle" value="3" />
    <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的大小(Oracle使用)
    <property name="poolPreparedStatements" value="true" />
    <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> -->
 
    <!-- 配置监控统计拦截的filters -->
  <property name="filters" value="stat" />
</bean>

 启动中间件,打开druid/index.html页面进行监控

 

数据源、SQL监控、SQL防火墙、Web应用、URI监控、Session监控、spring监控、JSON API

 

 

参考:Druid是什么和用StatViewServlet用于展示Druid的统计信息

你可能感兴趣的:(Srping配置阿里数据库连接池(Druid))