473,394 Members | 2,048 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,394 software developers and data experts.

Critical Sections and Static Methods

We have an abstract class with all static methods. It makes sense to have
it static because there are no member variables and the constructor is
empty.

Some of the methods update the SQL Server database and we were wondering if
there would be a problem if two threads called that method at the same time.
Would it corrupt the database?

I was thinking about locking the code on entry and unlocking it on exit.

My question is, would this work in an ASP.NET environment? Do the pages
access the same exact instance of the static classes? So, if User A
accesses the update code first and soon after, User B tries to access it,
User B will have to wait for A to unlock the code.

Thanks,

blue
Nov 18 '05 #1
2 1484
it depends on if the code your wrote was thread safe. yes two threads can
call the exact same code address (there is no instance).

if your class has no static variables (only methods), and the parameters
passed are instance variables you should be ok.

-- bruce (sqlwork.com)
"blue" <bl**@arizona.edu> wrote in message
news:en**************@TK2MSFTNGP10.phx.gbl...
We have an abstract class with all static methods. It makes sense to have
it static because there are no member variables and the constructor is
empty.

Some of the methods update the SQL Server database and we were wondering if there would be a problem if two threads called that method at the same time. Would it corrupt the database?

I was thinking about locking the code on entry and unlocking it on exit.

My question is, would this work in an ASP.NET environment? Do the pages
access the same exact instance of the static classes? So, if User A
accesses the update code first and soon after, User B tries to access it,
User B will have to wait for A to unlock the code.

Thanks,

blue

Nov 18 '05 #2
In case 1, static method does not access a shared resource.

In ASP.NET, if UserA and UserB calls the same static method of a
class, both threads can execute the static method at the same time
without blocking each other.

In case 2, static method does access a shared resource.

This will depend on if the shared resource is thread safe, and how
your code access the resource. Since the database is thread safe, it
will depend on how you implement the code.

If the static method access the database using only local variables,
then you won't need to lock because when ASP.NET switches between the
threads, it'll save the local variables on the thread's stack. For
example, you have local SQLConnection and SQLCommand objects.

However, if the static method uses static variables to access the
database, then you will have to lock it. Otherwise, the first thread
could close the database connection while the second thread is
accessing the database. For example, if you use a static SQLConnection
or SQLCommand object, then the object will be shared between the two
threads.

Tommy,

"blue" <bl**@arizona.edu> wrote in message news:<en**************@TK2MSFTNGP10.phx.gbl>...
We have an abstract class with all static methods. It makes sense to have
it static because there are no member variables and the constructor is
empty.

Some of the methods update the SQL Server database and we were wondering if
there would be a problem if two threads called that method at the same time.
Would it corrupt the database?

I was thinking about locking the code on entry and unlocking it on exit.

My question is, would this work in an ASP.NET environment? Do the pages
access the same exact instance of the static classes? So, if User A
accesses the update code first and soon after, User B tries to access it,
User B will have to wait for A to unlock the code.

Thanks,

blue

Nov 18 '05 #3

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

Similar topics

8
by: Paul Morrow | last post by:
I like many am not wild about the <at> operator. I also don't think that the decorator syntax should be so directly attached to the method, since what we're trying to do is to say something about...
5
by: Jon Paugh | last post by:
Is there a standard for where to put the different categories of code in a class file? For example, after the class declaration, normally I put class variables, then constuctor, then private...
77
by: Charles Law | last post by:
Hi guys I have a time critical process, running on a worker thread. By "time critical", I mean that certain parts of the process must be completed in a specific time frame. The time when the...
2
by: Xarky | last post by:
Hi, I am trying to learn Critical Sections. I have written a small program. Source code problem below. What the program is doing is disabling the CRTL-C signal in the critical section. My...
7
by: Piotrek Stachowicz | last post by:
Hi, I need to create the situation in my system, where no more critical sections can be initialized (win2000Server). I thought about creating a simple c# application and using the Monitor class....
6
by: MPH Computers | last post by:
Hi I am looking for some help on Threading and Critical Sections I have a main thread that controls an event the event handler creates a new thread for carrying out the work because the...
13
by: Hendrik van Rooyen | last post by:
Hi, I would like to do the following as one atomic operation: 1) Append an item to a list 2) Set a Boolean indicator It would be almost like getting and holding the GIL, to prevent a...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.