Solved

Autoinc key fields in a Paradox multiuser database - URGENT

Posted on 2000-05-09
8
354 Views
Last Modified: 2008-02-01
I have a program that runs on four computers, using the same database. I've set the NetFileDir and PrivFileDir properties to the correct places on the net.

When one station appends a record on a table at the same time another station is appending a record on the same table, both get the same value for the autoincrement field. As it is part of the key, I got a Key Violation.

The AfterPost event of all my tables is calling the FlushBuffers method, and CachedUpdates is False. The sequence to include records is:

  - call the tables's Refresh method
  - Append
  - Post
  - FlushBuffers

I'll try to use LockTable (read and write), but I'm not sure it will work. I've heard about the BDE OnChangeNotify, but don't know if it can help.

I need an urgent answer, because I must end this work in two days!

Please help.

Thanks
0
Comment
Question by:mazzini
  • 5
  • 3
8 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2793551
hi mazzini,

that would be difficult, because the next autoincrement-value is part of the table header and will be read and increased just as the record will be inserted.

well, its a very short time, where it can be happen,
that two clients retrieved the same autoinc-value.

if it appears often then review the netdir and private-dirsettings
to following rules:
- netdir-entry -> must be char by char the same on each client
- privatedir-entry -> must be physical different for each client

if it appears sometimes,
then i would recommend to do a autoincrement byself, which a seperat table holding one integerfield, which is opened exclusive, read the value and stores the value+1 back.

another solution would be to catch the keyviolation-exception,
waiting some milliseconds and then post again.

hope this helps a bit

meikl
0
 

Author Comment

by:mazzini
ID: 2793649
meikl:

Thanks for answering quickly !!

I'll verify netdir/privatedir, test again and tell what happened.

Your tip of using a separate table to implement an autoinc by myself sounds to be a solution. I'd prefer to use autoinc fields at first, but if I can't get a solution in a short period of time, I'll try your tip.

Thanks again.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2794082
hi again mazzini,

just a sample, how it could be done, if you do the autoinc byself
(in this case you must restructure your Table(s) to set the autoinc-field to a LargeInteger-field before)
(delphi code here)

//Function for Retrieving the next Value
//the physical table, where
//table2 is pointed to, holds only one record with one integerfield
//initial prefilled with the value 1
//Three tries to open the table exclusive

Function TForm1.Get_Next_Id : Integer;
const Retries  = 3;
var
  OK : Boolean;
  Retry : Integer;
begin
  OK := False;
  Retry := 0;
  Result := -1;
  Table2.Exclusive := True;  //Open Exclusive
  while (not ok) and (Retry < Retries) do
  begin
    try
      Table2.Open;
      Result := Table2.Fields[0].AsInteger;
      Table2.Edit;
      Table2.Fields[0].AsInteger := Result + 1;
      Table2.Post;
      Table2.Close;
      Ok := True;
    except
      Inc(Retry);
      Sleep(100);  //wait a bit, adjustable if to short
    end;
  end;
  If Not ok then
    Raise Exception.Create('Problem Getting ID-Value');
end;

//Sample to call
//Just in the BeforePost-event
procedure TForm1.Table1BeforePost(DataSet: TDataSet);
begin
  If Table1.State = dsInsert then
  try
    Table1.FieldByName('Id').AsInteger := Get_Next_Id;
  except
    ShowMessage('Insert Failed :-(');
    Abort;
  end;
end;

just if you need to go this way

meikl
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2798955
hi mazzini,

any results?

just forgot to say, that the netdirfile must be on a shared directory, where all clients have access to it.

meikl
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:mazzini
ID: 2801077
Hi meikl,

sorry for the silence, but I was busy.   :-)

Since I don't have much time for test, I will use your routine in place of all the autoinc fields I'm using now.

After correcting the use of NetFileDir, as you have pointed, I used the sequence with LockTable (read/write) / Refresh / Append / Post / UnlockTable (write/read) and all runs properly, but I don't have time to test it hardly. So, since I don't want to put my neck on it, I will use your routine in place of autoinc fields.

If you post an answer, the points are yours. And, once again, thanks for your tips. They really helped!

Best regards
Mazzini
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2801848
hi mazzini,

just to say that the softwaresolution is a workaround,
because if paradox properly configured, then it will be hard
to reproduce your problem, but possible.

if you have further problems (i don't hope this),
then contact me via email
meikl@spektracom.de
or post it in this q
(i let the notification-mark checked here)

well, glad to helped you
good luck again

meikl ;-)
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 1500 total points
ID: 2801851
oops, was meant as an answer ;-)
0
 

Author Comment

by:mazzini
ID: 2802274
One more time, thanks. I will keep your e-mail, but hope not to annoy you too much ;-)

Mazzini
mazzini@via-rs.net
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction: Often, when running a query with joins, the results show up "duplicates", and often, those duplicates can be "eliminated" in the results using DISTINCT, for example. Using DISTINCT is simple: just add it after the SELECT keyword, an…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

11 Experts available now in Live!

Get 1:1 Help Now