简体   繁体   中英

Hibernate Session creation exception after server idle

I am using a simple java application for manipulating the table records from browser. I have used hibernate for making DAO calls. The application works fine on normal usage. However when the application is idle for 1 to 2 days the following exception occurs.

Caused by: java.sql.SQLException: IO Error: Connection reset

After using c3p0 the dle time extended up to 10 days. But I want the application to be running up all the time.

I have been using Linux sever. JDK 1.5. Tomcat server 6.

My Hibernate config,

jdbc:oracle:thin:@something.com:1789:patentmd oracle.jdbc.driver.OracleDriver

    <property name="hibernate.connection.username">something</property>
      <property name="hibernate.connection.password">something</property>

    <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
    <property name="hibernate.show_sql">true</property>

 <!--    <property name="hibernate.current_session_context_class">thread</property>  -->

  <property name="c3p0.acquire_increment">1</property> 
    <property name="c3p0.idle_test_period">30</property> 
    <property name="c3p0.max_size">5</property> 
    <property name="c3p0.min_size">2</property>
    <property name="c3p0.max_statements">5</property> 
    <property name="c3p0.timeout">180</property>


     <!--  Mapping Resources. One entry should exist for each table  -->
    <mapping resource="com/hibernate/pojo/clientgrp/ClientGroup.hbm.xml"/>
       <mapping resource="com/hibernate/pojo/applepub/Apple_Pub.hbm.xml"/>
        <mapping resource="com/hibernate/pojo/prspub/Prs_Pub.hbm.xml"/>

</session-factory>

The full stack trace,

$$$$$$$$$$ sessionfactory is not null
Connection : true
Hibernate: select clientgrou0_.MGR_PRS_ID as MGR1_0_, clientgrou0_.MGR_PRS_NAME as MGR2_0_, clientgrou0_.MGR_CLIENT_GROUP as
 MGR3_0_, clientgrou0_.UPDATE_DT as UPDATE4_0_, clientgrou0_.CREATE_DT as CREATE5_0_, clientgrou0_.DEPT_NM as DEPT6_0_ from 
CLIENTGROUPS clientgrou0_
############ ClientGroupDAO.getAll()  , hibernate exception org.hibernate.exception.JDBCConnectionException: could not execu
te query
ClientGroupTableHandler.getAllRecords() $$$$$$$$ exception occured  6org.hibernate.HibernateException: org.hibernate.excepti
on.JDBCConnectionException: could not execute query
ClientGroupTableHandler.Main() $$$$$$$$ exception occured  1java.lang.Exception: org.hibernate.HibernateException: org.hiber
nate.exception.JDBCConnectionException: could not execute query
java.lang.Exception: org.hibernate.HibernateException: org.hibernate.exception.JDBCConnectionException: could not execute qu
ery
        at com.web.servlets.ClientGroupTableHandler.getAllRecords(ClientGroupTableHandler.java:294)
        at com.web.servlets.ClientGroupTableHandler.Main(ClientGroupTableHandler.java:76)
        at com.web.servlets.ClientGroupTableHandler.doPost(ClientGroupTableHandler.java:56)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
        at java.lang.Thread.run(Thread.java:619)
Caused by: org.hibernate.HibernateException: org.hibernate.exception.JDBCConnectionException: could not execute query
        at com.hibernate.dao.ClientGroupDAO.getAll(ClientGroupDAO.java:76)
        at com.web.servlets.ClientGroupTableHandler.getAllRecords(ClientGroupTableHandler.java:288)
        ... 16 more
Caused by: org.hibernate.exception.JDBCConnectionException: could not execute query
        at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:99)
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
        at org.hibernate.loader.Loader.doList(Loader.java:2536)
        at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276)
        at org.hibernate.loader.Loader.list(Loader.java:2271)
        at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:452)
        at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
        at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
        at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1268)
        at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
        at com.hibernate.dao.ClientGroupDAO.getAll(ClientGroupDAO.java:45)
        ... 17 more
Caused by: java.sql.SQLException: IO Error: Connection reset
        at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:876)
        at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1153)
        at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1275)
        at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3576)
        at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3620)
        at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1203)
        at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
        at org.hibernate.loader.Loader.getResultSet(Loader.java:1953)
        at org.hibernate.loader.Loader.doQuery(Loader.java:802)
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
        at org.hibernate.loader.Loader.doList(Loader.java:2533)
        ... 25 more
Caused by: java.net.SocketException: Connection reset

  at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:96)
        at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
        at oracle.net.ns.DataPacket.send(DataPacket.java:199)
        at oracle.net.ns.NetOutputStream.flush(NetOutputStream.java:211)
        at oracle.net.ns.NetInputStream.getNextPacket(NetInputStream.java:227)
        at oracle.net.ns.NetInputStream.read(NetInputStream.java:175)
        at oracle.net.ns.NetInputStream.read(NetInputStream.java:100)
        at oracle.net.ns.NetInputStream.read(NetInputStream.java:85)
        at oracle.jdbc.driver.T4CSocketInputStreamWrapper.readNextPacket(T4CSocketInputStreamWrapper.java:123)
        at oracle.jdbc.driver.T4CSocketInputStreamWrapper.read(T4CSocketInputStreamWrapper.java:79)
        at oracle.jdbc.driver.T4CMAREngine.unmarshalUB1(T4CMAREngine.java:1122)
        at oracle.jdbc.driver.T4CMAREngine.unmarshalSB1(T4CMAREngine.java:1099)
        at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:288)
        at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:191)
        at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:523)
        at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:207)
        at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:863)
        ... 35 more

Blockquote

Add this property in your hibernate config file. I hope it ll work.

<property name="connection.provider_class">
      org.hibernate.connection.C3P0ConnectionProvider
    </property>

Looks strange, are you holding on to a stale Hibernate Session in some way? You should always get a fresh Hibernate Session and release it afterwards.

The underlying connection is stale.

The normal course of action is either to get the connection pool to test the connection before you use it.

For C3P0:

 c3p0.testConnectionOnCheckout=true
 c3p0.preferredTestQuery="select 1 from dual"

http://community.jboss.org/wiki/HowToConfigureTheC3P0ConnectionPool

The other option is to set the min number of connections to 0, and make sure the pool is configured to evict unused connections before the database connection timeout is reached.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM