2011-10-04 100 views
2

首先,在發佈此問題之前,我已閱讀與此錯誤有關的帖子。但是,他們大多數都沒有使用PostgreSQL,所以這個錯誤可能會以不同的方式處理。下面是這個錯誤的堆棧跟蹤:使用PostgreSQL破壞管道

WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 0, SQLState: 08006 
4450102501 [XmlBlaster.socket_ssl.cbWorkerThread] ERROR org.hibernate.util.JDBCExceptionReporter - An I/O error occured while sending to the backend. 
java.lang.RuntimeException: org.hibernate.exception.JDBCConnectionException: could not execute query using iterate 
    at edu.umd.cattlab.vatraffic.listener.output.VaTrafficDbModule.processSingleMessage(VaTrafficDbModule.java:380) 
    at edu.umd.cattlab.vatraffic.listener.output.VaTrafficDbModule.processMessage(VaTrafficDbModule.java:266) 
    at edu.umd.cattlab.vatraffic.listener.input.InputModule.sendMessageToOuputModules(InputModule.java:36) 
    at edu.umd.cattlab.vatraffic.listener.input.XmlBlaster.update(XmlBlaster.java:173) 
    at org.xmlBlaster.client.XmlBlasterAccess.update(XmlBlasterAccess.java:1043) 
    at org.xmlBlaster.client.protocol.AbstractCallbackExtended.update(AbstractCallbackExtended.java:111) 
    at org.xmlBlaster.client.protocol.AbstractCallbackExtended.update(AbstractCallbackExtended.java:199) 
    at org.xmlBlaster.util.protocol.RequestReplyExecutor.receiveReply(RequestReplyExecutor.java:444) 
    at org.xmlBlaster.client.protocol.socket.WorkerThread.run(WorkerThread.java:51) 
Caused by: org.hibernate.exception.JDBCConnectionException: could not execute query using iterate 
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:74) 
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) 
    at org.hibernate.loader.hql.QueryLoader.iterate(QueryLoader.java:427) 
    at org.hibernate.hql.ast.QueryTranslatorImpl.iterate(QueryTranslatorImpl.java:380) 
    at org.hibernate.engine.query.HQLQueryPlan.performIterate(HQLQueryPlan.java:224) 
    at org.hibernate.impl.SessionImpl.iterate(SessionImpl.java:1192) 
    at org.hibernate.impl.QueryImpl.iterate(QueryImpl.java:46) 
    at edu.umd.cattlab.schema.hibernate.cattXML.extensions.VaTraffic.VaTrafficHelper.laneMap(VaTrafficHelper.java:165) 
    at edu.umd.cattlab.vatraffic.listener.output.VaTrafficDbModule.processIncidentDescription(VaTrafficDbModule.java:396) 
    at edu.umd.cattlab.vatraffic.listener.output.VaTrafficDbModule.processSingleMessage(VaTrafficDbModule.java:360) 
    ... 8 more 
Caused by: org.postgresql.util.PSQLException: An I/O error occured while sending to the backend. 
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:218) 
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:451) 
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:350) 
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:254) 
    at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:186) 
    at org.hibernate.loader.Loader.getResultSet(Loader.java:1787) 
    at org.hibernate.loader.hql.QueryLoader.iterate(QueryLoader.java:404) 
    ... 15 more 
Caused by: java.net.SocketException: Broken pipe 
    at java.net.SocketOutputStream.socketWrite0(Native Method) 
    at java.net.SocketOutputStream.socketWrite(Unknown Source) 
    at java.net.SocketOutputStream.write(Unknown Source) 
    at java.io.BufferedOutputStream.flushBuffer(Unknown Source) 
    at java.io.BufferedOutputStream.write(Unknown Source) 
    at org.postgresql.core.PGStream.SendChar(PGStream.java:174) 
    at org.postgresql.core.v3.QueryExecutorImpl.sendBind(QueryExecutorImpl.java:829) 
    at org.postgresql.core.v3.QueryExecutorImpl.sendOneQuery(QueryExecutorImpl.java:1053) 
    at org.postgresql.core.v3.QueryExecutorImpl.sendQueryPreamble(QueryExecutorImpl.java:373) 
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:189) 

我們的應用程序使用Hibernate,所以這裏的Hibernate配置:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-configuration PUBLIC 
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 

<hibernate-configuration> 

    <session-factory> 

    <!-- Database connection settings --> 
    <property name="connection.driver_class">org.postgresql.Driver</property> 
    <property name="connection.url">jdbc:postgresql://cannottell.umd.edu</property> 
    <property name="connection.username"></property> 
    <property name="connection.password"></property> 

    <!-- JDBC connection pool (use the built-in) --> 
    <property name="connection.pool_size">1</property> 

    <!-- Enable C3P0 database connection pool --> 
    <!----> <!--<property name="c3p0.min_size">2</property> 
    <property name="c3p0.max_size">5</property> 
    <property name="c3p0.timeout">600</property> 
    <property name="c3p0.max_statements">0</property> 
    <property name="hibernate.c3p0.idle_test_period">300</property> 
    <property name="hibernate.c3p0.acquire_increment">1</property>--> 

    <!-- SQL dialect --> 
    <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property> 

    <!-- Oracle doesn't handle prepared statement caching very well. This disables prepared statements. --> 
    <property name="statement_cache.size">0</property> 

    <!-- Enable Hibernate's automatic session context management --> 
    <property name="current_session_context_class">thread</property> 

    <!-- Disable the second-level cache --> 
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> 

    <!-- Echo all executed SQL to stdout --> 
    <property name="show_sql">false</property> 

    <!-- Drop and re-create the database schema on startup --> 
    <property name="hbm2ddl.auto">false</property> 

    <!-- ritis schema mappings --> 
    <mapping resource="edu/umd/cattlab/schema/hibernate/cattXML/cattXML.hbm.xml"/> 
    <mapping resource="edu/umd/cattlab/schema/hibernate/cattXML/cattXMLLookup.hbm.xml"/> 

    <!-- vatraffic schema mappings --> 
    <mapping resource="edu/umd/cattlab/schema/hibernate/cattXML/extensions/VaTraffic/vaTraffic.hbm.xml"/> 
    <mapping resource="edu/umd/cattlab/schema/hibernate/cattXML/extensions/VaTraffic/vaTrafficLookup.hbm.xml"/> 

