9 Replies Latest reply on Mar 11, 2011 11:19 PM by itays100

    Error after a while

    itays100 Newbie

      Hi All,


      I am facing a problem which I am sure people will be in interesting in.
      After leave the application for a day I did a refresh (F5). I got the an error from tomcat which indicate that
      a connection is already closed!


      My persitence.xml :


      <?xml version="1.0" encoding="UTF-8"?>
      <persistence xmlns="http://java.sun.com/xml/ns/persistence" 
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" 
                   version="1.0">
         <persistence-unit name="bookingDatabase" transaction-type="RESOURCE_LOCAL">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <jta-data-source>java:comp/env/jdbc/TestDB</jta-data-source>
            <properties>         
               <property name="hibernate.hbm2ddl.auto" value="update"/>
               <property name="hibernate.show_sql" value="true"/>
               <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/>
            </properties>
         </persistence-unit>
      </persistence>



      My component.xml looks like:




      <core:manager conversation-timeout="120000" 
                        concurrent-request-timeout="500"
                        conversation-id-parameter="cid"/>
                        
          <transaction:entity-transaction entity-manager="#{entityManager}"/>
                        
          <persistence:entity-manager-factory name="bookingDatabase"/>
          
          <persistence:managed-persistence-context name="entityManager"
                                     auto-create="true" 
                          entity-manager-factory="#{bookingDatabase}"/>
      
           <factory name="hibernateSession" scope="STATELESS" auto-create="true"
          value="#{entityManager.delegate}" />
      
          <ui:jpa-entity-loader entity-manager="#{entityManager}" />




      Tomcat stack:




      Error while commiting the transaction  
      
          javax.persistence.RollbackException: Error while commiting the transaction
           at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:71)
           at org.jboss.seam.transaction.EntityTransaction.commit(EntityTransaction.java:110)
           at org.jboss.seam.util.Work.workInTransaction(Work.java:58)
           at org.jboss.seam.transaction.TransactionInterceptor.aroundInvoke(TransactionInterceptor.java:91)
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
           at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:44)
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
           at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
           at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:185)
           at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:103)
           at org.jboss.seam.ui.JpaEntityLoader_$$_javassist_seam_14.put(JpaEntityLoader_$$_javassist_seam_14.java)
           at org.jboss.seam.ui.EntityConverter.getAsString(EntityConverter.java:65)
           at org.jboss.seam.ui.converter.PrioritizableConverter.getAsString(PrioritizableConverter.java:67)
           at org.jboss.seam.ui.converter.ConverterChain.getAsString(ConverterChain.java:126)
           at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getFormattedValue(HtmlBasicRenderer.java:448)
           at com.sun.faces.renderkit.html_basic.MenuRenderer.renderOption(MenuRenderer.java:480)
           at com.sun.faces.renderkit.html_basic.MenuRenderer.renderOptions(MenuRenderer.java:772)
           at com.sun.faces.renderkit.html_basic.MenuRenderer.renderSelect(MenuRenderer.java:832)
           at com.sun.faces.renderkit.html_basic.MenuRenderer.encodeEnd(MenuRenderer.java:280)
           at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:861)
           at org.jboss.seam.ui.util.cdk.RendererBase.renderChild(RendererBase.java:190)
           at org.jboss.seam.ui.util.cdk.RendererBase.renderChildren(RendererBase.java:166)
           at org.jboss.seam.ui.renderkit.ValidateAllRendererBase.doEncodeChildren(ValidateAllRendererBase.java:33)
           at org.jboss.seam.ui.util.cdk.RendererBase.encodeChildren(RendererBase.java:92)
           at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:837)
           at org.jboss.seam.ui.util.cdk.RendererBase.renderChild(RendererBase.java:186)
           at org.jboss.seam.ui.util.cdk.RendererBase.renderChildren(RendererBase.java:166)
           at org.jboss.seam.ui.renderkit.DecorateRendererBase.doEncodeChildren(DecorateRendererBase.java:152)
           at org.jboss.seam.ui.util.cdk.RendererBase.encodeChildren(RendererBase.java:92)
           at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:837)
           at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:234)
           at com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:180)
           at com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:127)
           at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:837)
           at org.ajax4jsf.renderkit.RendererBase.renderChild(RendererBase.java:282)
           at org.ajax4jsf.renderkit.RendererBase.renderChildren(RendererBase.java:262)
           at org.ajax4jsf.renderkit.RendererBase.renderChild(RendererBase.java:284)
           at org.ajax4jsf.renderkit.RendererBase.renderChildren(RendererBase.java:262)
           at org.richfaces.renderkit.html.ModalPanelRenderer.doEncodeChildren(ModalPanelRenderer.java:385)
           at org.richfaces.renderkit.html.ModalPanelRenderer.doEncodeChildren(ModalPanelRenderer.java:380)
           at org.ajax4jsf.renderkit.RendererBase.encodeChildren(RendererBase.java:121)
           at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:837)
           at javax.faces.component.UIComponent.encodeAll(UIComponent.java:930)
           at javax.faces.component.UIComponent.encodeAll(UIComponent.java:933)
           at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:592)
           at org.ajax4jsf.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:100)
           at org.ajax4jsf.application.AjaxViewHandler.renderView(AjaxViewHandler.java:176)
           at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:110)
           at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
           at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
           at javax.faces.webapp.FacesServlet.service(FacesServlet.java:266)
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
           at org.jboss.seam.web.IdentityFilter.doFilter(IdentityFilter.java:40)
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:90)
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:178)
           at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
           at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:390)
           at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:517)
           at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:56)
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
           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.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:433)
           at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
           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:293)
           at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
           at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
           at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
           at java.lang.Thread.run(Thread.java:619)
      Caused by: org.hibernate.exception.GenericJDBCException: Cannot release connection
           at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
           at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
           at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
           at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
           at org.hibernate.jdbc.ConnectionManager.closeConnection(ConnectionManager.java:455)
           at org.hibernate.jdbc.ConnectionManager.aggressiveRelease(ConnectionManager.java:406)
           at org.hibernate.jdbc.ConnectionManager.afterTransaction(ConnectionManager.java:293)
           at org.hibernate.jdbc.JDBCContext.afterTransactionCompletion(JDBCContext.java:221)
           at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:127)
           at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:54)
           ... 81 more
      Caused by: java.sql.SQLException: Already closed.
           at org.apache.tomcat.dbcp.dbcp.PoolableConnection.close(PoolableConnection.java:84)
           at org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.close(PoolingDataSource.java:181)
           at org.hibernate.connection.DatasourceConnectionProvider.closeConnection(DatasourceConnectionProvider.java:74)
           at org.hibernate.jdbc.ConnectionManager.closeConnection(ConnectionManager.java:451)
           ... 86 more



      Do you have any idea how can I avoid this error ?
      Thanks!

        • 1. Re: Error after a while
          Tim Evers Master

          Timeouts were invented for a good reason. :)


          You have at least 2 timeouts to think about.


          Your Session timeout which you'll probably find in your web.xml (measured in minutes)
          Your conversation-timeout in your components.xml (measured in milliseconds)


          <core:manager conversation-timeout="120000" 
                            concurrent-request-timeout="500"
                            conversation-id-parameter="cid"/>
          



          Your connection was closed because your conversation timed out. Your conversation timeout is set to 2 mins. Sounds to me like you need to put a rule in your pages.xml for no conversation so that you get redirected to the login screen after your conversation or session times out.

          • 2. Re: Error after a while
            itays100 Newbie

            Hi,


            I have both rules:


            session timeout (10 min):




            <exception class="javax.faces.application.ViewExpiredException">
                    <redirect view-id="/home.xhtml"/>
             </exception>



            No conversation:




            <pages xmlns="http://jboss.com/products/seam/pages"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="http://jboss.com/products/seam/pages http://jboss.com/products/seam/pages-2.1.xsd"
            
                   no-conversation-view-id="/home.xhtml"
                             login-view-id="/home.xhtml">




            • 3. Re: Error after a while
              itays100 Newbie

              Anyone has an idea how to solve this issue ?


              Thanks!

              • 4. Re: Error after a while
                Leo van den berg Master

                Hi,


                You should set/test pre-conditions before entereing a page. After catching these errors you should redirect to a page which has to figure out if you timed-out and the restart again.


                Leo

                • 5. Re: Error after a while
                  itays100 Newbie

                  I figure out how to avoid this error using the following configuration in persistence.xml :



                  <?xml version="1.0" encoding="UTF-8"?>
                  <persistence xmlns="http://java.sun.com/xml/ns/persistence" 
                               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                               xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" 
                               version="1.0">
                     <persistence-unit name="bookingDatabase" transaction-type="RESOURCE_LOCAL">
                        <provider>org.hibernate.ejb.HibernatePersistence</provider>
                        <jta-data-source>java:comp/env/jdbc/TestDB</jta-data-source>
                        <properties>         
                              <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
                           <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
                           <property name="hibernate.show_sql" value="true"/>
                           <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/>
                           
                           <property name="transaction.flush_before_completion" value="true"/>
                             <property name="connection.release_mode" value="after_statement"/>
                        </properties>
                     </persistence-unit>
                  </persistence>




                  Leo,
                  I am not sure what tests are you talking about cause I cannot catch this exception on the code. And I am not asking for a connection in the code. All is managed by the framework.


                  Only after changing the connection release mode to be after_statement the exception disappeared.
                  I am not sure if this is best practice behaviour though!
                  Any thought/suggestion is welcome!

                  • 6. Re: Error after a while
                    Leo van den berg Master

                    Hi,


                    try Seam's conversation component and check the status of the application/session before doing something with the data on the outdated page.


                    Leo

                    • 7. Re: Error after a while
                      itays100 Newbie

                      Hi Leo,
                      Thanks for reply!
                      I am using seam conversation component. I am not sure what do you mean by check the status of the application/session.
                      Do you have an example of code ? Why should I do all this anyway ? Seam/hibernate (JPA) should handle this automatically.


                      That's why I choose seam in the first place.


                      Thanks

                      • 8. Re: Error after a while
                        Leo van den berg Master

                        Hi,


                        If I understand it correct, you leave a specific page on the screen and access after the session/conversation timeout. You can take different approaches in pages.xml


                        I use a bean with a Session scope and a destroy method which helps you to figure out if a session is active or not. In the example below, I use the userName in an Event which is fired whenever the session closes. I use it for logout registration (it even works if you close the window)





                        @Destroy
                        public void destroy(){
                             Events.instance().raiseAsynchronousEvent("es.esam.xpose.session.timeout", userName);
                        }
                             
                        



                        You can observer this event and do whatever you want with it. In your case I would use a ajax-poll to see if the session has timed out and render a login.


                        Leo

                        • 9. Re: Error after a while
                          itays100 Newbie

                          I'm using remember me, so i prefer to avoid logout the user on session timeout. But I do want to release the db connection on session timeout,so the next time the user request a page he will start a new transaction.


                          The problem now is that the application try to do a roll back to a transaction from the day before.


                          It looks like that I need to release connection on session timeout. Do you have any idea how to do it on seam ?
                          What the point for the persitence.xml then ?