3 Replies Latest reply on Jul 13, 2004 4:59 PM by Sampath Kondam

    ConcurrentModificationException while retreiving Session var

    Prasad Ganguri Newbie

      We are getting the ConcurrentModificationException while fetching the session variables.

      Initially, it is not throwing any exception, but after two or three iterations of session variable changes, it is throwing the error.

      The code is:

      public void cleanUpSessionVar (String sessionVarName)
      {
      Enumeration e = session.getAttributeNames();
      while (e.hasMoreElements())
      {
      String name = (String)e.nextElement();
      if(name.equalsIgnoreCase(sessionVarName))
      session.removeAttribute(name);

      }
      }
      The stack trace is :
      15:33:47,764 INFO [STDOUT] java.util.ConcurrentModificationException
      15:33:47,774 INFO [STDOUT] at java.util.HashMap$HashIterator.next(HashMap.java:731)
      15:33:47,774 INFO [STDOUT] at org.apache.catalina.util.Enumerator.nextElement(Enumerator.java:217)
      15:33:47,784 INFO [STDOUT] at com.secret.http.SessionVariables.cleanUpSessionVar (SessionVars.java:60)
      15:33:47,794 INFO [STDOUT] at org.apache.jsp.TabHandler_jsp._jspService(TabHandler_jsp.java:233)
      15:33:47,794 INFO [STDOUT] at com.secret.http.jsp.JspBase.service(SecrspPage.java:32)
      15:33:47,804 INFO [STDOUT] at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:210)
      15:33:47,814 INFO [STDOUT] at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
      15:33:47,824 INFO [STDOUT] at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
      15:33:47,834 INFO [STDOUT] at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
      15:33:47,834 INFO [STDOUT] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:
      247)
      15:33:47,855 INFO [STDOUT] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
      15:33:47,855 INFO [STDOUT] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
      15:33:47,865 INFO [STDOUT] at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPip
      eline.java:643)
      15:33:47,875 INFO [STDOUT] at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      15:33:47,885 INFO [STDOUT] at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      15:33:47,895 INFO [STDOUT] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
      15:33:47,905 INFO [STDOUT] at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPip
      eline.java:643)
      15:33:47,925 INFO [STDOUT] at org.jboss.web.tomcat.security.JBossSecurityMgrRealm.invoke(JBossSecurityMgrRealm.java:220)
      15:33:47,925 INFO [STDOUT] at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPip
      eline.java:641)
      15:33:47,945 INFO [STDOUT] at org.apache.catalina.valves.CertificatesValve.invoke(CertificatesValve.java:246)
      15:33:47,945 INFO [STDOUT] at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPip
      eline.java:641)
      15:33:47,965 INFO [STDOUT] at org.jboss.web.tomcat.tc4.statistics.ContainerStatsValve.invoke(ContainerStatsValve.java:76)
      15:33:47,975 INFO [STDOUT] at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPip
      eline.java:641)
      15:33:47,985 INFO [STDOUT] at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      15:33:47,995 INFO [STDOUT] at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      15:33:48,005 INFO [STDOUT] at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2417)
      15:33:48,015 INFO [STDOUT] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
      15:33:48,015 INFO [STDOUT] at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPip
      eline.java:643)
      15:33:48,035 INFO [STDOUT] at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
      15:33:48,035 INFO [STDOUT] at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPip
      eline.java:641)
      15:33:48,055 INFO [STDOUT] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
      15:33:48,055 INFO [STDOUT] at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPip
      eline.java:641)
      15:33:48,075 INFO [STDOUT] at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:6
      5)
      15:33:48,085 INFO [STDOUT] at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPip
      eline.java:641)
      15:33:48,095 INFO [STDOUT] at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:577)
      15:33:48,105 INFO [STDOUT] at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPip
      eline.java:641)
      15:33:48,115 INFO [STDOUT] at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      15:33:48,125 INFO [STDOUT] at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      15:33:48,135 INFO [STDOUT] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
      15:33:48,145 INFO [STDOUT] at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPip
      eline.java:643)
      15:33:48,155 INFO [STDOUT] at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      15:33:48,165 INFO [STDOUT] at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      15:33:48,165 INFO [STDOUT] at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:197)
      15:33:48,175 INFO [STDOUT] at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:781)
      15:33:48,185 INFO [STDOUT] at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Proto
      col.java:549)
      15:33:48,205 INFO [STDOUT] at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:605)
      15:33:48,205 INFO [STDOUT] at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:677)
      15:33:48,215 INFO [STDOUT] at java.lang.Thread.run(Thread.java:479)


      I tried to catch the exception and tried to print the stack trace it showing that the process is going in an infinite loop.

      public void cleanUpSessionVar (String sessionVarName)
      {
      Enumeration e = session.getAttributeNames();
      while (e.hasMoreElements())
      {
      String name = "";
      try
      {
      name = (String)e.nextElement();
      }
      catch (Exception ex)
      {
      ex.printStackTrace();
      }
      if (name.equalsIgnoreCase(sessionVarName))
      {
      session.removeAttribute(name);
      }
      }
      }





      What is wrong with our code? Please help.....