[Gambas-user] How to create global locks (or mutex) in a database by using a specific table (Was Re: R: Re: A question about serial (autoincrement) mysql fields)
Benoit Minisini
gambas at ...1...
Thu May 15 17:50:39 CEST 2008
On mardi 13 mai 2008, doriano.blengino at ...1909... wrote:
>
> What do you mean with a lock record? Perhaps rely on a key violation
> or something similar?
Yes.
> And what about transactions?
> I am sorry to bother you about extra-gambas things, but your help
> would be appreciated (and gambas is involved, anyway).
>
> Thanks for your reply - have fun.
>
>
> Doriano Bengino
You can use a table to create database locks this way:
1) Create a table named "LOCK".
2) This table has, for example, one field "Name".
3) This table has an -unique- index on "Name". In other words, make "Name" the
primary key.
4) To set a lock, just create a record inside the "LOCK" table. If the
creation failed, that means the lock has been set by another client. Then you
can wait a little and try again. Otherwise, if the creation succeeds, you
have the lock, and you can continue.
5) Once you have the lock, create your record in the other table (with the
autoincrement field), read the last used autoincrement field with a SQL
request, and so on...
6) Once your job is terminated, release the lock by destroying the record
created in 3).
The primary index on LOCK ensures that the lock is set in an atomic way.
Regards,
--
Benoit Minisini
More information about the User
mailing list