Hi, We build a single framed page by running two servlets - button bar and data frame... Both servlets access the same session. I'll try to explain the problem: Two Servlets - call them AA and BB running concurrently - like when building a page using two servlets...
AA runs and does a putValue in the session, then calls a getValue later on in the servlet... all OK so far... servlet is still running...
Meanwhile, servlet BB starts up and does the same putValue on the same key name, then does a getValue later on in the servlet... all OK so far...
Now servlet AA which hasnt finished yet and is still in the doGet, does a getValue on the session on that original key name, and it comes back null... but at seemingly random times - but for arguements sake, say its once every 100 attempts... So what is going on? The key/value pair that I'm putting in the session is simply a String/String pair. Do people think that the fact that the 2nd servlet has done a putValue has somehow chnaged the reference to the value, and so the 1st servlets ref point is now null? Cheers
In your situation I would certainly synchronize on the session object before changing/setting any stored data. Is the session object supposed to be created before either of these two servlets see it? If not, maybe two sessions are being created - log the session ids to see. Bill
hi - thanks for the reply! the session ID are the same in both servlets, i do a req.getSession(false) in both servlets and handle the event where this returns null etc... Would you ever expect the session to not be the same? I have re-written the code to only put the value in the session once, so i now have 'one putvalue and many getvalues' for this one variable. OK - this was originally bad coding - but its surprised me that it caused a problem. I dont want to use synchronised anywhere if possible - this particualar varaible i use is very heavily used in my system... I use synchronized for starting my connection pool up -so that only one servlet can do this etc... i'll be testing my code tomorrow to see if this has helped... cheers,
Joined: Sep 03, 2001
Bill - I find that using the session and its implications isnt explained very well in any book that i've read so far... havent read yours so cant comment as yet! For example: Do I *need* to do a putValue after I've done a getValue and then modified that object? Intuitively, you would think so, but as the local is just a ref to the original one, which is still in the heap, then it doesnt seem to be needed... eg: MyObject myobject = new MyObject(); // create my own object myobject.setMyProperty("some_value"); // set some property session.putValue("my_key", myobject); // store it in the session myobject.setMyProperty("some_new_value"); // change a property //session.putValue("my_key", myobject); // this isnt needed System.out.println(((MyObject)session.getValue("my_key")).getMyProperty()); // returns 'some_new_value' So am I right in saying that I only need do a putValue *after* I've created a 'new' object, and not after i've done a getValue and modified it? cheers,