SpringMVCDruid连接池配置使⽤
这次在项⽬中遇到了⼀个奇怪的问题, 就是在每隔⼀段时间访问服务器就会报连接超时的错误,但在次请求就不会出现该问题,刚开始以为是服务器那边数据库的问题,但是发现其他项⽬中都没有这种问题,所以查了很久发现原来是数据连接池这块的相关参数根本没有配置,这才导致出现的这样的错误,耽搁的这么长时间;都怪我太⼤意,所以写下来这个错误,下次就不要在犯了;
这⾥推荐⼀个很好⽤的数据连接池:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:p="/schema/p"
xmlns:tx="/schema/tx"
xmlns:context="/schema/context"
xmlns:aop="/schema/aop"
xsi:schemaLocation="/schema/beans
/schema/beans/spring-beans-3.0.xsd
/schema/tx
/schema/tx/spring-tx-3.0.xsd
/schema/context
/schema/context/spring-context-3.0.xsd
/schema/aop
/schema/aop/spring-aop-3.0.xsd ">
<context:component-scan base-package="shop.base.service.impl" />
<context:annotation-config />
<tx:annotation-driven />
<!-- 读取JDBC的配置-->
<bean id="propertyConfigurer" class="org.springframework.fig.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:jdbc.properties"/>
</bean>
<!-- 配置数据源,从上⾯配置⽂件读取-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="sql.jdbc.Driver" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!--==============DRUID连接池相关配置==================-->
<!--初始化时建⽴物理连接的个数-->
<property name="initialSize" value="5"/>
<!--最⼩连接池数量-->
<property name="minIdle" value="10"/>
<!--最⼤连接池数量-->
<property name="maxActive" value="100"/>
<!--连接时最⼤等待时间,单位毫秒-->
<property name="maxWait" value="60000"/>
<!--配置间隔多久才进⾏⼀次检测,检测需要关闭的空闲连接,单位是毫秒-->
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<!--配置⼀个连接在池中最⼩⽣存的时间,单位是毫秒-->
<property name="minEvictableIdleTimeMillis" value="300000"/>
<!--⽤来检测连接是否有效的SQL,要求是⼀个查询语句-->
<property name="validationQuery" value="SELECT 1 FROM DUAL"/>
<property name="validationQuery" value="SELECT 1 FROM DUAL"/>
<!--申请连接时执⾏ validationQuery 检测连接是否有效,做了这个配置会降低性能-->
<property name="testOnBorrow" value="true"/>
<!--归还连接时执⾏ validationQuery 检测连接是否有效,做了这个配置会降低性能-->
<property name="testOnReturn" value="true"/>
<!--建议配置为true,不影响性能,并且保证安全性;申请连接的时候检测,如果空闲时间⼤于
timeBetweenEvictionRunsMillis,执⾏ validationQuery 检测连接是否有效-->
<property name="testWhileIdle" value="true"/>
<!--是否缓存preparedStatement,也就是 PSCache-->
<property name="poolPreparedStatements" value="true"/>
<!--指定每个连接上 PSCache 的⼤⼩-->
<property name="maxOpenPreparedStatements" value="20"/>
<!-- 打开清除废弃连接功能 -->
<property name = "removeAbandoned" value = "true"/>
<!--清除废弃连接的超时时间-->
<property name="removeAbandonedTimeout" value="1800"/>
<!-- 关闭清除连接时输出错误⽇志 -->
<property name = "logAbandoned" value = "true"/>
<!--配置监控统计拦截的filters,属性类型是字符串,通过别名的⽅式配置扩展插件,
常⽤的插件有:监控统计⽤的STAT;⽇志⽤的log4j;防御SQL注⼊的wall-->
<property name="filters" value="stat,wall,log4j"/>
<!--通过 connectProperties 属性来打开mergeSql功能;慢SQL记录-->
<property name="connectionProperties" value="Sql=ture;druid.stat.slowSqlMillis=5000"/>
<!--合并多个DruidDataSource的监控数据-->
<property name="useGlobalDataSourceStat" value="true"/>
</bean>
<!-- MyBatis_Plus配置: -->
<bean id="sqlSessionFactory" class="batisplus.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 配置扫描实体的包路径 -->
<property name="typeAliasesPackage" value="ity"/>
<!-- 配置扫描 MAPPER XML的位置 -->
<property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"/>
<!-- 配置 MyBatis配置⽂件的位置 -->
<property name="configLocation" value="classpath:l"/>
<property name="globalConfig" ref="globalConfig"/>
<!-- 配置插件 -->
<property name="plugins">
<array>
<!-- 分页插件配置 -->
<bean id="paginationInterceptor" class="batisplus.plugins.PaginationInterceptor">
<property name="dialectType" value="mysql"/>
<property name="optimizeType" value="aliDruid" />
</bean>
</array>
</property>
</bean>
<!-- MP 全局配置注⼊ -->
<bean id="globalConfig" class="ity.GlobalConfiguration">
<property name="idType" value="0"/>
<property name="dbColumnUnderline" value="true"/>
</bean>
<!-- 配置扫描MAPPER接⼝的包路径 -->
<bean id="mapperScannerConfigurer" class="batis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="shop.base.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!-- 事务处理  -->
<bean id= "transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  <property name="dataSource" ref="dataSource"/>
</bean>
<!-- 事务注解:开启注解⽀持-->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
<!-- 全局AOP事物,除get,list,select,query开头的⽅法外,都处在事物当中  -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
druid连接池配置详解<tx:attributes>
<tx:method name="select*" propagation="REQUIRED" read-only="true" />
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="list*" propagation="REQUIRED" read-only="true" />
<tx:method name="query*" propagation="REQUIRED" read-only="true" />
<tx:method name="*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"/>
</tx:attributes>
</tx:advice>
<!-- 配置事务切⾯到Service层 -->
<aop:config expose-proxy="true" proxy-target-class="true" >
<aop:advisor advice-ref="txAdvice" pointcut="execution(* shop.base.service..*.*(..))"/>
</aop:config>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="/2001/XMLSchema-instance" xmlns="java.sun/xml/ns/javaee" xsi:schemaLocation="java.sun/xml/ns/j  <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:l</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>org.t.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>SetCharacterEncoding</filter-name>
<filter-class>
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SetCharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>Spring MVC</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:l</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Spring MVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<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/*,*.ico,/druid/*</param-value>
</init-param>
<init-param>
<param-name>sessionStatEnable</param-name>
<param-value>false</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>        <init-param>
<!-- ⽤户名 -->
<param-name>loginUsername</param-name>
<param-value>admin</param-value>
</init-param>
<init-param>
<!-- 密码 -->
<param-name>loginPassword</param-name>
<param-value>admin</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DruidStatView</servlet-name>
<url-pattern>/druid/*</url-pattern>
</servlet-mapping>
</web-app>
这样就可以在该服务器下  /druid 中访问 Druid 中拦截到的信息了;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。