• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

thread safety issue.......

 
lusha tak
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all!!
what u have to say about threads safety in the folowing code sample, as my applcation is running fine if accessed by a single user but causes problems such as reslultset is closed or invalid cursor state....etc.(if accessed by multiple users concurrently..)
please help!!!
Lusha

[This message has been edited by lusha tak (edited September 23, 2001).]
[This message has been edited by lusha tak (edited September 23, 2001).]
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So typically only a single instance of a servlet (or jsp) will created for each page. This means that there is only a single copy of the instance variables. If multiple people hit a single page, multiple threads will be allowed to access that single instance of the servlet class.
Fixing the thread-safety of your code is as simple as changing this:

to this:

In the first version, a person will get a db Connection, start a statement etc, then a second person will come along and overwrite the first first person's Connection.
In the second version, each person accessing the page will get their own version of these variables and they won't interact.
Dave.
 
lusha tak
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank David....
i will make the changes, the way u have described... one more thing...how do i ensure the same level of independence among various users in my servlets...
i mean, what refers to servlets as this (<%! declarations %> ) in jsp pages..
Thanks for ur precious time...
lusha
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand your question correctly, you are asking what this looks like in Servlets?
Any instance variable.
Therefore this is not thread-safe:

If you absolutely positively have to use local variables, you either have to handle synchronization issues yourself or have a look at SingleThreadModel.
Dave.
[This message has been edited by David O'Meara (edited September 24, 2001).]
 
lusha tak
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot, David!!
Lusha
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic