?
Solved

Read Data before Commit ?

Posted on 2010-09-06
6
Medium Priority
?
705 Views
Last Modified: 2013-12-19
Hi I have a situation as follows

TABLE MONTHLYBILL [ NO INSERT, ALWAYS UPDATE ONLY]
MONTHLYBILL_ID [PK]
MEMBER_ID
AMOUNT


TABLE MONTHLYBILLDETAIL [INSERT / UPDATE]
MONTHLYBILLDETAIL_ID [PK]
MONTHLYBILL_ID [FK]
LEDGER_ID
AMOUNT



In a multi user environment, let us say user1 is trying to do as follows

1      UPDATE MONTHLYBILL SET AMOUNT = AMOUNT +100 WHERE MEMBER_ID=1
2      INSERT INTO MONTHLYDETAIL LEDGER_ID=1  AMOUNT= AMOUNT +75
3      INSERT MONTHLYDETAIL LEDGER_ID=1 AMOUNT = AMOUNT + 25


In a multi user environment, let us say user2 is trying to do as follows

1      UPDATE MONTHLYBILL SET AMOUNT = AMOUNT +100 WHERE MEMBER_ID=1
2      INSERT INTO MONTHLYDETAIL LEDGER_ID=1  AMOUNT= AMOUNT +75
3      INSERT MONTHLYDETAIL LEDGER_ID=3 AMOUNT = AMOUNT + 25

So in case 1, line 3, it should actually update since LEDGER_ID =1 is already present.

In case 2 lie 2, it should update since user1 has aleady inserted LEDGER_ID=1 but line 3 should be inseted since LEDGER_ID=3 is not present.

When so many users are operating how do i ensure that this logic works correctly ?



0
Comment
Question by:GRChandrashekar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 1

Expert Comment

by:XMarshall10
ID: 33610207
If I have understood ur issue correctly, Irrespective of the fact that you are directly programming in the back end (using TSQL) or in the Front end (using C#), you should check the existing value in the LEDGER_ID field, and then decide whether to INSERT or UPDATE.

Also, in a multi user environment, using Multithreading approach from the Front end may be ideal using individual worker threads, and using thread synchronization for managing simultaneous DB operations.

-XM.
0
 

Author Comment

by:GRChandrashekar
ID: 33610252
Yes Problem is understood correctly. It is winform application with C# framework 2.0 Mutlithreading does not arise and it this action is performed from one form
0
 

Author Comment

by:GRChandrashekar
ID: 33610299
But remember, checking LEDGER_ID when data is not commited
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33610437
You should put all the DB logic into a Stored Procedure and also use Transactions (with locks) so that the in case of multi-user scenario no matter the order of their invocation the processing is always sequential.
0
 
LVL 15

Accepted Solution

by:
Franck Pachot earned 2000 total points
ID: 33610783
Hi,

You can use the MERGE statement instead of choosing INSERT or UPDATE.
If not, you will have to SELECT FOR UPDATE to check if the row exists, then choose to UPDATE or INSERT.
But as a concurrent insert can happen at the same time, you must catch the primary key constraint violation error when you INSERT, and then you will UPDATE.

Regards,
Franck.
0
 
LVL 3

Expert Comment

by:htonkov
ID: 33613525
Hi!
You might write a trigger on detail table to update AMOUNT in master table acording to value inserted in detail...

Something like:

create or replace trigger BLAH
before insert or update on MONTHLYBILLDETAIL
begin
 update MONTHLYBILL set amount=amount+nvl(:new.amount,0)-nvl(:old.amount,0);
end;

But instead of inserting/updating/merging into detail table I'd personally use a procedure to handle that,
use simple MERGE inside it, but in separate procedure, just to to handle tricky code on separate location...

Regards
Hrvoje
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that useā€¦
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question