From:   "Terence M. Bandoian" <tere...@tmbsw.com>
To:     Tomcat Users List <users@tomcat.apache.org>
Date:   02/01/2016 07:58 PM
Subject:        Tomcat Server - Arraylist 
java.util.ConcurrentModificationException issue



On 2/1/2016 6:50 AM, Subhro Paul wrote:
> Hi Team,
>
> Our web application has a "header.jsp" which has 2  Arraylist on it. 
Each
> ArrayList has more than 50 items inside. The code is to identify the
> mobile device and requested page and transfer the call to mobile page
> accordingly.
>
> This code works fine once we restart the server and can continue running 
2
> months without any issue. But day by day it starts showing 500 error 
with
> exception in log "ConcurrentModificationException". Below is the code
> snippet of JSP code. My Question is why this issue is happening around 2
> months? If we clear the temp file created by the server in work folder
> then that will run for some time( 2- 3 days) and then again the same
> exception starts occurring. I have identified one solution by moving the
> JAVA code from JSP to JAVA file which worked good while perform testing
> but client wants to know the root cause of the issue.
>
> FYI, earlier we had Vector in place of Arraylist which gave trouble of
> thread blocking due to synchronization. So, converting from Arraylist to
> Vector will not be a good idea.
>
>
> Exception:
>
> java.util.ConcurrentModificationException
>          at
> java.util.ArrayList$Itr.checkForComodification(ArrayList.java:859)
>          at java.util.ArrayList$Itr.next(ArrayList.java:831)
>          at
> org.apache.jsp.includes.header_jsp.isHomePage(header_jsp.java:97)
>
>
> Code Snippet:
>
> <%!
> private Set uas = new HashSet();
> ArrayList<String> urlnames = new ArrayList<String>();
> ArrayList<String> excludePathList = new ArrayList<String>();


Hi, Subhro-

Fields declared in a declarations section (<%!  ... %>) are class 
variables.  If Tomcat uses a single JSP object to serve multiple 
requests, which I believe it does, access to these fields should be made 
thread safe.  A simple solution would be to move the declarations of 
these fields to a scriptlet section (<% ... %>) which would result in 
them being local to the JSP service method.  It isn't the most efficient 
way to go about it but it should solve the concurrent access problem 
you're seeing.

-Terence Bandoian
/http://www.tmbsw.com/
/

>
> private boolean haveToRedirect(HttpServletRequest request,
>                  String stopMobiCookie) {
>          boolean doRedirect = false;
> 
>          String userAgent = request.getHeader("User-Agent");
>          if (! stopMobiCookie.equals("yes") && userAgent != null &&
> userAgent.length()!=0 && (userAgent.indexOf("UsableNet")==-1))
>          {
>
>                  Iterator iter = uas.iterator();
>                  while (iter.hasNext()) {
>                          if (userAgent.indexOf((String)iter.next())!=-1) 
{
>                                  doRedirect = true;
>                                  break;
>
>
>                          }
>                  }
>          }
>          return doRedirect;
> }
>
>          private boolean isHomePage(HttpServletRequest request){
>                  Iterator<String> itr = urlnames.iterator();
>                  String path = "";
>                  while (itr.hasNext()) {
>                           path = itr.next();
>                           if
> (request.getRequestURI().toString().equalsIgnoreCase(path)){
>                                   return true;
>                           }
>                  }
>                  return false;
>          }
> 
>          private boolean isExcludePath(HttpServletRequest request){
>                  Iterator<String> itr = excludePathList.iterator();
>                  String path = "";
>                  while (itr.hasNext()) {
>                           path = itr.next();
>                           if (request.getRequestURI().startsWith(path) 
&&
> !request.getRequestURI().startsWith("/info/contact.jsp")){
>                                   return true;
>                           }
>                  }
>                  return false;
>          }
>
> %>
>
>
> <%
>
> uas.add("Blazer");
> uas.add("Danger hiptop");
> uas.add("DoCoMo/");
> uas.add("Ericsson");
> uas.add("Googlebot-Mobile");
> uas.add("MSN Mobile Proxy");
> uas.add("Handheld");
> uas.add("HTC_HD2_T58585 Opera");
> uas.add("iPhone");
> uas.add("iPod");
> uas.add("Klondike");
> uas.add("LG-");
> uas.add("LGE-");
> ....... Arround 40 items
>
>
> excludePathList.add("/business/my_account");
> excludePathList.add("/business/save_energy");
> excludePathList.add("/business/services");
> excludePathList.add("/business/small_large_business");
> excludePathList.add("/info/environment");
> excludePathList.add("/info/about");
> excludePathList.add("/info/index.jsp");
> excludePathList.add("/info/ambassador.jsp");
> ........ more than 50 items
>
>
> stopMobiCookie = some cookie code
>
> boolean doRedirect = haveToRedirect((HttpServletRequest)request,
> stopMobiCookie);
>
>
> if(doRedirect){
>                          boolean isHomePage =
> isHomePage((HttpServletRequest)request);
>                          boolean isExcludePath =
> isExcludePath((HttpServletRequest)request);
> 
>                          session.setAttribute("mobile_agent", "yes");
>
>                          if(!isHomePage && !isExcludePath){
>                                  String mobileServer = 
"mobile.server.com";
>                                  try{
>                                          mobileServer = mobileServer +
> request.getRequestURI().toString();
>                                  }catch(Exception e){}
> %>
>                                  <script type="text/javascript">
>                                          window.location
> ="<%=mobileServer%>";
>                                  </script>
> <%
>                          }
>                  }
> %>
>
>
>
> Thanks & Regards
> Subhro Paul


Hi Team,

But why this error come after long time? this should have occurred at 
starting.

Thanks & Regards,
Subhro Paul 


=====-----=====-----=====
Notice: The information contained in this e-mail
message and/or attachments to it may contain 
confidential or privileged information. If you are 
not the intended recipient, any dissemination, use, 
review, distribution, printing or copying of the 
information contained in this e-mail message 
and/or attachments to it are strictly prohibited. If 
you have received this communication in error, 
please notify us by reply e-mail or telephone and 
immediately and permanently delete the message 
and any attachments. Thank you


Reply via email to