Article View: borland.public.cppbuilder.database.ado
Article #5194Re: Clarification on AutoInc fields
From: Formentz
Date: Fri, 29 Jun 2001 13:05
Date: Fri, 29 Jun 2001 13:05
44 lines
2757 bytes
2757 bytes
Hi Umberto,
I had the same problem with SQL Server and solved it with REPEATABLE READ
Transaction Isolation Level.
This isolation level doesn't let successive transactions to read from a
table that is being updated until the first transaction is committed or
rolled back.
If InterBase supports this type of transacptions you'll have only to start
the transaction, get the new value and increment the field which contains
the counter.
To accomplish a better security you should do, every time you need the
unique id :
1) start a repeatable read transaction;
2) increment the field in the counters table;
3) get the value from the counters table;
4) insert the rows you need;
5) if error rollback transaction;
6) else commit transaction.
In this way the first thing you do is to lock all the accesses to the
counters table (2) so you can safely read the new ID value (3) and insert
rows. If the insert goes ok you commit the trans, else you rollback and the
field in the counters table return to its prior value.
I tried it using 8 applications running together with a cycle that gets 400
ids and does the 400 inserts and no errors or repeated values occurred.
HTH
Ciao
Formentz
"Umberto Uderzo" <umberto@uderzo.it> ha scritto nel messaggio
news:3b3c3ad2_1@dnews...
> Hi Mark,
>
> Sure you're right about the SELECT MAX, infact this must be protected by
> some sort of lock, but let's talk about the link you posted: the solution
is
> good but i think this will be better using a separate session to compute
> only the new id. So you can use transactions normally with your
application,
> and call a function for the id that works on his own session, that can be
> opened only once for working session and into wich you can perform as many
> COMMIT as you line without affecting the main data processing...
>
> Bye... Umberto Uderzo
>
Technical Details
Message-ID:
<3b3c7c38_1@dnews>Path:
arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!nntp-relay.ihug.net!newsfeeds.ihug.co.nz!ihug.co.nz!dnewsReferences:
<3b3b532b$1_2@dnews> <3B3BD2D2.5F446F35@temporaldoorway.com> <3b3c3ad2_1@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10698b,2eab2f6830ef7732 X-Google-Attributes: gid10698b,public X-Google-ArrivalTime: 2001-06-29 06:05:22 PST From: "Formentz" <francesco@leotron.com> Newsgroups: borland.public.cppbuilder.database.ado References: <3b3b532b$1_2@dnews> <3B3BD2D2.5F446F35@temporaldoorway.com> <3b3c3ad2_1@dnews> Subject: Re: Clarification on AutoInc fields Date: Fri, 29 Jun 2001 15:05:52 +0200 Lines: 47 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4133.2400 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 NNTP-Posting-Host: 62.122.12.130 Message-ID: <3b3c7c38_1@dnews> X-Trace: dnews 993819704 62.122.12.130 (29 Jun 2001 06:01:44 -0700) Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!nntp-relay.ihug.net!newsfeeds.ihug.co.nz!ihug.co.nz!dnews