2 Replies Latest reply on Jul 24, 2006 10:20 AM by flo thomas

    ConcurrentModificationException

    flo thomas Newbie

      Hi,
      I am getting a ConcurrentModificationException which I cannot really resolve, maybe someone has some hints...

      I have the following:

      @Stateful
      @Name("accBroker")
      @Scope(CONVERSATION)
      @LoggedIn
      public class AccountBrokerBean implements AccountBroker{
      
      @In(create=true)
       private EntityManager em;
      
       @In(value="securityBroker",create=true)
       private SecurityBroker securityBroker;
      
      @In
      User user;
      
       @Begin(join=true)
      public void find() {
       List<Accounts> accounts = em.createQuery("from Account");
       securityBroker.filter(user,accounts);
      
      }
      ...
      @Stateful
      @Name("securityBroker")
      @Scope(CONVERSATION)
      @LoggedIn
      public class SecurityBrokerBean implements SecurityBroker{
      
      @In(create=true)
       private EntityManager em;
      
      public void filter(User u, Collection<Accounts> accounts) {
       for (Account a : accounts) {
       if (accounts.notAccessibleBy(user) {
       accounts.remove(a);
       } else if (accounts.accessibleBy(user) {
       continue;
       } else {
       accounts.remove(a);
       }
      }
      
      }
      
      ...
      


      I get the CME somehere in the filter method...
      Any clues?

      Thanks,
      Joey

        • 1. Re: ConcurrentModificationException
          hdslkf hklsdg Apprentice

          You cannot change the length of a Collection (by adding or removing items) while iterating through it.

          You can
          - make a copy of the account collections, delete accounts from that copy during iteration, clear the account collection and insert all elements of the copy into the account collection *uff*
          - insert all accounts to delete into a toDelete collection during iteration, and then iterate over the toDelete collection and delete items from the account collection.

          Confused? Here's an example for the second way:

          public void filter(...) {
           Collection<Accounts> toDelete = new ArrayList<Accounts>();
           for (Account a: accounts)
           if(accounts.notAccessibleBy(user)
           toDelete.add(a);
           else if (accounts.accessibleBy(user)
           continue;
           else
           toDelete.add(a);
          
          for (Account a: toDelete)
           accounts.remove(a);
          }

          btw: The checks you make (notAccessiblyBy, accessibleBy) seem to be a bit weird ;). It looks like you want to keep ALL accounts in the collection or delete ALL of them. Wouldn't it work like this?
          public void filter(...) {
           if (accounts.notAccessibleBy(user))
           accounts.clear();
          }



          • 2. Re: ConcurrentModificationException
            flo thomas Newbie

            Hi
            Thanks for your help.
            mhm... I was so sure that it is related to some transactional stuff that I didn't even think about that :) - the first example works...


            there are actually a couple of access rules that I left out, so it's quite a complex scheme since accounts have default visibility rules and can be hidden from specific users and can be shared with specific users...

            Thanks!!!!