This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Servlet thread safety of a local session attribute  RSS feed

 
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good day, all.

I have a servlet, in which I have local variables declared, initiated and assigned to session attributes.

I need the servlet to be thread-safe, such that multiple browser windows on same computer can have the servlet running and you could perform calculations in each window, but such that the operations in the various threads would not influence each other, that is, that the account balance variable was not shared among the threads, causing inconsistent states of it for any thread at any time.

The below is my code:

   

Now, what happens instead is, when using multiple threads (browser windows running the servlet), the balance that one thread overwrites, becomes the balance that another thread reads. Which is of course incorrect. I need each simultaneous thread to have its own copy of this variable for consistent results.

As far as I know, unlike storing the balance as a class (instance) variable, storing it as local and assigning to session attribute is thread-safe. But if that is so, why do the threads update one another's variable? What is coded incorrectly?

Thanks a lot!
 
Rancher
Posts: 3604
39
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First off, some architecture.

The servlet executes on the server, not the browser, so "browser windows running the servlet" is incorrect phrasing, and would lead to incorrect conclusions about what's happening.
There is just the one servlet on the server, and each request sent uses that in its own thread.

That's part 1.

Part 2 is that a web app session is created for each request from a unique browser.  I say that, because part of the way the session is recognised is (quite often) via a cookie, and a single breed of browser generally has only one cache for the current logged in user (that is, user of the PC/Mac/whatever the browser is running in).

So, what you are seeing is likely to be lots of browser windows open, however they are all using the same session if they are all (eg) Chrome.

One way around this is to have multiple browsers on your machine (Firefox, Explorer, Safari, opera), each of which will store their cookies in a different place.
 
Yevgeni Duvenage
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave, thank you very much, you are totally correct.

The problem was indeed that one browser (Chrome in my case) shared the session between all running tabs. Shared the cookie, as you explained. I tested now with rather running the servlet in Chrome and another in Internet Explorer and they ran perfectly independently from each other, not affecting each other's "balance" variable.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!