samedi 4 juillet 2015

Hibernate in Web app does not function after idled 8 hours [duplicate]

This question already has an answer here:

My web app function as expected but it won't after it idle 8 hours. We use Servlet, JSP, Hibernate technologies in our web app. After 8 hours, it shows an error page, it looks like below

    HTTP Status 500 - JDBC rollback failed

      type Exception report

       message JDBC rollback failed

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.hibernate.TransactionException: JDBC rollback failed
    org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:170)
    vn.com.fsoft.dao.CustomerDAO.addCustomer(CustomerDAO.java:45)
    vn.com.fsoft.RegistrationServlet.doPost(RegistrationServlet.java:43)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    vn.com.fsoft.LanguageFilter.doFilter(LanguageFilter.java:23)
    org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)
    org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)
root cause
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Communications link failure during rollback(). Transaction resolution unknown.

Below is a method to save customer

public void addCustomer(Customer customer) {
        Transaction trns = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            trns = session.beginTransaction();
            session.save(customer);
            session.getTransaction().commit();  
        } 
        catch(Exception ex){
            if (trns != null) {
                trns.rollback();
            }
            ex.printStackTrace();
        }
        finally {
            session.close();
        }

My questions are that:

  • Why Tomcat show that page? How can we hide that page in the case there is an error

  • I tried to use connection pool, and it looks good till now (not deploying to production yet). I wonder if this is the best practice or there is a better solution

      <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
    
      <property name="hibernate.c3p0.min_size">5</property>
      <property name="hibernate.c3p0.max_size">20</property>
      <property name="hibernate.c3p0.timeout">300</property>
      <property name="hibernate.c3p0.max_statements">50</property>
      <property name="hibernate.c3p0.idle_test_period">3000</property>
    
      <property name="hibernate.c3p0.preferredTestQuery">SELECT 1</property>
      <property name="hibernate.c3p0.testConnectionOnCheckout">true</property>
    
    

Using current approach, I still see error in the log

     java.lang.Exception: DEBUG -- CLOSE BY CLIENT STACK TRACE
    at com.mchange.v2.c3p0.impl.NewPooledConnection.close(NewPooledConnection.java:646)
    at com.mchange.v2.c3p0.impl.NewPooledConnection.closeMaybeCheckedOut(NewPooledConnection.java:259)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.destroyResource(C3P0PooledConnectionPool.java:619)
    at com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask.run(BasicResourcePool.java:1024)
    at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:648)
2015-07-04 17:58:04 DEBUG C3P0PooledConnectionPool:627 - Successfully destroyed PooledConnection: com.mchange.v2.c3p0.impl.NewPooledConnection@48035c6e
2015-07-04 17:58:04 DEBUG BasicResourcePool:1027 - Successfully destroyed resource: com.mchange.v2.c3p0.impl.NewPooledConnection@48035c6e
2015-07-04 17:58:04 DEBUG BasicResourcePool:1561 - Removing expired resource: com.mchange.v2.c3p0.impl.NewPooledConnection@6e67b3ca [com.mchange.v2.resourcepool.BasicResourcePool@66d4416]
2015-07-04 17:58:04 DEBUG ThreadPoolAsynchronousRunner:236 - com.mchange.v2.async.ThreadPoolAsynchronousRunner@6fc0aa13: Adding task to queue -- com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask@2c56b7e4
2015-07-04 17:58:04 DEBUG BasicResourcePool:1747 - trace com.mchange.v2.resourcepool.BasicResourcePool@66d4416 [managed: 2, unused: 2, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@79f5060b)
2015-07-04 17:58:04 DEBUG BasicResourcePool:1022 - Preparing to destroy resource: com.mchange.v2.c3p0.impl.NewPooledConnection@6e67b3ca
2015-07-04 17:58:04 DEBUG C3P0PooledConnectionPool:616 - Preparing to destroy PooledConnection: com.mchange.v2.c3p0.impl.NewPooledConnection@6e67b3ca
2015-07-04 17:58:04 DEBUG GooGooStatementCache:355 - ENTER METHOD: closeAll( com.mysql.jdbc.JDBC4Connection@5c96419c )! -- num_connections: 0
2015-07-04 17:58:04 DEBUG GooGooStatementCache:394 - closeAll(): com.mchange.v2.c3p0.stmt.GlobalMaxOnlyStatementCache stats -- total size: 0; checked out: 0; num connections: 0; num keys: 0
2015-07-04 17:58:04 DEBUG BasicResourcePool:1747 - trace com.mchange.v2.resourcepool.BasicResourcePool@66d4416 [managed: 2, unused: 2, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@79f5060b)
2015-07-04 17:58:04 DEBUG NewPooledConnection:646 - com.mchange.v2.c3p0.impl.NewPooledConnection@6e67b3ca closed by a client.
java.lang.Exception: DEBUG -- CLOSE BY CLIENT STACK TRACE
    at com.mchange.v2.c3p0.impl.NewPooledConnection.close(NewPooledConnection.java:646)
    at com.mchange.v2.c3p0.impl.NewPooledConnection.closeMaybeCheckedOut(NewPooledConnection.java:259)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.destroyResource(C3P0PooledConnectionPool.java:619)
    at com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask.run(BasicResourcePool.java:1024)
    at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:648)
2015-07-04 17:58:04 DEBUG C3P0PooledConnectionPool:627 - Successfully destroyed PooledConnection: com.mchange.v2.c3p0.impl.NewPooledConnection@6e67b3ca

Aucun commentaire:

Enregistrer un commentaire