Mybatis jdbc连接超时解决

这两天在测试Hive权限控制代码Hamza,发现每天来的时候第一次老是会报出以下错误:

[html]  view plain  copy
  1. 2015-03-26 09:40:25.956 ERROR GroupPrivController,119 -   
  2. ### Error querying database.  Cause: org.apache.ibatis.transaction.TransactionException: Error configuring AutoCommit.  Your driver may not support getAutoCommit() or setAutoCommit(). Requested setting: false.  Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 57,704,088 milliseconds ago.  The last packet sent successfully to the server was 57,704,089 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.  
  3. ### The error may exist in mybatis/hiveDb.xml  
  4. ### The error may involve com.weibo.hamza.core.dao.DbDao.findByOwnerId  
  5. ### The error occurred while executing a query  
  6. ### Cause: org.apache.ibatis.transaction.TransactionException: Error configuring AutoCommit.  Your driver may not support getAutoCommit() or setAutoCommit(). Requested setting: false.  Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 57,704,088 milliseconds ago.  The last packet sent successfully to the server was 57,704,089 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.  
  7. org.apache.ibatis.exceptions.PersistenceException:   
  8. ### Error querying database.  Cause: org.apache.ibatis.transaction.TransactionException: Error configuring AutoCommit.  Your driver may not support getAutoCommit() or setAutoCommit(). Requested setting: false.  Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 57,704,088 milliseconds ago.  The last packet sent successfully to the server was 57,704,089 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.  
  9. ### The error may exist in mybatis/hiveDb.xml  
  10. ### The error may involve com.weibo.hamza.core.dao.DbDao.findByOwnerId  
  11. ### The error occurred while executing a query  
  12. ### Cause: org.apache.ibatis.transaction.TransactionException: Error configuring AutoCommit.  Your driver may not support getAutoCommit() or setAutoCommit(). Requested setting: false.  Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 57,704,088 milliseconds ago.  The last packet sent successfully to the server was 57,704,089 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.  
  13.     at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23)  
  14.     at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:104)  
  15.     at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:95)  
  16.     at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:124)  
  17.     at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:90)  
  18.     at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:40)  
  19.     at com.sun.proxy.$Proxy13.findByOwnerId(Unknown Source)  
  20.     at com.weibo.hamza.core.service.impl.DbServiceImpl.findByOwnerId(DbServiceImpl.java:124)  
  21.     at com.weibo.hamza.auth.hessian.AuthServiceImpl.findDbByOwnerId(AuthServiceImpl.java:55)  
  22.     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
  23.     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)  
  24.     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)  
  25.     at java.lang.reflect.Method.invoke(Method.java:606)  
  26.     at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:319)  
  27.     at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)  
  28.     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)  
  29.     at org.springframework.remoting.support.RemoteInvocationTraceInterceptor.invoke(RemoteInvocationTraceInterceptor.java:77)  
  30.     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)  
  31.     at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)  
  32.     at com.sun.proxy.$Proxy4.findDbByOwnerId(Unknown Source)  
  33.     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
  34.     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)  
  35.     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)  
  36.     at java.lang.reflect.Method.invoke(Method.java:606)  
  37.     at com.caucho.hessian.server.HessianSkeleton.invoke(HessianSkeleton.java:180)  
  38.     at com.caucho.hessian.server.HessianSkeleton.invoke(HessianSkeleton.java:109)  
  39.     at org.springframework.remoting.caucho.HessianExporter.doInvoke(HessianExporter.java:198)  
  40.     at org.springframework.remoting.caucho.HessianExporter.invoke(HessianExporter.java:118)  
  41.     at org.springframework.remoting.caucho.HessianServiceExporter.handleRequest(HessianServiceExporter.java:66)  
  42.     at org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.handle(HttpRequestHandlerAdapter.java:49)  
  43.     at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)  
  44.     at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)  
  45.     at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)  
  46.     at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)  
  47.     at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)  
  48.     at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)  
  49.     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306)  
  50.     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)  
  51.     at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)  
  52.     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)  
  53.     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:244)  
  54.     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)  
  55.     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)  
  56.     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)  
  57.     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)  
  58.     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)  
  59.     at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:541)  
  60.     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)  
  61.     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:383)  
  62.     at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)  
  63.     at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)  
  64.     at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)  
  65.     at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:288)  
  66.     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)  
  67.     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)  
  68.     at java.lang.Thread.run(Thread.java:745)  
  69. Caused by: org.apache.ibatis.transaction.TransactionException: Error configuring AutoCommit.  Your driver may not support getAutoCommit() or setAutoCommit(). Requested setting: false.  Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 57,704,088 milliseconds ago.  The last packet sent successfully to the server was 57,704,089 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.  
  70.     at org.apache.ibatis.transaction.jdbc.JdbcTransaction.setDesiredAutoCommit(JdbcTransaction.java:102)  
  71.     at org.apache.ibatis.transaction.jdbc.JdbcTransaction.openConnection(JdbcTransaction.java:135)  
  72.     at org.apache.ibatis.transaction.jdbc.JdbcTransaction.getConnection(JdbcTransaction.java:58)  
  73.     at org.apache.ibatis.executor.BaseExecutor.getConnection(BaseExecutor.java:279)  
  74.     at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:69)  
  75.     at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:56)  
  76.     at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:267)  
  77.     at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:141)  
  78.     at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:105)  
  79.     at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:81)  
  80.     at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:101)  
  81.     ... 54 more  
  82. Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 57,704,088 milliseconds ago.  The last packet sent successfully to the server was 57,704,089 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.  
  83.     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)  
 很明显, 是连接出现问题了。原来mysql服务端会默认检查每个连接connection,一旦发现connection被闲置8小时(wait_timeout),便会被断开。那我们可以在连接字符串中加入autoReconnect=true,同时配置下mybatis,一段时间又默认执行下简单的查询,表明自己这个连接还活着,具体配置如下:

[html]  view plain  copy
  1. <environments default="development">   
  2.    <environment id="development">   
  3.        <transactionManager type="jdbc"/>   
  4.        <dataSource type="POOLED">   
  5.            <property name="url" value="${cp.Url}"/>  
  6.            <property name="driver" value="${cp.DriverClassName}"/>  
  7.            <property name="username" value="${cp.Username}"/>   
  8.            <property name="password" value="${cp.Password}"/>   
  9.            <property name="poolPingEnabled" value="true"/>  
  10.            <property name="poolPingQuery" value="select 1"/>  
  11.            <property name="poolPingConnectionsNotUsedFor" value="3600000"/>  
  12.        </dataSource>   
  13.    </environment>   
  14. </environments>   

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值