Solved

Read Data before Commit ?

Posted on 2010-09-06
6
679 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 500 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.

839 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