0

i have a grid from where users select the row , when a row is clicked then its id is sending to my action class AddBookToSession.java and after wards it is returning a list into my jsp page invoice.jsp

I am getting error java.util.ConcurrentModificationException ,when users selects a row from my grid.

I read this similar question ,But still i am not able to solve my problem.

My problem is : Why i am getting java.util.ConcurrentModificationException Error and how can I solve this issue. Please help me to solve this problem.

Error in console:

   Dec 10, 2012 11:37:30 PM org.apache.catalina.core.ApplicationDispatcher invoke
   SEVERE: Servlet.service() for servlet jsp threw exception
   java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:819)
    at java.util.ArrayList$Itr.next(ArrayList.java:791)
    at org.apache.struts2.components.IteratorComponent.end(IteratorComponent.java:334)
    at org.apache.struts2.views.jsp.IteratorTag.doAfterBody(IteratorTag.java:87)
       -------------------
        ------------------

invoice.jsp

  <s:if  test="#session['BOK'].size() > 0"> 
         <table width="100%" class="userTable" valign="top" border="0"> 
          <s:iterator value="#session.BOK" status="userStatus">
       <tr class="<s:if test="%{#userStatus.odd == true}">odd</s:if> <s:else>even</s:else>">
             <td width="80%"><s:property value="bookTitile" /></td>
                 <td align="right" width="20%">
                 <s:url id="cancelURL" action="RemovebooksFromSession" namespace="/admin/setups/secure/jspHomepage/bookstransaction">
                         <s:param name="bkid" value="%{id}"></s:param>
                  </s:url>
                <sj:a href="%{cancelURL}" targets="myAdvanceDivBoxx">Rem</sj:a></td>
        </tr>
         </s:iterator>
         </table> 
         </div>
     </s:if>

AddBookToSession.java

  public String execute() 
    {  
        String bookid = request.getParameter("bid"); 
        String qnty=dao.getquantityById(Integer.parseInt(bookid));

        if(qnty.equals("0")||qnty.equals("")||qnty.equals("null")){
            return SUCCESS;
        } 
        Bookdetails book = dao.listBookDetailsById(Integer.parseInt(bookid));
        books = (ArrayList) session.get(BillTransactionBooksConstants.BOK);
        if ( books == null ) books = new ArrayList<Bookdetails>();
        boolean already_exists = false;
        for ( Bookdetails b : books ) 
        {
              if ( Integer.toString(b.getId()).equals(bookid))
            {
                already_exists = true; 
                break;
            }
        }
        if (book != null && !already_exists  ) 
        { 
            books.add(book);
            System.out.println("books size"+books.size()); 
            session.put(BillTransactionBooksConstants.BOK,books);
        }
        return SUCCESS;
    } 
Community
  • 1
  • 1
Dan
  • 2,086
  • 11
  • 71
  • 137
  • That JSP looks like it is the one to remove book from the list in the session. Are you sure you are showing the right JSP code? Also, you should post more of the stacktrace, e.g. currently it's missing the JSP name. – Bhesh Gurung Dec 10 '12 at 22:45
  • @BheshGurung That is correct jsp. The list which i added in the session is iterating here. i given a link removebook to remove from the list. – Dan Dec 11 '12 at 03:48

3 Answers3

0

Fundamentally, you're modifying an object while some other object is depending on the state of the first object to remain unchanged.

This is most often seen when using a non-modification-safe iterator on some sort of collection object, and then modifying the collection.

My JSP is too rusty to wade through your source, but I see a couple of cases where you appear to be using iterators, and the one in your JSP looks most suspect.

(I believe this error can also occur when you use multi-threading to manage collection objects in a non-thread-safe fashion.)

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
  • can you please explain it with the solution similar to my question? – Dan Dec 10 '12 at 19:10
  • @AshutoshSingh - Look at the places where you modify an ArrayList (which is what is complaining). See if you're doing so while you're iterating through the ArrayList. My guess is that "RemovebooksFromSession" is the guilty party. – Hot Licks Dec 10 '12 at 19:42
  • @AshutoshSingh - Hint: Actually *look* at the exception traceback! – Hot Licks Dec 10 '12 at 19:43
0

It is possible that the page is refreshed around the time your code executes "books.add(book);" in which case you will get the exception as you are modifying a collection at same time someone else is using it.

Is it possible to modify your code so that the "books" you modify in the AddBookToSession is copy of the internal list of books.

books = new ArrayList<Bookdetails>((ArrayList) session.get(BillTransactionBooksConstants.BOK););
Ashwinee K Jha
  • 9,187
  • 2
  • 25
  • 19
0

The exception stack trace points to the s:iterator in your jsp being the place where the exception is thrown. That means that while that element goes through the book list another piece of code adds or removes from the list. That could be your Java code, or some other (e.g. RemovebooksFromSession).

Take a look at your code and try to determine if it is possible that other code runs while your jsp page is being build.

Cannot help you more with the code you posted.

pushy
  • 9,535
  • 5
  • 26
  • 45
  • The problem was same as you said. The same list was doing two functions together. i.e user is adding element on the list at the same time the list is iterating and showing in the same page. For solution i added if list is not completly shown users can not add new value on it. – Dan Dec 11 '12 at 19:23