• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enumeration

 
Umberto Fabbrucci
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the Java in General (intermediate) forum.

Dave
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • 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
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Devesh H Rao
Ranch Hand
Posts: 687
Hibernate jQuery Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20559
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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.
 
jiju ka
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic