我正在测试一些处理网站注册的代码。 java代码如下(节选):

if (request.getParameter("method").equals("checkEmail")){ 
            String email= request.getParameter("email"); 
            ResultSet rs =null; 
            PreparedStatement ps = db.prepareStatement(query); 
            ps.setString(1, email); 
            rs = ps.executeQuery();              
            if(rs.next()){  
                            //email already present in Db  
            } else { 
                            //proceed with registration..... 

大多数时候进程执行没有任何问题,但我遇到间歇性问题,因为与数据库的连接正在关闭而失败。每次失败时,它都会在同一点失败 - 当运行上面的准备好的语句时(显然会检查正在提交的电子邮件是否已经在数据库中)。

Postgres 的版本是 8.1.23

感谢任何帮助或建议。 Stacktrace 如下(编辑:有时 Stacktrace 表示由 Stream Closed 引起,有时 Socket Closed 如下所示):

13:53:00,973 ERROR Registration:334 - org.postgresql.util.PSQLException: An I/O error occured while sending to the backend. 
 
  at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:283) 
  at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:479 
  at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:367) 
  at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:271) 
  at Registration.doPost(Registration.java:113) 
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:637) 
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) 
  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:128) 
  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) 
  at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:567) 
  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) 
  at org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:190) 
  at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:291) 
  at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:769) 
  at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:698) 
  at org.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:891) 
  at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:690) 
  at java.lang.Thread.run(Thread.java:595) 
 
Caused by: java.net.SocketException: Socket closed 
 
  at java.net.SocketInputStream.socketRead0(Native Method) 
  at java.net.SocketInputStream.read(SocketInputStream.java:129) 
  at org.postgresql.core.VisibleBufferedInputStream.readMore(VisibleBufferedInputStream.java:135) 
  at org.postgresql.core.VisibleBufferedInputStream.ensureBytes(VisibleBufferedInputStream.java:104) 
  at org.postgresql.core.VisibleBufferedInputStream.read(VisibleBufferedInputStream.java:73) 
  at org.postgresql.core.PGStream.ReceiveChar(PGStream.java:259) 
  at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1620) 
  at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257) 
    ... 22 more       

请您参考如下方法:

我在 PostgreSQL 8.4 中遇到了相同的异常,但我连接到同一主机上的本地数据库。原因是连接不再有效,所以我需要重新打开它。

有一个ticket在具有相关主题的 postgresql.org 上。他们的回答非常相似,只是捕获异常并重新打开连接


评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!