2013-05-28 74 views
5

我正在使用oracle 11g,hibernate 3和jsf2.I在was7上部署了我的應用程序。每件事情都很順利,但是當我嘗試登錄後5-6小時它是給我的錯誤java.sql.SQLException:Io異常:通過peer重置連接:套接字寫入錯誤

ERROR org.hibernate.util.JDBCExceptionReporter - Io exception: Connection reset by peer: socket write error 
[5/28/13 11:31:25:048 IST] 00000024 SystemErr  R org.hibernate.exception.GenericJDBCException: could not execute query 
    at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:126) 
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:114) 
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) 
    at org.hibernate.loader.Loader.doList(Loader.java:2231) 
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2125) 
    at org.hibernate.loader.Loader.list(Loader.java:2120) 
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401) 
    at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:361) 
    at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196) 
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1148) 
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102) 

我無法找出如何解決這個錯誤。請幫助我。提前感謝。

現在我已經通過代碼解決了這個問題,但我不知道它是否正確或不正確。是否建議我,我應該繼續解決這個問題。

public class ApplicationUtilityBean implements Serializable { 
private SessionFactory sessionFactory; 
private AnnotationConfiguration cfg; 
public String filePath; 
private String realPath = Config.PATH; 

public ApplicationUtilityBean() throws URISyntaxException { 
    getConnection(); 
} 

public void getConnection() { 
    URL r = this.getClass().getClassLoader().getResource("hibernate.cfg.xml"); 
      cfg = new AnnotationConfiguration(); 
    cfg.configure(r); 
    String pwd = cfg.getProperty("hibernate.connection.password"); 
    TripleDESEncryption tripledesenc = null; 
    try { 
     tripledesenc = new TripleDESEncryption(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    cfg.setProperty("hibernate.connection.password", 
      tripledesenc.decrypt(pwd)); 
    sessionFactory = cfg.buildSessionFactory(); 

    System.out.println("cfg: " + cfg); 
    System.out.println("sessionFactory: " + sessionFactory); 
} 

public Session getSession() { 
    System.out.println("Going to get session"); 
    Session session = null; 
    try { 
     System.out.println("cfg is: " + cfg); 
     System.out.println("sessionFactory: " + sessionFactory); 
     session = sessionFactory.openSession(); 
     if(session != null){ 
      try { 
       Transaction trxn = session.beginTransaction(); 
       Query queryResult = session.createSQLQuery("select * from dual"); 
       List<GTS_USER>listgtsuser = queryResult.list();  
           } catch (Exception e) {     
       e.printStackTrace(); 
       System.out.println("Creating new connection............"); 
       session.close(); 
       sessionFactory.close(); 
       getConnection(); 
       session = sessionFactory.openSession(); 
      } 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return session; 
} 

}

和我的休眠配置文件是

<hibernate-configuration> 
<session-factory> 

    <property name="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</property> 
    <property name="hibernate.cache.use_query_cache">true</property> 
    <property name="hibernate.cache.use_second_level_cache">true</property> 

    <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property> 
    <property name="hibernate.connection.url">jdbc:oracle:thin:@xxxxxxxxx:1521:HMS</property> 
    <property name="hibernate.connection.username">xxxxx</property> 
    <property name="hibernate.connection.password">xxxxxxxxxxx</property> 
    <property name="hibernate.connection.pool_size">10</property> 
    <property name="show_sql">true</property> 
    <property name="dialect">org.hibernate.dialect.OracleDialect</property> 
    <property name="hibernate.hbm2ddl.auto">update</property> 
+0

您是在hibernate配置文件中指定數據庫參數還是使用was7中定義的數據源並使用JNDI查找? – gkamal

+0

只在休眠配置中指定db參數 – RaviPancholi

回答

4

您可能正面臨數據庫連接超時。在每個數據庫中,當連接處於打開狀態並且某個時間段內沒有任何活動時會有超時。你需要一個連接池管理器。

如果您安裝了c3p0並正確配置它,它將允許休眠時保持連接的活動狀態和/或在需要時重新打開它。

確實這是針對MySQL和Hibernate的example,但它是一樣的。你必須包括c3p0.jar並添加到您的hibernade配置文件:

<property name="c3p0.min_size">5</property> 
<property name="c3p0.max_size">20</property> 
<property name="c3p0.timeout">1800</property> 
<property name="c3p0.max_statements">50</property> 
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> 

請務必根據你的數據庫配置c3p0.timeout

+0

我嘗試過,但沒有工作。是否還有任何代碼級別更改? – RaviPancholi

+0

對不起,犯了一個錯誤,這應該現在工作! –

+0

現在我已測試它工作正常。感謝您的幫助。 – RaviPancholi

0

這裏的問題是,空閒連接將被數據庫服務器被關閉。應用程序不會了解它並嘗試使用一個陳舊的連接(從初始化期間創建的池中)。解決這個問題的方法是在使用池中的連接之前配置連接池以進行活動測試(通常通過發起簡單的選擇查詢)。

如何做到這一點因您如何設置數據源/連接池而異。如果您提供更多詳細信息,我可以提供更具體的說明。

+0

我剛剛使用了hibernate的defoult連接池3.我在hibernate.cfg.xml中設置了proprty。 – RaviPancholi

1

根據@BalusC

如果您的應用程序應該運行相當長的時間和相當頻繁連接數據庫,然後再考慮使用連接池,以提高連接性能。如果您的應用程序是一個Web應用程序,那麼請查看應用程序服務器的文檔,它通常會提供一個具有DataSource特性的連接池設施。如果它是一個客戶端應用程序,那麼尋找經過多年驗證其穩健性的第三方連接池庫,如Apache Commons DBCP(常用的,用於批處理appservers),C3P0(從Hibernate中已知)和Proxool(如果您想要XA連接)。

相關問題