解决异常:“The last packet sent successfully to the server was 0 milliseconds ago. ”的办法

时间:2024-02-20 11:57:59

  出现异常”The last packet sent successfully to the server was 0 milliseconds ago.“的大部分原因是由于数据库回收了连接,而系统的缓冲池不知道,继续使用被回收的连接所致的。

           以mysql为例:

           第一种解决办法,就是将mysql回收空闲连接的时间变长,mysql默认回收时间是8小时,可以在mysql目录下的my.ini中增加下面配置,将时间改为1天。

           单位是秒,最大好像是24天:

          [mysqld]

          wait_timeout=86400

           第二种解决办法,可以通过配置,让缓冲池去测试连接是否被回收,如果被回收,则不继续使用,以dbcp为例:

          #SQL查询,用来验证从连接池取出的连接
          dbcp.validationQuery=SELECT 1
          #指明连接是否被空闲连接回收器(如果有)进行检验,如果检测失败,则连接将被从池中去除
          dbcp.testWhileIdle=true
          #在空闲连接回收器线程运行期间休眠的时间值,以毫秒为单位,一般比minEvictableIdleTimeMillis小
          dbcp.timeBetweenEvictionRunsMillis=300000
          #在每次空闲连接回收器线程(如果有)运行时检查的连接数量,最好和maxActive一致
         dbcp.numTestsPerEvictionRun=50
          #连接池中连接,在时间段内一直空闲,被逐出连接池的时间(1000*60*60),以毫秒为单位
          dbcp.minEvictableIdleTimeMillis=3600000

*********************************

 

 如何解决:待定;
  分析问题:
  第一:因为自己的appkey和appSecret是正确的,并且这个程序在家里面的电脑是没有问题的,但是在公司执行的时候就报错误。
  第二:是不是其他公司代理导致问题存在呢?还是?下面达内老师为大家分享具体内容。

淘宝客开发过程遇到问题:
  //开始调用淘宝API接口
  TaobaoClient client = new DefaultTaobaoClient(url, appkey, appSecret);
  TaobaokeItemsGetRequest req=new TaobaokeItemsGetRequest();
   req.setFields("num_iid,title,nick,pic_url,price,click_url,commission,commission_rate,commission_num,commission_volume,shop_click_url,seller_credit_score,item_location,volume");
  //返回数据
  TaobaokeItemsGetResponse response = client.execute(req);
  List<TaobaokeItem> list = response.getTaobaokeItems();
  System.out.println(list.size());
  Pageinfo newpage =new Pageinfo(defaultCurpage, pageno, pagesize,  list);  //生成Page
  //生成json数据
  String listJson = JSON.toJSONString(newpage, true);
  System.out.println(listJson);
  错误代码如下:
  [java]
  2012-12-12 9:26:51 com.taobao.api.internal.util.TaobaoLogger _logCommError
  严重: 2012-12-12 09:26:51^_^taobao.taobaoke.items.get^_^1021298225^_^10.240.70.132^_^Windows XP^_^top-sdk-java-20121129^_^http://gw.api.tbsandbox.com/router/rest?sign=AC99D0A45E6D90ECF5D3F62E05F2EA9E×tamp=2012-12-12+09%3A26%3A49&v=2.0&app_key=1021298225&method=taobao.taobaoke.items.get&sign_method=hmac&partner_id=top-sdk-java-20121129&format=json^_^^_^Connection refused: connect
  com.taobao.api.ApiException: java.net.ConnectException: Connection refused: connect
  at com.taobao.api.DefaultTaobaoClient.doPost(DefaultTaobaoClient.java:200)
  at com.taobao.api.DefaultTaobaoClient._execute(DefaultTaobaoClient.java:105)
  at com.taobao.api.DefaultTaobaoClient.execute(DefaultTaobaoClient.java:83)
  at com.taobao.api.DefaultTaobaoClient.execute(DefaultTaobaoClient.java:71)
  at com.taobao.www.fanjf.action.DisplayPayedAction.getTaobaokeItemsListInit1(DisplayPayedAction.java:130)
  at com.taobao.www.fanjf.action.DisplayPayedAction.execute(DisplayPayedAction.java:55)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  at java.lang.reflect.Method.invoke(Unknown Source)
  at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:446)
  at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:285)
  at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
  at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
  at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
  at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
  at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
  at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
  at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
  at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
  at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
  at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
  at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
  at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
  at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:238)
  at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
  at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
  at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:238)
  at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
  at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
  at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:191)
 at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
  at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
  at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
  at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:90)
  at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
  at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:252)
  at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
  at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
  at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
  at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
  at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
  at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
  at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
  at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
  at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
  at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
  at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)
  at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
  at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
  at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
  at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:193)
  at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
  at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
  at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
  at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
  at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:544)
  at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
  at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
  at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
  at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:931)
  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
  at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
  at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
  at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
  at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
  at java.lang.Thread.run(Unknown Source)
  Caused by: java.net.ConnectException: Connection refused: connect
  at java.net.PlainSocketImpl.socketConnect(Native Method)
  at java.net.PlainSocketImpl.doConnect(Unknown Source)
  at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
  at java.net.PlainSocketImpl.connect(Unknown Source)
  at java.net.SocksSocketImpl.connect(Unknown Source)
  at java.net.Socket.connect(Unknown Source)
  at sun.net.NetworkClient.doConnect(Unknown Source)
  at sun.net.www.http.HttpClient.openServer(Unknown Source)
  at sun.net.www.http.HttpClient.openServer(Unknown Source)
  at sun.net.www.http.HttpClient.<init>(Unknown Source)
  at sun.net.www.http.HttpClient.New(Unknown Source)
  at sun.net.www.http.HttpClient.New(Unknown Source)
  at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
  at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
  at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
  at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
  at com.taobao.api.internal.util.WebUtils.doPost(WebUtils.java:115)
  at com.taobao.api.internal.util.WebUtils.doPost(WebUtils.java:88)
  at com.taobao.api.internal.util.WebUtils.doPost(WebUtils.java:68)
  at com.taobao.api.DefaultTaobaoClient.doPost(DefaultTaobaoClient.java:197)
  … 74 more

 以上就是今天达内IT培训老师为大家分享的有关Connection refused: connect错误的内容,相信能够帮助到大家。

************************************

今天在使用JDBC操作mysql时遇到下面的异常信息:

引用

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at com.tomymap.galaxy.virgo.util.DbService.getConnection(DbService.java:66)
at com.tomymap.galaxy.virgo.util.DbService.getConnection(DbService.java:46)
at com.tomymap.galaxy.virgo.dao.PNNDao.getConnection(PNNDao.java:51)
at com.tomymap.galaxy.virgo.dao.DaoBase.executeUpdate(DaoBase.java:69)
at com.tomymap.galaxy.virgo.dao.PNNDao.updatePNNRelation(PNNDao.java:161)
at com.tomymap.galaxy.virgo.pnn.PyramidNeuralNetwork.buildPNNRelations(PyramidNeuralNetwork.java:400)
at com.tomymap.galaxy.virgo.pnn.PyramidNeuralNetwork.incrementalGenPNN(PyramidNeuralNetwork.java:144)
at com.tomymap.galaxy.virgo.pnn.PyramidNeuralNetwork.main(PyramidNeuralNetwork.java:410)


着实让人崩溃的信息,花费了一天的时间来解决。
不罗嗦,直接描述解决办法

1 配置:
2 ubuntu10.10
3 5.1.49-1ubuntu8.1
4 mysql-connector-java-5.1.18.jar


交互:
300k records
300k * 10 = 3M次访问mysql
处理时间0.5h以内

解决方法:
(1)使用JDBC URL中使用autoReconnect属性,url添加

&autoReconnect=true&failOverReadOnly=false&maxReconnects=10

(2) 修改MySQL的参数. /etc/my.cnf 添加

[mysqld]
wait_timeout=31536000
interactive_timeout=31536000

(3)重启mysql

service mysql restart

原因分析:
(1)大量数据访问情况下,mysql connection连接有可能失效
(2)长时间不妨问,connection会失效

http://dev.mysql.com/doc/refman/5.1/zh/database-administration.html#dynamic-system-variables
http://www.blogjava.net/heweiya/archive/2006/01/18/28483.html