【问题标题】:How to resolve Too Many Connection Error in Hibernate如何解决 Hibernate 中的太多连接错误
【发布时间】:2012-03-02 15:58:57
【问题描述】:

我在我的 Web 应用程序中使用 struts2+hibernate3。它工作正常。有时显示无法打开连接。 Action 类 中用于连接休眠的以下连接语句。

protected SessionFactory getSessionFactory() {
    try {  

        Configuration cfg = new Configuration(); 
        cfg.configure("hibernate.cfg.xml"); 
        SessionFactory factory = cfg.buildSessionFactory(); 
        return factory; 

    } catch (Exception e) {
        log.error("sessionFactory", e);
        throw new IllegalStateException(
                "Could not locate SessionFactory");
    }
}
    public List viewAllPromotion() {
        System.out.println("finding student instance");
        try {
            Session session = sessionFactory.openSession();
            System.out.println("View All Student"); 
            session.beginTransaction(); 
            List results = session.createQuery("from   Student").list();  
            System.out.println("List got Rsults:"+results.size());

            session.close();  
            return results; 


        } catch (RuntimeException re) {  
            log.error("find by example failed", re);
            throw re;  
        }
    }

休眠配置文件

 <session-factory>
    <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/marksheet</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">admin</property>
    <property name="hibernate.search.autoregister_listeners">false</property>  
    <property name="hibernate.session_factory_name">MySessionFactory</property> 
    <property name="current_session_context_class">thread</property> 
    <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
    <property name="hibernate.connection.pool_size">10</property>

    <property name="show_sql">true</property>
    <mapping resource="com/rewardz/model/student.hbm.xml" />   
    <mapping resource="com/rewardz/model/course.hbm.xml" />              
    <mapping resource="com/rewardz/model/subject.hbm.xml" />  
    <mapping resource="com/rewardz/model/staff.hbm.xml" />  
    <mapping resource="com/rewardz/model/Role.hbm.xml" />  
    <mapping resource="com/rewardz/model/Privilege.hbm.xml" />  
    <mapping resource="com/rewardz/model/Logtab.hbm.xml" />  
</session-factory>

当我进行更多交易时,我收到以下错误消息。

   HTTP Status 500 - type Exception report

   message

        descriptionThe server encountered an internal error () that prevented it from fulfilling this request.

   exception

        org.hibernate.exception.JDBCConnectionException: Cannot open connection

注意异常的完整堆栈跟踪及其根本原因可在 GlassFish Server Open Source Edition 3.1.1 日志中找到。

谁能帮我解决这个问题?提前致谢。

【问题讨论】:

  • 您已经在您的代码中开始了事务但从未关闭它?
  • @Umesh Awasthi:我使用过 session.close() 方法。我可以知道如何关闭交易吗?
  • 类似tx.commit();
  • @UmeshAwasthi:对此还有一个疑问。你能告诉我关于 Session.close() 方法,是否是来自 MySQl 的关闭连接?
  • it 通过释放 JDBC 连接并清理来结束会话。更多详细信息请参阅文档。docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/…

标签: mysql struts2 hibernate3


【解决方案1】:

您应该发布堆栈跟踪。我猜它会告诉你更多关于哪里出了问题。

另外,尝试使用像 c3p0 或 bonecp 这样的连接池。不使用这些库之一时,您可能会遇到奇怪的连接问题,例如打开时间过长后连接超时等。

【讨论】:

  • 我无法清除 c3p0 或 bonecp。你能解释一下吗?
  • 连接池库将保持打开的许多连接,并在您需要连接时为您提供现有连接,这样您就无需打开新连接,这具有性能优势。他们还做了其他好事,比如在给你之前确保连接是打开的,你不能单独使用 Hibernate。我不确定它是否会解决您的问题,但无论如何,这是一件好事。 community.jboss.org/wiki/HowToConfigureTheC3P0ConnectionPool
  • 我错过了在所有方法中关闭我的交易,所以它显示了太多的连接错误。一定要阅读有关 boneCp/c3pO 的信息。非常感谢您在我的问题上所做的出色工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-02
  • 2021-01-23
  • 1970-01-01
  • 2013-11-12
  • 2010-11-15
  • 2017-10-29
  • 1970-01-01
相关资源
最近更新 更多