Subject Re: generator of generators
Author duilio_fos <irel_llc@libero.it>
> I don't know too many Italians but it couldn't hurt my vocabulary
to learn a few choice words. :)

one for all: "stronzo" means "asshole".

You can travel all over Italy, saying "stronzo" to everybody you meet.

Nobody will stop you. You will be a winner in every situation. :)

>It's not exactly high-tech, but it works in some situations like
>this.

it is the dear old solution of dbase times.

I have a very special code somewhere...where is it...there is it!

function TdEmployee.NewPgr:longint;
const
ntMaxTries = 100;
var
NumTab:TTable;
WaitCount,i,Tries : Integer;
begin
NumTab:=Exp.TabArr[Ord(TB_NUM)];
with NumTab do
try
Randomize;
Tries := 0;
while Tries < ntMaxTries do
try
Inc(Tries);
Open;
Edit;
Break;
except
on EDBEngineError do
{The call to Edit failed because the record could not be
locked.}
begin
{Wait for a random period and try again.}
WaitCount := Random(20);
for I := 1 to WaitCount do
Application.ProcessMessages;
Continue;
end;
end; {try}
if State = dsEdit then
begin
result:=FieldByName('Progressive').AsInteger;
Inc(result);
FieldByName('Progressive').AsInteger:=result;
Post;
end
else
{If the record could not be locked after the
specified number of tries raise an exception.}
raise Exception.Create('Cannot get next unique number.');
finally
Close;
end;
end;

(of course the table structure has to be changed according to your
suggestion, but the principle is clear).

> Of course, the one drawback is that you have to build an
administrator function that can clear the locks in case someone
crashes after posting the Locked field.

...without counting the horrible time degradation.

I guess this solution is better: I create say 100 generators in the
database, named

GEN_1...GEN_100

When a new company registers, it is given a unique ID, say I (the
number is provided by a different generator).

The client application will know the company ID and will use the
right generator GEN_I to uniquely identify records.

I don't need to create generators on the fly, because they are
already there, waiting for someone to use them.

When the number of registered companies grows close to 100 (what is
unlike), I will add

GEN_101...GEN_200

What do you think ?

Thank you

Duilio