• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Enumeration

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am getting the "ConcurrentModificationException" in this block of code :

for(Enumeration en = sessione.getAttributeNames(); en.hasMoreElements(); sessione.removeAttribute((String)en.nextElement()));


I'm using Tomcat/4.1.31

can someone please help me ? Thank
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to the Java in General (intermediate) forum.

Dave
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you using an Iterator? Make sure there is no trace of the Iterator outside the block you are using it in, OR make sure your Iterator is declared and used as a local variable, so it vanishes after you hve finished using it.
If you need it again elsewhere, you will have to declare it as a local variable anew.
CR
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Umberto Fabbrucci:
Hi,
I am getting the "ConcurrentModificationException" in this block of code :

for(Enumeration en = sessione.getAttributeNames(); en.hasMoreElements(); sessione.removeAttribute((String)en.nextElement()));


I'm using Tomcat/4.1.31

can someone please help me ? Thank



ConcurrentModificationException occurs when another or the current thread tries to manipulate the Collection when the current thread is iterationg the Collection . It is surprising to see this exception occuring for Enumeration interface which is outside the Collection framework.
 
Ranch Hand
Posts: 687
Hibernate jQuery Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Umberto Fabbrucci:
Hi,
I am getting the "ConcurrentModificationException" in this block of code :

for(Enumeration en = sessione.getAttributeNames(); en.hasMoreElements(); sessione.removeAttribute((String)en.nextElement()));


I'm using Tomcat/4.1.31

can someone please help me ? Thank




1. Enumeration en = sessione.getAttributeNames()
........
3. sessione.removeAttribute((String)en.nextElement()));


ConcurrentModificationException occurs as the loop is working on the enumeration which is in turn linked to the session from where you are removing the attribute.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is why the Java API discourages Enumerations:

NOTE: The functionality of this interface is duplicated by the Iterator interface. In addition, Iterator adds an optional remove operation, and has shorter method names. New implementations should consider using Iterator in preference to Enumeration.


But you are trying to remove all attributes, isn't there a better method for that? Something like clear or removeAllElements?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Pradip Bhat]: ConcurrentModificationException occurs when another or the current thread tries to manipulate the Collection when the current thread is iterationg the Collection .

Not necessarily another thread. Could also be the same thread, as seen here.

It is surprising to see this exception occuring for Enumeration interface which is outside the Collection framework.

It is surprising. But remember that Enumeration is just an interface - how is it implemented underneath? In this case it I'm guessing that Tomcat's HttpSession may be using a HashMap or TreeMap internally to maintain the session attributes, and getAttributeNames() is returning an Enumeration which gives a view of the keySet(). Unfortunately this is a bad choice for Tomcat - the HttpSession API doesn't really offer a better way to remove attributes. They should have returned an enumeration containing a copy of the attribute names, so this wouldn't be an issue. Oh well.

Pradip, you should be able to work around this by copying the Enumeration to a separate List, then iterate through the List and removeAttribute() for each name.
 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Solution to this will be to cache the key while iterating and then iterate through keys to remove the attributes.

HttpSession don't have a removeAll, clear, empty or clean. What is the reason?
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[jiju ka]: Solution to this will be to cache the key while iterating and then iterate through keys to remove the attributes.

Yes, that sounds rather similar to copying the Enumeration to a separate List and then iterating through the List, doesn't it?

HttpSession don't have a removeAll, clear, empty or clean. What is the reason?

I suppose the original designers of the interface didn't think of it, and it's not considered important enough to try to add now. Adding methods to an interface is fairly unusual in a published (and widely used) API, as it would break existing implemenations. And really, it should be fairly easy to remove all attributes the way Umberto tried to. It's just that Tomcat's implementation here wasn't thought out well enough.
[ November 17, 2005: Message edited by: Jim Yingst ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic