Solved

Save record after EDatabaseError (already changed by another user)

Posted on 2010-08-13
7
798 Views
Last Modified: 2013-11-23
Hi,

Is there a way to post a record even when it was just updated by another user. Normally one would get an EDatabaseError (Can't find row for update).

I found some code to handle the synchronisation with the dataset, but that doesn't save the new updated values.

// Added to OnPostError-event
with ADODataset1 do 
begin
  UpDateCursorPos; {MUST ALWAYS DO!}
  RecordSet.Resync(adAffectCurrent, adResyncAllValues);
  ReSync([]); {May not be needed}
end;

Open in new window


Is there a way to save the last update from this point on anyway?
I've tried Action := daRetry; but then the error keeps popping up.
0
Comment
Question by:Delphiwizard
7 Comments
 
LVL 1

Accepted Solution

by:
soscpd earned 167 total points
ID: 33436657
Hi

Use Try / Except. If Try fails, Save the record on Except, like:

Try
  SaveRecord(record);
Except
  DoSaveRecordAnyWay(record);
End;

On debugger, you should see the error message. Alone, the application should not complain about anything.

If you like to try while error, you can even loop like

Var
 PostEd : Boolean;
Begin

PostEd := False;

While Not PostEd do
Try
  if SaveRecord(record) = 1 then
    PostEd := True
  else
    PostEd := False;
Except
  DoSaveRecordAnyWay(record);
  PostEd := False;// For sure
End;

Of course, that's insane. A single broken database connection can gift you a endless loop.

I suspect you have a design issue here. What database are you using? Why both users can't work at the same time? Do they need to update the record? Can't they insert and you pick the last (updated) from a select max on a timestamp field?

If you like to elaborate a little bit, maybe we can find another way to handle that then shut the EDatabaseError up and post anyway.

Regards
Rafael
0
 
LVL 2

Assisted Solution

by:markusventer
markusventer earned 167 total points
ID: 33436909
set Cursorlocation to clUseServer on your dataset
0
 

Author Comment

by:Delphiwizard
ID: 33438121
soscpd:
I discovered that trying to save the record at all costs might not be a good idea. The record might be deleted by the other user, and therefore never accessible for any update. Probably a better way would be to abort the post and refresh the dataset. Any changes will be lost then. Maybe CursorLocation = clUseServer might solve the inaccessable row.
markusventer:
I'll do some testing with CursorLocation = clUseServer
Do you also have any experience with CursorType? I'm thinking of using ctDynamic instead of ctKeySet. According to the help, this would make added and deleted records know to other users. Any suggestions for usage of one or the other?
 
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 37

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 166 total points
ID: 33440499
there will allways be a problem with duplicate updates >> who wins ?

if you ever solve this problem, let me know, i'm very interested
only if it's a works in all cases solution :)

which i'm about 100000000000000000% sure doesn't exist.

a software lock (notifying the user someone else is going to or will change the record) is the only solution i know of
but it doesn't allways work for 100%
people allways ask for a overrule, or i'm the boss, so i should have priority ...
0
 

Author Comment

by:Delphiwizard
ID: 33440967
One solution I'm  now building is to check the actual database (by additional query) to see if the selected record has changed in comparison to the details on screen.
This check can be done just before an update is posted. As I have only one table that is troubling me with regard to this issue, I can be pretty sure I got the 99,9%. Basically it would be very uncommon that two users would change the same record (in this case events in a scheduler). But even than I can fallback on this check. Also I build a mechanism with timer to regulary update the scheduler every xx seconds.
Deleted records that were still on screen by an other user, can be handled the same way. Just check if it is actually still in the database, if not, abort transaction and refresh the table/scheduler. There are a lot of checks to perform to cover all situations, but I believe I got it covered now.
0
 

Author Comment

by:Delphiwizard
ID: 33440983
Setting cursorLocation on the ADOQuery to clUseServer didn't work for me. I got all kinds of errors (like: Database doesn't support bookmarks, which is strange as I'm using SQL Server 2008, I would imaging that it will have support for that??). Anyway being as far as I am with programing it would mess up the due-date of my project to figure that part out (and everything would need re-testing also).
0
 

Author Closing Comment

by:Delphiwizard
ID: 33440992
All your suggestions put me in the right direction, thank you all.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 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

12 Experts available now in Live!

Get 1:1 Help Now