473,387 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Static Class - 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
7 4469
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;
Do NOT do that, as all users will share the same "Person" instance!
And I doubt that that is what you want.
Use Session to store personal data.

Hans Kesting
Jul 19 '07 #2

Session is a lot less "expensive" than a static variable. Sessions
expire and get cleared out, statics don't (not until aspnet recycles).

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Thu, 19 Jul 2007 05:26:10 -0700, "re********@yahoo.com"
<re********@yahoo.comwrote:
>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 #3
I'm not sure I'd agree with a kind of blanket statement like that, since
there is considerably more overhead in simply creating a session item than
populating a static variable. The main issue here is that the OP is using
static objects which means that every user will have the same "person" object
- which is *not* what he wants.
-- Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
bogMetaFinder: http://www.blogmetafinder.com

"Samuel R. Neff" wrote:
>
Session is a lot less "expensive" than a static variable. Sessions
expire and get cleared out, statics don't (not until aspnet recycles).

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Thu, 19 Jul 2007 05:26:10 -0700, "re********@yahoo.com"
<re********@yahoo.comwrote:
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 #4
George,

The docs say HttpSession is:

Any public static (Shared in Visual Basic) members of this type are
thread safe. Any instance members are not guaranteed to be thread
safe.

And reviewing relevant parts in Reflector don't show any cross-request
locking. If you're right, that's great, but can you back it up with a
reference?

Even if you are right about session access, if the object in the
session is a complex object then you need to sync access to it's
properties as in the OP's case.

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
On Fri, 20 Jul 2007 11:02:01 -0400, "George Ter-Saakov"
<gt****@cardone.comwrote:
>You are safe with Sessions.
Session is locked per request. Meaning even if person hit 2 buttons quickly
(for the same session) he will have second request on hold by .NET untill
first finished.

So you do not need to sync access to objects you keep in Session.

George.
Jul 26 '07 #5
"if the object in the session is a complex object then you need to sync
access to it's properties as in the OP's case."

ASP.NET guarantees you that only one thread at a time will access Session,
hence the object that is kept inside of the Session.
So really no need to sync access.
-------------------------------------------------
As far as cross-requests and reflection just do a Google search "ASP.NET
session locking"
you will come across bunch of articles.

One of them
(http://odetocode.com/Blogs/scott/arc...5/20/3648.aspx )
say
"To prevent two pages from modifying in-process Session variables at the
same time, the ASP.NET runtime uses a lock. When a request arrives for a
page that reads and writes Session variables, the runtime acquires a writer
lock. The writer lock will block other pages in the same Session who might
write to the same session variables. "

I bet you can find similar thing in MSDN. Actually Session locking was
there forever from plain asp.
George


"Samuel R. Neff" <sa********@nomail.comwrote in message
news:d3********************************@4ax.com...
George,

The docs say HttpSession is:

Any public static (Shared in Visual Basic) members of this type are
thread safe. Any instance members are not guaranteed to be thread
safe.

And reviewing relevant parts in Reflector don't show any cross-request
locking. If you're right, that's great, but can you back it up with a
reference?

Even if you are right about session access, if the object in the
session is a complex object then you need to sync access to it's
properties as in the OP's case.

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
On Fri, 20 Jul 2007 11:02:01 -0400, "George Ter-Saakov"
<gt****@cardone.comwrote:
>>You are safe with Sessions.
Session is locked per request. Meaning even if person hit 2 buttons
quickly
(for the same session) he will have second request on hold by .NET untill
first finished.

So you do not need to sync access to objects you keep in Session.

George.

Jul 26 '07 #6
PS: Actually found it on MSDN
http://msdn.microsoft.com/msdnmag/issues/05/09/WebQA/

The last question on the bottom

George.
"George Ter-Saakov" <gt****@cardone.comwrote in message
news:uJ*************@TK2MSFTNGP04.phx.gbl...
"if the object in the session is a complex object then you need to sync
access to it's properties as in the OP's case."

ASP.NET guarantees you that only one thread at a time will access Session,
hence the object that is kept inside of the Session.
So really no need to sync access.
-------------------------------------------------
As far as cross-requests and reflection just do a Google search "ASP.NET
session locking"
you will come across bunch of articles.

One of them
(http://odetocode.com/Blogs/scott/arc...5/20/3648.aspx )
say
"To prevent two pages from modifying in-process Session variables at the
same time, the ASP.NET runtime uses a lock. When a request arrives for a
page that reads and writes Session variables, the runtime acquires a
writer lock. The writer lock will block other pages in the same Session
who might write to the same session variables. "

I bet you can find similar thing in MSDN. Actually Session locking was
there forever from plain asp.
George


"Samuel R. Neff" <sa********@nomail.comwrote in message
news:d3********************************@4ax.com...
>George,

The docs say HttpSession is:

Any public static (Shared in Visual Basic) members of this type are
thread safe. Any instance members are not guaranteed to be thread
safe.

And reviewing relevant parts in Reflector don't show any cross-request
locking. If you're right, that's great, but can you back it up with a
reference?

Even if you are right about session access, if the object in the
session is a complex object then you need to sync access to it's
properties as in the OP's case.

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
On Fri, 20 Jul 2007 11:02:01 -0400, "George Ter-Saakov"
<gt****@cardone.comwrote:
>>>You are safe with Sessions.
Session is locked per request. Meaning even if person hit 2 buttons
quickly
(for the same session) he will have second request on hold by .NET untill
first finished.

So you do not need to sync access to objects you keep in Session.

George.


Jul 26 '07 #7

Thanks for the links.

Best regards,

Sam
On Thu, 26 Jul 2007 14:59:04 -0400, "George Ter-Saakov"
<gt****@cardone.comwrote:
>PS: Actually found it on MSDN
http://msdn.microsoft.com/msdnmag/issues/05/09/WebQA/

The last question on the bottom

George.

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
Jul 27 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Irmen de Jong | last post by:
Hi, I've developed the Metaclass below, because I needed a way to make a bunch of classes thread-safe. I didn't want to change every method of the class by adding lock.aqcuire()..lock.release()...
6
by: Kumar | last post by:
Hi, If i am using static variable in a function, in say a dll. If that dll is being used by a multi-threaded program, will the static variable be safe to use? I mean, will it have the problems...
1
by: Oleg Ogurok | last post by:
Hi all, I've just realized that static data are not thread safe because they are shared by all users of the ASP.NET application. What if a static method is passed an object variable? Do I still...
28
by: Protoman | last post by:
I'd like to now if this smart pointer class if thread-safe: Code: // COPYRIGHT CMDR DOUGLAS I. PEREIRA 07/10/06 // ALL UNAUTHORIZED THIRD PARTY USE IS PROHIBITED // I HAVE WORKED VERY HARD...
1
by: =?Utf-8?B?YmJn?= | last post by:
If many thread needs to test if one field of static class is null, if (StaticClass.fieldA == null) will it be thread safe and doesn't need thread synchronization? Bob
4
by: Angus | last post by:
Hello I have written a singleton class like this: //--------------------------------------------------------------------------- // Instance...
7
by: Abhinay | last post by:
Hi there, I am developing server in java which accept client request in multi threaded fashion and response back, each client request will be process by separate dedicated server thread. I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.