java.sql.SQLException:Ioexception:由peer重置连接:套接字写入错误

我正在使用oracle 11g,hibernate 3和jsf2.I在was7.上部署了我的应用程序。每件事情都顺利但是当我尝试在5-6小时后登录时它会给我错误

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"); Listlistgtsuser = 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配置文件是

   net.sf.ehcache.hibernate.EhCacheRegionFactory true true oracle.jdbc.OracleDriver jdbc:oracle:thin:@xxxxxxxxx:1521:HMS xxxxx xxxxxxxxxxx 10 true org.hibernate.dialect.OracleDialect update 

您可能正面临数据库连接超时。 在每个数据库上,当连接打开且某段时间内没有活动时会出现超时。 您需要一个连接池管理器。

如果您安装c3p0并正确配置它将允许hibernate保持您的连接活动和/或在您需要时重新打开它。

这是MySQL和Hibernate的一个例子 ,但它是一样的。 您必须包含c3p0.jar并将其添加到您的hibernade配置文件中:

 5 20 1800 50 org.hibernate.connection.C3P0ConnectionProvider 

确保根据您的数据库配置c3p0.timeout

根据@BalusC

如果您的应用程序应该运行相对较长的时间并且经常连接数据库,那么请考虑使用连接池来提高连接性能。 如果您的应用程序是Web应用程序,那么请查看appserver的文档,它通常提供了一个DataSource风格的连接池设施。 如果它是一个客户端应用程序,那么寻找第三方连接池库,这些库已经certificate了它们多年来的健壮性,例如Apache Commons DBCP(常用于批量应用服务器),C3P0(来自Hibernate)和Proxool(如果你想要的话) XA连接)。

这里的问题是数据库服务器将关闭空闲连接。 应用程序没有了解它并尝试使用过时的连接(从初始化期间创建的池)。 解决此问题的方法是在使用池中的连接之前,将连接池配置为对活动进行测试(通常通过触发简单的选择查询)。

如何执行此操作会因您设置数据源/连接池的方式而异。 如果您提供更多详细信息,我可以提供更具体的说明。