469,299 Members | 2,073 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,299 developers. It's quick & easy.

Static - Thread Safe?

I am developing an application using asp.net 2.0. I created all my
business objects in my app_code folder. As of right now, all my
classes are public.

In my aspx pages, I am declaring the class like so

static Person myPerson;

The static declaration is working for me, for It keeps the instance of
the class open through the lifetime of the user who is logged in.
However, I read that the reference remains open even after the user's
session, and it is probably not going to be thread safe? I want to
avoid storing it in a Session due to it being rather expensive on the
server. I could probably make the class private, and ensure that the
class is instantiated accordingly. Any suggestions from you experts?

Jul 19 '07 #1
2 2638
I believe any global object is not thread-safe by definition if you don't
synchronize access to fields/methods properly

If your Person is related to user/session, probably you would be better off
by making it in-session instance. Just destroy it when closing session and
you should be pretty safe.
<re********@yahoo.comwrote in message
news:11**********************@g4g2000hsf.googlegro ups.com...
>I am developing an application using asp.net 2.0. I created all my
business objects in my app_code folder. As of right now, all my
classes are public.

In my aspx pages, I am declaring the class like so

static Person myPerson;

The static declaration is working for me, for It keeps the instance of
the class open through the lifetime of the user who is logged in.
However, I read that the reference remains open even after the user's
session, and it is probably not going to be thread safe? I want to
avoid storing it in a Session due to it being rather expensive on the
server. I could probably make the class private, and ensure that the
class is instantiated accordingly. Any suggestions from you experts?

Jul 19 '07 #2
re********@yahoo.com wrote:
I am developing an application using asp.net 2.0. I created all my
business objects in my app_code folder. As of right now, all my
classes are public.

In my aspx pages, I am declaring the class like so

static Person myPerson;

The static declaration is working for me, for It keeps the instance of
the class open through the lifetime of the user who is logged in.
However, I read that the reference remains open even after the user's
session, and it is probably not going to be thread safe? I want to
avoid storing it in a Session due to it being rather expensive on the
server. I could probably make the class private, and ensure that the
class is instantiated accordingly. Any suggestions from you experts?
Thread safety is not a problem if you have data that is local to the
thread. A static variable however, is not local to the thread. What you
have is a single Person object that is shared by all threads, which
means that your application only can handle a single user.

Making it private makes no difference either. That only means that it's
private to the class where you declared it, not private to the thread.

If you want to store user specific data, you have to use user specific
storage, like Session variables.

--
Göran Andersson
_____
http://www.guffa.com
Jul 19 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Steve | last post: by
4 posts views Thread by Simon Harvey | last post: by
6 posts views Thread by Macca | last post: by
14 posts views Thread by Shimon Sim | last post: by
6 posts views Thread by Olumide | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.