</session-factory> 

</hibernate-configuration> 

,這裏是我的問題:

  1. 這是錯誤真的來自丟失了與數據庫的連接,還是來自我的代碼的錯誤? (如果後者是真的,那麼我會提供代碼)
  2. 無論哪種方式,我該如何處理這個錯誤?
  3. 如果錯誤來自數據庫,如果我沒有直接訪問數據庫的情況,如何複製此錯誤(用於測試目的)?

如果我問的問題太多,我很抱歉。順便說一下,如果有必要,我想添加更多關於此問題的信息。我非常感謝你們的迴應。謝謝。

ADDED

下面是開放代碼和關閉Hibernate的Session:

Session sess = HibernateUtil.getSessionFactory().openSession(); 

    Transaction tx = null; 
    String eventId = null; 

    try { 

     sess.getTransaction().setTimeout(new Integer(configuration 
         .getProperty("messageProcessingTimeout"))); 

     if (!sess.isConnected()) { 
      attemptReconnect(); 
     } else { 
      log.debug("Database connection open."); 
     } 

     //tx = sess.beginTransaction(); 
     //log.debug("After transaction"); 
     eventId = processIncidentDescription(message, sess); 

     //tx.commit(); 

     // Sending jmsNotify. 
     if (Boolean.valueOf(configuration.getProperty("sendJmsNotify")) 
       && eventId != null) { 
      log.debug("Sending jmsNotify message " + "<notify id=\"" 
        + eventId + "\"/>"); 
      jmsSender.sendMessage("<notify id=\"" + eventId + "\"/>"); 
     } 
    } catch (Exception e) { 

     try { 
      if (tx != null) 
       tx.rollback(); 
     } catch (TransactionException te) { 
      log.warn("Unable to roll back transaction."); 
     } 

     throw new RuntimeException(e); 
    } finally { 
     sess.close(); 
    } 

ADDED

下面是可能會導致錯誤的代碼:

public static HashMap<String,HashMap<String,Object>> laneMap(Session sess){ 
    HashMap<String,HashMap<String,Object>> table = new HashMap<String,HashMap<String,Object>>(); 
    Query query = sess.createQuery("from Direction"); 
    Iterator<Object> iterate = query.iterate(); 
    while (iterate.hasNext()){ 
     Object obj = iterate.next(); 
     Direction direct = (Direction) obj; 
     if (table.get("direction") == null){ 
      table.put("direction", new HashMap<String,Object>()); 
     } 
     String directDesc = direct.getDirectionDescription(); 
     table.get("direction").put(directDesc, direct); 
    } 
    Query query2 = sess.createQuery("from LaneStatus"); 
    Iterator<Object> iterate2 = query2.iterate(); 
    while (iterate2.hasNext()){ 
     Object obj = iterate2.next(); 
     LaneStatus laneStatus = (LaneStatus) obj; 
     if (table.get("lane_status") == null){ 
      table.put("lane_status", new HashMap<String,Object>()); 
     } 
     String directDesc = laneStatus.getLaneStatusDescription(); 
     table.get("lane_status").put(directDesc, laneStatus); 
    } 
    Query query3 = sess.createQuery("from LaneType"); 
    Iterator<Object> iterate3 = query3.iterate(); 
    while (iterate3.hasNext()){ 
     Object obj = iterate3.next(); 
     LaneType laneType = (LaneType) obj; 
     if (table.get("lane_type") == null){ 
      table.put("lane_type", new HashMap<String,Object>()); 
     } 
     String directDesc = laneType.getLaneTypeDescription(); 
     table.get("lane_type").put(directDesc, laneType); 
    } 
    return table; 
} 
+0

你可以請發佈相關的休眠代碼? –

+0

@ danny.lesnik哪一個具體? – vandershraaf

+0

打開/關閉Hibernate會話和事務。 –

回答

4

當您的應用程序無法再連接到Postgres數據庫時,會發生此問題。當我將VPN連接到我的辦公室時,發生了這種情況,然後關閉了VPN連接,從而切斷了數據庫連接。我的本地服務器將顯示上述內容。如果在您的服務器環境中發生此問題,則需要查看是否存在網絡連接問題。

+0

這是否意味着我無法控制那裏發生的事情?我已閱讀關於MySQL的auto_connect屬性,以便在發生此類錯誤時可以重新連接。我不確定postgres是否具有相同的功能。 – vandershraaf

+0

你最近怎麼連接?如果你使用的是連接池,那麼這個問題應該只發生在網絡出現故障,或者數據庫崩潰時,或者這些線路上發生的事情。 – atrain

+0

嗯,我不認爲DB崩潰了。基本上我只需要重新啓動應用程序與數據庫就好了。可能來自我的代碼? – vandershraaf

1

莫非應用程序試圖打開更多連接到PostgreSQL sim在您的PG實例中配置的最大併發連接數是否超過?

+2

這將導致一個不同的錯誤消息(表明太多連接打開)。 –

+0

@甘道夫這是有道理的,但是,正如a_horse_with_no_name指出的那樣,那將會是不同的錯誤。你有什麼證明你的說法嗎? – vandershraaf