-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: ConcurrentModificationException when iterating over sets
PostPosted: Tue Jan 25, 2005 5:15 pm 
Newbie

Joined: Fri Oct 01, 2004 1:29 pm
Posts: 5
Location: Brasil
My n-tier webapp uses AppFuse 1.7 as a base for development.
It uses Spring Framework (Spring MVC too) and Hibernate (v 2.1.7c) integrated.
I'm using OpenSessionInView pattern, and my tiers are:

Model (Pojo's) -> DAOs -> Services/Managers -> Controllers -> Views.

So, in the DAOs layer, I get one list of objects of type Category,
with a generic method:

Code:
    public List getObjects(Class clazz) {
        return getHibernateTemplate().loadAll(clazz);
    }


In the Services layer, I want to filter the list (exclude the childs) to show it in the views.
The controller tells the service layer to get these filtered list:

Code in the controller:
Code:
    protected Map referenceData(HttpServletRequest request)
        throws Exception {
        Categoria categoria = (Categoria) formBackingObject(request);

        Map model = new HashMap();
        model.put("categorias", mgr.getObjects(Categoria.class, categoria, Boolean.TRUE, "getCategoriasFilhas"));

        return model;
    }


Code in the manager:
Code:
    public List getObjects(Class clazz) {
        return dao.getObjects(clazz);
    }

    public List getObjects(Class clazz, Object excluded, Boolean excludeChilds, String methodToGetChilds) {
        List objects = getObjects(clazz);
       
        Set toRemove = new HashSet();
        toRemove .add(excluded);
       
        if (excludeChilds == Boolean.TRUE) {
            Set childs = getChilds(excluded, methodToGetChilds);
            if (!childs.isEmpty()) {
                toRemove.addAll(childs);
            }
        }
       
        objects.removeAll(toRemove);
        return objects;
    }
   
    private Set getChilds(Object root, String methodToGetChilds) {
        Set childs = (Set) callObjectMethod(root, methodToGetChilds);
        //Set childs = getChildsCopy(root, methodToGetChilds);
       
        Iterator i = childs.iterator();
        while (i.hasNext()) {
            Object sub = i.next();
            Set subChilds = getChilds(sub, methodToGetChilds);
            if (!subChilds.isEmpty()) {
                childs.addAll(subChilds);
            }
        }
       
        return childs;
    }
   
    /*
    private synchronized Set getChildsCopy(Object root, String methodToGetChilds) {
        return new HashSet((Set) callObjectMethod(root, methodToGetChilds));
    }
    */

    public Object callObjectMethod(Object object, String methodName) {
       Object ret = null;
       Class clazz   = object.getClass();
       try {
          Method method = clazz.getMethod(methodName, null);
          ret = method.invoke(object, null);
       } catch (Exception ex) {
          return null;
       }
       return ret;       
    }


I tried to make a synchronized copy of the list before iterating over it, with is the code commented above.
Copying or not, the code in the getChilds method causes a ConcurrentModificationException in the half of the process.
I suspect that this is because I am using lazy loading in the childs. (class Categoria, method getCategoriasFilhas)

An example graph of objects is:

Code:
         Root
       /      \
      2       5
     / \        \
   3    4       6
        /  \
       7   8


When the code finishes the tree above instance "2" and get's the top (root) to iterate over the tree above "5", that exception occurs.

Anyone have any code/sample/ideia to solve this? I could filter this list in the DAO layer using HQL/etc, but i want to filter this here in the service layer, because I desire to improve my OO logic.

Thanks very much

[]'s
Caetano


Top
 Profile  
 
 Post subject: Re: ConcurrentModificationException when iterating over sets
PostPosted: Fri Oct 16, 2009 5:35 pm 
Regular
Regular

Joined: Thu Sep 09, 2004 6:46 pm
Posts: 96
Did u solve this?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.