Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用PostgreSQL断管_Java_Hibernate_Postgresql - Fatal编程技术网

Java 使用PostgreSQL断管

Java 使用PostgreSQL断管,java,hibernate,postgresql,Java,Hibernate,Postgresql,首先,在发布这个问题之前,我已经阅读了关于这个错误的帖子。但是,它们中的大多数都没有使用PostgreSQL,因此可能会以不同的方式处理此错误。以下是此错误的堆栈跟踪: WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 0, SQLState: 08006 4450102501 [XmlBlaster.socket_ssl.cbWorkerThread] ERROR org.hibernate.util.JDBCE

首先,在发布这个问题之前,我已经阅读了关于这个错误的帖子。但是,它们中的大多数都没有使用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>

org.postgresql.Driver
jdbc:postgresql://cannottell.umd.edu
1.
org.hibernate.dialogue.PostgreSqlDialogue
0
线
org.hibernate.cache.NoCacheProvider
假的
假的
以下是我的问题:

  • 这个错误真的是因为与数据库的连接丢失,还是因为我的代码出错?(如果后者为真,则我将提供相关代码)
  • 不管怎样,我应该如何处理这个错误
  • 如果错误来自数据库,如果我没有直接访问数据库的权限,如何复制此错误(用于测试目的)
  • 如果我问的问题太多,我很抱歉。顺便说一句,如果必要的话,我想补充更多关于这个问题的信息。我真的很感谢你们的回复。多谢各位

    增加

    以下是打开和关闭hibernate会话的代码:

    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();
        }
    
    会话sess=HibernateUtil.getSessionFactory().openSession(); 事务tx=null; 字符串eventId=null; 试一试{ sess.getTransaction().setTimeout(新整数(配置 .getProperty(“messageProcessingTimeout”); 如果(!sess.isConnected()){ 尝试重新连接(); }否则{ 调试(“数据库连接打开”); } //tx=sess.beginTransaction(); //log.debug(“交易后”); eventId=processIncidentDescription(消息,sess); //tx.commit(); //发送jmsNotify。 if(Boolean.valueOf(configuration.getProperty(“sendJmsNotify”)) &&eventId!=null){ 调试(“发送jmsNotify消息“+”); jmsSender.sendMessage(“”); } }捕获(例外e){ 试一试{ 如果(tx!=null) tx.回滚(); }捕获(TransactionException te){ log.warn(“无法回滚事务”); } 抛出新的运行时异常(e); }最后{ sess.close(); } 增加

    以下是可能导致错误的代码:

    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;
    }
    
    公共静态HashMap laneMap(会话sess){
    HashMap table=新的HashMap();
    Query Query=sess.createQuery(“从方向”);
    Iterator iterate=query.iterate();
    while(iterate.hasNext()){
    Object obj=iterate.next();
    方向直接=(方向)obj;
    if(table.get(“direction”)==null){
    table.put(“direction”,newhashmap());
    }
    字符串directDesc=direct.getDirectionDescription();
    表.get(“direction”).put(directDesc,direct);
    }
    Query query2=sess.createQuery(“来自LaneStatus”);
    迭代器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());
    }
    字符串directDesc=laneStatus.getLaneStatusDescription();
    表格get(“车道状态”).put(directDesc,laneStatus);
    }
    queryquery3=sess.createQuery(“来自LaneType”);
    迭代器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());
    }
    字符串directDesc=laneType.getLaneTypeDescription();
    table.get(“lane_type”).put(directDesc,laneType);
    }
    返回表;
    }
    
    是否会出现这样的情况,即您的应用程序尝试同时打开更多到PostgreSQL的连接,而不是PG实例中配置的最大并发连接数?

    当您的应用程序无法再连接到Postgres DB时,会发生此问题。我曾经遇到过这样的情况,当我被VPN连接到我的办公室,然后关闭VPN连接,这会切断DB连接。然后,我的本地服务器将显示上述内容。如果此问题发生在您的服务器环境中,您需要查看是否存在网络连接问题。

    打开/关闭hibernate会话和事务时,请发布相关的hibernate代码?@danny.lesnik具体是哪一个?我在最新编辑中添加了一些代码。这将导致不同的错误消息(表示打开的连接太多).@Gandalf这是有道理的,但是是的,正如一个没有名字的马所指出的,那将是不同的错误。你有什么可以证明你的说法吗?这是否意味着我对那里发生的事情没有任何控制权?我读到MySQL有自动连接属性,这样每当发生这种错误时它就可以重新连接。我不确定postgres也有同样的功能。你是如何连接的?如果你使用的是连接池,那么这个问题应该只有在网络出现故障,或者DB崩溃,或者类似的情况下才会发生。好吧,我不认为DB崩溃。基本上我只需要用DB重新启动应用程序就可以了。可能是我的问题代码?很抱歉,但我不太理解您的问题。我可以说的是,连接不是由内部Hibernate池以外的池池进行池化的。(它也没有使用c3p0,因为此应用程序已经在生产服务器中)如果是这种情况,您可能需要检查可能会切断Postgres连接的网络漏洞。当我在本地工作时,管道中断,我需要做的就是重新启动,但对于生产服务器来说,这显然是不可接受的。您可能会