473,396 Members | 1,945 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,396 software developers and data experts.

Concurrent database updates

Hi,

What is the best way to resolve following problem in a multi-user
environment ?

I have a table called TOTAL_SALES.
The table has 2 columns : "CarId" and "TotalSales"

I need to access a given car in the TOTAL_SALES table using the "CarId",
read the "TotalSales" value and increment this value with 1 unit.
Then, the "TotalSales" value will be used in other calculations. Another
user who accesses the TOTAL_SALES table will use the NEW "TotalSales" value.

However, there is a concurrency problem. When 2 users access the
TOTAL_SALES table at the same time. Both can read and use the same
"TotalSales" value before the other one incremented the value. Whta is the
best way to solve this in ADO.NET ?

Thanks

Jill
Nov 18 '05 #1
5 1430
Tell us what database you are using. Your options are significantly
different depending on which database you are using.

GH

"Jill Graham" <ji**********@yahoo.com.au> wrote in message
news:Of*************@TK2MSFTNGP10.phx.gbl...
Hi,

What is the best way to resolve following problem in a multi-user
environment ?

I have a table called TOTAL_SALES.
The table has 2 columns : "CarId" and "TotalSales"

I need to access a given car in the TOTAL_SALES table using the "CarId",
read the "TotalSales" value and increment this value with 1 unit.
Then, the "TotalSales" value will be used in other calculations. Another
user who accesses the TOTAL_SALES table will use the NEW "TotalSales" value.
However, there is a concurrency problem. When 2 users access the
TOTAL_SALES table at the same time. Both can read and use the same
"TotalSales" value before the other one incremented the value. Whta is the best way to solve this in ADO.NET ?

Thanks

Jill

Nov 18 '05 #2

Actually, your database should be structured differently.
Values that can be calculated should not be stored in a table, but
calculated when needed.
You should have a 'transactions' table that would have columns for the carid
and whatever other info you need on a sale.
There would be one record for each sale.
Then whenever you need the total sales count you can obtain it by getting
the count of records in the transactions table for that carid. It may seem
simple to just add to a stored value, but ultimately the methodology is
prone to inaccuracy.

--

Alphonse Giambrone
Email: a-giam at customdatasolutions dot us
"Jill Graham" <ji**********@yahoo.com.au> wrote in message
news:Of*************@TK2MSFTNGP10.phx.gbl...
Hi,

What is the best way to resolve following problem in a multi-user
environment ?

I have a table called TOTAL_SALES.
The table has 2 columns : "CarId" and "TotalSales"

I need to access a given car in the TOTAL_SALES table using the "CarId",
read the "TotalSales" value and increment this value with 1 unit.
Then, the "TotalSales" value will be used in other calculations. Another
user who accesses the TOTAL_SALES table will use the NEW "TotalSales" value.
However, there is a concurrency problem. When 2 users access the
TOTAL_SALES table at the same time. Both can read and use the same
"TotalSales" value before the other one incremented the value. Whta is the best way to solve this in ADO.NET ?

Thanks

Jill

Nov 18 '05 #3
What I would do is (Explained with SQL Server)

Begin Transaction
UPDATE TOTAL_SALES SET TotalSales = TotalSales + 1
WHERE CarId = 101
SELECT TotalSales FROM TOTAL_SALES WITH (NOLOCK)
WHERE CarId = 101
COMMIT TRANSACTION

In ADO.NET make sure that you set the Transaction IsolationLevel to
ReadCommitted.

Regards,

Trevor Benedict R
MCSD

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #4
Hi,

Thanks for your answer. But could you explain me what happens between the
UPDATE and the SELECT statement ? Is the record locked ?

What happens when another user changes the TotalSales value before the
server executes the SELECT statement ?

Jill

"Trevor Benedict R" <tr********@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
What I would do is (Explained with SQL Server)

Begin Transaction
UPDATE TOTAL_SALES SET TotalSales = TotalSales + 1
WHERE CarId = 101
SELECT TotalSales FROM TOTAL_SALES WITH (NOLOCK)
WHERE CarId = 101
COMMIT TRANSACTION

In ADO.NET make sure that you set the Transaction IsolationLevel to
ReadCommitted.

Regards,

Trevor Benedict R
MCSD

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 18 '05 #5
Since you are within a Transaction, the record will be locked for
Update/Delete/Select till you either Rollback or Commit the Transaction.

Using SQL Server as an example, there is a method to perform a dirty
read, meaning not to wait for the lock but to read the state of the
value as is in the database, in our case the previous value. So when you
need accurate values, in the sense that users should always see the
latest value, then we go by the lock. So we wait for one user to release
the lock, then take that value and start working from there. This will
be streamlined process. Only glitch is the waiting time which should be
nano seconds, provided you keep the Transaction as small as possible.

If another user is in the process of updating/incrementing the counter,
then the next call will wait for the previous call to finish.

We do an update first, so as to get an exclusive lock on the Record.
Remember that SQL Server goes by Page level locking and for high volume
updates inside a smaller table, for those queries that read from the
table, you could specify the NOLOCK clause so as to aviod waiting for
locks to be released.

Let me know if you need anymore information.

Regards

Trevor Benedict R
MCSD

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #6

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

Similar topics

11
by: Durai | last post by:
Hi All, I tested "concurrent testing" in MySQL. It works fine. But I couldn't do in PostgreSQL 7.3.4 on HPUX IPF. I got deadlock problem. I used the PHP script to update table( one script...
3
by: mgPA | last post by:
Short: How can I limit the number of concurrent logins to Access (2000) DB? Long: I seem to be having the problem discussed in previous postings of having more than 9 or 10 concurrent logins. ...
1
by: Babz | last post by:
Hi, How I can prevent concurrent updation of a records in .net. I am using Dataset which is disconnected from the data source. In this situation if two users opens the same record, modifies the...
6
by: John Smith | last post by:
I have a C++ program which appends new text to a file. Multiple instances of this program may run concurrently, hence there is a possiblity that 2 or more instances (say C1 and C2) of this C++...
3
by: Subrat Das | last post by:
Hi, I have a java application which calls a stored procedure to insert data into a table.Multiple threads of java call the same procedure at the same time. Sometimes it happens that few threads send...
2
by: mktselvan | last post by:
Hi, Existing running oracle application 11i (11.5.8) Database version is 8.1.7.4 There is any command / way to know the number of concurrent users for this application. ...
10
by: Frankie | last post by:
It appears that System.Random would provide an acceptable means through which to generate a unique value used to identify multiple/concurrent asynchronous tasks. The usage of the value under...
6
blyxx86
by: blyxx86 | last post by:
Good Morning Everyone, I am making a PHP web frontend for a MySQL database. I have finally started to create the UPDATE portions of the website and am wanting some opinions/feedback as to what...
1
by: sanjupommen | last post by:
I am in the process of exploring the possibility of providing our products on databases other than Oracle.I am able to migrate the data, procedures etc without too much effort (latest version of DB2...
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?
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
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...
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.