已经有太多连接

发布于 2024-10-20 14:27:12 字数 5820 浏览 2 评论 0原文

我正在使用 hibernate 连接到远程 postgres(版本 9.0)数据库。 收到以下错误

2011-03-08 06:48:25,695 ERROR [org.hibernate.util.JDBCExceptionReporter] (http-69.89.2.245-8080-9) FATAL: sorry, too many clients already
2011-03-08 06:48:25,695 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/JBOSSINI].[com.java.misc.feedback]] (http-69.89.2.245-8080-9) Servlet.service() for servlet com.java.misc.feedback threw exception: java.lang.NullPointerException
    at com.java.model.HiberEx.InsertsimpleReport(HiberEx.java:95) [:]
    at com.java.misc.feedback.doGet(feedback.java:87) [:]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:734) [:1.0.0.Final]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [:1.0.0.Final]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:324) [:6.0.0.Final]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242) [:6.0.0.Final]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [:6.0.0.Final]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) [:6.0.0.Final]
    at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:181) [:6.0.0.Final]
    at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.event(CatalinaContext.java:285) [:1.1.0.Final]
    at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.invoke(CatalinaContext.java:261) [:1.1.0.Final]
    at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:88) [:6.0.0.Final]
    at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:100) [:6.0.0.Final]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) [:6.0.0.Final]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [:6.0.0.Final]
    at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158) [:6.0.0.Final]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [:6.0.0.Final]
    at org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.invoke(ActiveRequestResponseCacheValve.java:53) [:6.0.0.Final]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362) [:6.0.0.Final]
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [:6.0.0.Final]
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:654) [:6.0.0.Final]
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:951) [:6.0.0.Final]
    at java.lang.Thread.run(Thread.java:619) [:1.6.0_21]

我的代码

public void InsertfailReport(String destination, 
                String source,
                String status,
                String timedate, String smsc,
                String failreason,
                String smsid,
                String message){
                SessionFactory sessionFactory = new Configuration().configure("com/java/hibernate.cfg.xml").buildSessionFactory();
                Session session = sessionFactory.openSession();
                Transaction transaction = null;

                try {
                    transaction = session.beginTransaction();
                    Failreport sobj=new Failreport();
                    sobj.setDestination(destination);
                    sobj.setFailreason(failreason);
                    sobj.setMessage(message);
                    sobj.setSmsc(smsc);
                    sobj.setSmsid(smsid);
                    sobj.setSource(source);
                    sobj.setStatus(status);
                    sobj.setTimedate(timedate);
                    session.save(sobj);
                    transaction.commit();
                } catch (HibernateException e) {
                    transaction.rollback();
                    e.printStackTrace();
                } finally {
                    session.close();
                }
        }

Hibernate.cfg.xml

<?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 name="MyFactory">
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.password">xxxx</property>
        <property name="hibernate.connection.url">jdbc:postgresql://xx.xx.xx.xx:5432/smslog</property>
        <property name="hibernate.connection.username">xxxxx</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="hibernate.validator.apply_to_ddl">false</property>
        <property name="hibernate.validator.autoregister_listeners">false</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.jdbc.batch_size">0</property>
          <mapping resource="com/java/model/Kannel.hbm.xml"/>
          <mapping resource="com/java/model/Smsc.hbm.xml"/>
          <mapping resource="com/java/model/Dlr.hbm.xml"/>
          <mapping resource="com/java/model/Routesmsc.hbm.xml"/>
          <mapping resource="com/java/model/Simplereport.hbm.xml"/>
          <mapping resource="com/java/model/Failreport.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

I am connecting to a remote postgres(version 9.0) database using hibernate. i get following error

2011-03-08 06:48:25,695 ERROR [org.hibernate.util.JDBCExceptionReporter] (http-69.89.2.245-8080-9) FATAL: sorry, too many clients already
2011-03-08 06:48:25,695 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/JBOSSINI].[com.java.misc.feedback]] (http-69.89.2.245-8080-9) Servlet.service() for servlet com.java.misc.feedback threw exception: java.lang.NullPointerException
    at com.java.model.HiberEx.InsertsimpleReport(HiberEx.java:95) [:]
    at com.java.misc.feedback.doGet(feedback.java:87) [:]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:734) [:1.0.0.Final]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [:1.0.0.Final]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:324) [:6.0.0.Final]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242) [:6.0.0.Final]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [:6.0.0.Final]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) [:6.0.0.Final]
    at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:181) [:6.0.0.Final]
    at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.event(CatalinaContext.java:285) [:1.1.0.Final]
    at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.invoke(CatalinaContext.java:261) [:1.1.0.Final]
    at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:88) [:6.0.0.Final]
    at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:100) [:6.0.0.Final]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) [:6.0.0.Final]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [:6.0.0.Final]
    at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158) [:6.0.0.Final]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [:6.0.0.Final]
    at org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.invoke(ActiveRequestResponseCacheValve.java:53) [:6.0.0.Final]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362) [:6.0.0.Final]
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [:6.0.0.Final]
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:654) [:6.0.0.Final]
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:951) [:6.0.0.Final]
    at java.lang.Thread.run(Thread.java:619) [:1.6.0_21]

this my code

public void InsertfailReport(String destination, 
                String source,
                String status,
                String timedate, String smsc,
                String failreason,
                String smsid,
                String message){
                SessionFactory sessionFactory = new Configuration().configure("com/java/hibernate.cfg.xml").buildSessionFactory();
                Session session = sessionFactory.openSession();
                Transaction transaction = null;

                try {
                    transaction = session.beginTransaction();
                    Failreport sobj=new Failreport();
                    sobj.setDestination(destination);
                    sobj.setFailreason(failreason);
                    sobj.setMessage(message);
                    sobj.setSmsc(smsc);
                    sobj.setSmsid(smsid);
                    sobj.setSource(source);
                    sobj.setStatus(status);
                    sobj.setTimedate(timedate);
                    session.save(sobj);
                    transaction.commit();
                } catch (HibernateException e) {
                    transaction.rollback();
                    e.printStackTrace();
                } finally {
                    session.close();
                }
        }

Hibernate.cfg.xml

<?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 name="MyFactory">
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.password">xxxx</property>
        <property name="hibernate.connection.url">jdbc:postgresql://xx.xx.xx.xx:5432/smslog</property>
        <property name="hibernate.connection.username">xxxxx</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="hibernate.validator.apply_to_ddl">false</property>
        <property name="hibernate.validator.autoregister_listeners">false</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.jdbc.batch_size">0</property>
          <mapping resource="com/java/model/Kannel.hbm.xml"/>
          <mapping resource="com/java/model/Smsc.hbm.xml"/>
          <mapping resource="com/java/model/Dlr.hbm.xml"/>
          <mapping resource="com/java/model/Routesmsc.hbm.xml"/>
          <mapping resource="com/java/model/Simplereport.hbm.xml"/>
          <mapping resource="com/java/model/Failreport.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白云悠悠 2024-10-27 14:27:12

您超出了为 PostgreSQL 服务器配置的最大连接数。

因此,您的服务器配置不正确,导致连接不足,或者您没有正确关闭连接。

如果您绝对确定要关闭连接,我强烈建议您使用连接池来限制并发会话的最大数量。

或者,您可以更改 PostgreSQL 配置并增加允许的连接数。有关详细信息,请参阅手册:

http://www.postgresql.org/docs/current/static/runtime-config-connection.html#RUNTIME-CONFIG-CONNECTION-SETTINGS

You are exceeding the number of maximum connections configured for your PostgreSQL server.

So you either have your server configured incorrectly to allow not enough connections or you are not closing your connections properly.

If you are absolutely sure you are closing your connections, I would strongly suggest you use a connection pool to limit the maximum number of concurrent sessions.

Alternatively you can change the PostgreSQL configuration and increase the number of allowed connections. See the manual for details:

http://www.postgresql.org/docs/current/static/runtime-config-connection.html#RUNTIME-CONFIG-CONNECTION-SETTINGS

爱给你人给你 2024-10-27 14:27:12

您在方法调用中创建一个新的会话工厂,这几乎肯定是一个坏主意 - 会话工厂应该被视为全局共享资源,就像数据源一样。更不用说,创建会话工厂需要相当长的时间。

由于您没有关闭会话工厂,因此在会话工厂本身被 GC 之前,其内部连接池实际上不会被清除。通常,您在应用程序启动/关闭期间创建和关闭会话工厂(例如InitializingBean/DisposableBean、ServletContextEventListener、ServiceMBean)

You're creating a new session factory inside the method call which is almost definitely a bad idea- session factories should be considered global shared resources, just like datasources. Not to mention, creating a session factory takes considerable time.

Since you're not closing the session factory, its internal connection pool won't actually be cleared until the session factory itself is GC'd. Typically you create and close session factories during application startup/shutdown (e.g. InitializingBean/DisposableBean, ServletContextEventListener, ServiceMBean)

灵芸 2024-10-27 14:27:12

增加

/var/lib/pgsql/data/postgresql.conf中的

max_connection和shared_buffers更改

max_connections = 100
shared_buffers = 24MB

max_connections = 300
shared_buffers = 80MB

并重新启动PostgreSQL数据库。

Increase max_connection and shared_buffers

in /var/lib/pgsql/data/postgresql.conf

change

max_connections = 100
shared_buffers = 24MB

to

max_connections = 300
shared_buffers = 80MB

and restart PostgreSQL database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文