Solved

Transactions and CORBA

Posted on 2000-05-18
3
224 Views
Last Modified: 2010-04-04
I have corba server which is connected to a database and
I use that database trough the client program.
I can make queries, updates etc. but now I have the
classic update problem.

A gets record:
'SELECT FIRSTNAME FROM TABLE WHERE ID = 1000'

B gets same record:
'SELECT FIRSTNAME FROM TABLE WHERE ID = 1000'

After this B updates and the A updates.
B's update is lost. Of course B shouldn't
be able to update, because A got the
record first.

How do I solve this problems in Delphi?
I didn't get any errors when made the
SQL query with A and then update with B

I'm using following strategy:

E.g. Get list of users
1. Call method GetUsers(userlist: TList), which is method of my DBHandler
class.
2. Create SQL query and execute it.
3. Loop trough resul, create TUser objecst
and add them to the userlist.
4. Show the users in some component e.g.
in TListview component.

I tried to use TDataClientSet.ApplyUpdates(0);
and CancelUpdates, but I only get "Unexpeceted
Error" with following code:

name := 'foo';
CorbaConnection1.Connected := True;
ClientDataSet1.Close;
sql := 'UPDATE TABLE SET FIRSTNAME = ' + '''' +
nimi + '''' + ' WHERE ID = 1000';
ClientDataSet1.CommandText := sql;
ClientDataSet1.Execute;
ClientDataSet1.CancelUpdates //unexcepted error here

Execute goes ok, so there is nothing wrong in the SQL.

It seems that I can use CancelUpdates/ApplyUpdates
only if I'm using some database with DBGrid.
Now the Execute command makes all the changes
in the database right away.

I'm using Solid Server as a database server.
0
Comment
Question by:tonitop
3 Comments
 
LVL 9

Expert Comment

by:ITugay
Comment Utility
hi tonitop,

Yes it's classical update problem. You need to prevent another users to get record for editing if one of them already get it. You need to have additional field to keep information about locks. It may be integer field, let its name be IDL. Normally it contain "0".

1. try mark record as locked
"update thetable set IDL=:id_user where ID=1000 and IDL=0"
nothing happen if another user "lock" it because of IDL<>0 in this case

2. get for editing
"select * from thetable where IDL=:id_user and ID=1000"
if query returns empty result then another user get record for editing

3. if previous action is success then make your changes and "unlock" record
"update thetable set IDL=0 where ID=1000"


It's usual way to mark record as locked in not locks supprted database.

----
Igor.
0
 

Accepted Solution

by:
lorenk earned 100 total points
Comment Utility
ToniTop,
The answer is a little more complex than what you might think.  First off, I would suggest NOT using the ClientDataSet objects.  These ar ejust not meant for these types of transactions.  I have created many 3-tier apps and never used them.  The appropriate way to do this is to keep the locking within the CORBA object itself.  This of course means that ALL apps that touch the database NEED to go through the CORBA object.  This makes sense though.  So, you keep a log of what apps have which records open and make sure that only the one with the "lock" makes the change stick.   This way, you can also update all clients IMMEDIATELY when the "locked" client makes a change.  And then a different client can get the "lock."

The log I am referring to in my apps is usually a TLIST of objects which contain the ID of the client and the ID of the record they are accessing.

This is the best way to do locking IMHO.  I never like to rely on the DB to do it.

Loren
0
 

Author Comment

by:tonitop
Comment Utility
Answer accepted
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now