session and synchronization

S

Saad Malik

I Everyone,
I had a quick question. I'm building a fairly simple server/client
application and I'm having a little problem:

My main Model/Controller class initially reads csv flat file and
creates about 100 JavaBeans (which HAS to be stored in a HashMap), and
then I store the HashMap into a session.

Then later I call dispatcher and forward the request to a JSP where it
extracts all of the beans.


while --- > HashMap.put(i,hashcar[j]
// assume hashCar is an HashMap containing 100 beans
session.setAttribute("hashCar",hashCar);

// then I forward to an JSP.
JSP PAGE
HashMap hashCar = (HashMap)session.getAttribute("hashCar")
// Use iterator on HashMap and iterate through the whole list...

Everything was perfect until I started debugging the application and
tried accessing the same webapp from same client successively in
multiple browser windows.

Then I get a ConcurrentModificationException from HashMap.

I was wondering if this kind of solution will fix it:

synchronize(hasCar)
// adding stuff


And also synchrnoize (hashCar)
in the JSP?
 
B

Bruno Tignac

Hello,

May be you could declare your hashmaps like that

HashMap myMap = Collections.synchronizedMap(new HashMap());

Bruno
 
V

VisionSet

Bruno Tignac said:
I Everyone,
I had a quick question. I'm building a fairly simple server/client
application and I'm having a little problem:

My main Model/Controller class initially reads csv flat file and
creates about 100 JavaBeans (which HAS to be stored in a HashMap), and
then I store the HashMap into a session.

Then later I call dispatcher and forward the request to a JSP where it
extracts all of the beans.


while --- > HashMap.put(i,hashcar[j]
// assume hashCar is an HashMap containing 100 beans
session.setAttribute("hashCar",hashCar);

// then I forward to an JSP.
JSP PAGE
HashMap hashCar = (HashMap)session.getAttribute("hashCar")
// Use iterator on HashMap and iterate through the whole list...

Everything was perfect until I started debugging the application and
tried accessing the same webapp from same client successively in
multiple browser windows.

Then I get a ConcurrentModificationException from HashMap.

I was wondering if this kind of solution will fix it:

synchronize(hasCar)
// adding stuff


And also synchrnoize (hashCar)
in the JSP?
Hello,

May be you could declare your hashmaps like that

HashMap myMap = Collections.synchronizedMap(new HashMap());

That will not work. This provides synchronisation at the atomic level. ie
for a single call eg put(...), it guarantees that multiple threads can call
methods on a map and not cause conflicts internal to the map. It does not
protect compound access for a thread eg while iterating over its values.

You simply cannot iterate over a collection and put things in it at the same
time. synchronising the whole iteration on the underlying map object should
do the trick I'd have thought, but it is probably not the best solution.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top