2012-02-10 46 views
1

我在我的web應用程序中使用struts2 + hibernate3。它工作正常。有時顯示無法打開連接。用於連接休眠的Follwoing連接語句動作類如何解決休眠中的太多連接錯誤

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; 
     } 
    } 

Hibernate配置文件

<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開源版3.1.1日誌可用。

任何人都可以幫我解決這個問題嗎?提前致謝。

+0

你已經開始在你的代碼中進行transcation但從未關閉它? – 2012-02-10 06:20:44

+0

@ Umesh Awasthi:我使用了session.close()方法。我可以知道如何結束交易嗎? – shiva 2012-02-14 07:39:14

+0

類似'tx.commit();' – 2012-02-14 07:43:09

回答

0

您應該發佈堆棧跟蹤。我猜它告訴你更多關於什麼是錯的。

此外,請嘗試使用連接池,如c3p0或bonecp。你可能會遇到奇怪的連接問題,當不使用這些庫之一,例如連接超時打開後超時等。

+0

我無法清除c3p0或bonecp。你能解釋一下嗎?? – shiva 2012-02-14 07:39:50

+0

連接池庫將保持打開多個連接,並在需要連接時爲您提供現有連接,這種連接方式不需要打開新連接,因此具有性能優勢。他們還會做其他很好的事情,比如確保連接在提供給您之前已經打開,而您無法單獨使用Hibernate。我不確定它是否能解決您的問題,但無論如何,這是一件好事。 https://community.jboss.org/wiki/HowToConfigureTheC3P0ConnectionPool – 2012-02-14 16:33:36

+0

我錯過了在所有方法中關閉我的trasaction,所以它顯示我的連接錯誤太多。一定要閱讀關於boneCp/c3pO。非常感謝您在我的問題上的出色努力。 – shiva 2012-02-15 08:25:48