Solved

Autoinc key fields in a Paradox multiuser database - URGENT

Posted on 2000-05-09
8
363 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
[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
  • 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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
 

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

Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

627 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