aspose file tools*
The moose likes Java in General and the fly likes Enumeration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Enumeration" Watch "Enumeration" New topic
Author

Enumeration

Umberto Fabbrucci
Greenhorn

Joined: Aug 30, 2004
Posts: 6
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


Umbe
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Moving to the Java in General (intermediate) forum.

Dave
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
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

Joined: Feb 27, 2002
Posts: 8919

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.


Groovy
Devesh H Rao
Ranch Hand

Joined: Feb 09, 2002
Posts: 687

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

Joined: Oct 27, 2005
Posts: 19723
    
  20

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?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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.


"I'm not back." - Bill Harding, Twister
jiju ka
Ranch Hand

Joined: Oct 12, 2004
Posts: 306
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

Joined: Jan 30, 2000
Posts: 18671
[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 ]
 
jQuery in Action, 2nd edition
 
subject: Enumeration