springMVC+mybatis+DruidDataSource 连接一直没释放

Jack_Ruin 发布于 2015/06/02 16:07
阅读 8K+
收藏 0

我是用springmvc+mybatis集成的框架,数据源使用的是DruidDataSource,连接数最大设置为20,使用spring监听器监听在线人数,发现连接一直增加,没有释放,直到最后报错,如下:

 2015-6-2 15:55:05 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet springMvc threw exception
com.alibaba.druid.pool.GetConnectionTimeoutException: loopWaitCount 0, wait millis 60000
at com.alibaba.druid.pool.DruidDataSource.pollLast(DruidDataSource.java:1215)
at com.alibaba.druid.pool.DruidDataSource.getConnectionInternal(DruidDataSource.java:894)
at com.alibaba.druid.pool.DruidDataSource.getConnectionDirect(DruidDataSource.java:777)
at com.alibaba.druid.filter.FilterChainImpl.dataSource_connect(FilterChainImpl.java:4534)
at com.alibaba.druid.filter.stat.StatFilter.dataSource_getConnection(StatFilter.java:659)
at com.alibaba.druid.filter.FilterChainImpl.dataSource_connect(FilterChainImpl.java:4530)
at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:759)
at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:751)
at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:87)
at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
at org.mybatis.spring.transaction.SpringManagedTransaction.openConnection(SpringManagedTransaction.java:80)
at org.mybatis.spring.transaction.SpringManagedTransaction.getConnection(SpringManagedTransaction.java:66)
at org.apache.ibatis.executor.BaseExecutor.getConnection(BaseExecutor.java:279)
at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:69)
at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:56)
at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:267)
at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:141)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:105)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:81)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:101)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:95)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:59)
at com.rz.baseframework.core.dao.impl.BaseDaoImpl.getCountByParam(BaseDaoImpl.java:70)
at com.rz.baseframework.core.service.impl.OnlineServiceImpl.datagrid(OnlineServiceImpl.java:30)
at com.rz.baseframework.core.controller.OnlineController.datagrid(OnlineController.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:643)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:140)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)


数据源配置(部分):


<!-- 配置数据源 -->
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_username}" />
<property name="password" value="${jdbc_password}" />


<!-- 初始化连接大小 -->
<property name="initialSize" value="1" />
<!-- 连接池最大使用连接数量 -->
<property name="maxActive" value="20" />
<!-- 连接池最小空闲 -->
<property name="minIdle" value="1" />
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="60000" />


<!--  <property name="poolPreparedStatements" value="true" /> <property 
name="maxPoolPreparedStatementPerConnectionSize" value="33" />  -->


<property name="validationQuery" value="${validationQuery}" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="testWhileIdle" value="true" />


<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="25200000" />


<!-- 打开removeAbandoned功能 -->
<!-- 配置removeAbandoned对性能会有一些影响,建议怀疑存在泄漏之后再打开。
在上面的配置中,如果连接超过30分钟未关闭,
就会被强行回收,并且日志记录连接申请时的调用堆栈。 -->
<property name="removeAbandoned" value="false" />
<!-- 1800秒,也就是30分钟 -->
<property name="removeAbandonedTimeout" value="1800" />
<!-- 关闭abanded连接时输出错误日志 -->
<property name="logAbandoned" value="true" />


<!-- 监控数据库 -->
<property name="filters" value="mergeStat" /> 
<!--<property name="filters" value="stat" />-->
</bean>


<!-- myBatis文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--引用别名配置文件,在mapper配置文件里引用时,就可以直接使用别名  -->
<property name="configLocation" value="classpath:configs/mybatis-config.xml"/>
<!-- 自动扫描mapping目录, 省掉mybatis-config.xml里的手工配置 -->
<property name="mapperLocations" value="classpath:com/rz/**/mapping/*.xml" />
</bean>
<!-- 使用mapper接口的时候扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.rz..*.mapping" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>

<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>


加载中
0
蔡熙伟
蔡熙伟
你是不是忘了这个

<tx:annotation-driven transaction-manager="transactionManager" />

0
J
Jack_Ruin

引用来自“蔡熙伟”的评论

你是不是忘了这个

<tx:annotation-driven transaction-manager="transactionManager" />

我是用的是这种方式

<!-- 拦截器方式配置事物 -->

<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="append*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="repair" propagation="REQUIRED" />
<tx:method name="delAndRepair" propagation="REQUIRED" />


<tx:method name="get*" propagation="SUPPORTS" />
<tx:method name="find*" propagation="SUPPORTS" />
<tx:method name="load*" propagation="SUPPORTS" />
<tx:method name="search*" propagation="SUPPORTS" />
<tx:method name="datagrid*" propagation="SUPPORTS" />


<tx:method name="*" propagation="SUPPORTS" />
</tx:attributes>

</tx:advice>

蔡熙伟
蔡熙伟
那应该是某个方法没有释放连接
0
帐号以作废
帐号以作废
楼主,解决了吗,遇到同样的问题,请指导
J
Jack_Ruin
配置文件的问题,只配置了sqlsessionfactory,没有配置sqlsessiontemplate,
0
J
Jack_Ruin
配置文件的问题,只配置了sqlsessionfactory,没有配置sqlsessiontemplate,
0
MLGKO
MLGKO

没有配置sqlsessiontemplate就不会自动关闭吗??

刘王林123
怎么解决的,这几天也碰到,求答案
返回顶部
顶部