On 07-Mar-2012 13:47 , Mr. K.V.B.L. wrote:
>
> I don't know about the sequence thing, but I'll check it out. The
> database table is already defined and in production.
The sequence object is separate from the TABLE. An IDENTITY could be
defined in a separate table, but the scenario in the original message,
as further clarified and then included below, probably not a good fit.
I am not sure a sequence is either. Too little information to know for
sure.
> There are two fields for each row in this table: an account number
> and transaction number. The account number is key, so a record lock
> would work here instead of a whole table lock.
I will refer to the TABLE as TBL, the account number as AN, and the
transaction number as TN.
>>>> The basic flow of the program(s):
>>>>
>>>> SELECT a value from the table
>>>> UPDATE that value with value + 1
>>>>
>>>> so I feel that these two steps should be locked and both
>>>> completed before the next program can access the table. So you
>>>> can use this...
>>>>
>>>> LOCK TABLE <table> IN EXCLUSIVE MODE
I had originally alluded that LOCK TABLE was not a /good fit/ with
autocommit active, although the DLCOBJ CL command could remove the lock
if necessary; i.e. for lack of a COMMIT to do so [or if the LOCK were
dropped, depending on the implementation of autocommit; isolation *none
or otherwise].
I then alluded that a row lock by FOR UPDATE could be used instead
with FETCH and UPDATE WHERE CURRENT of; though unstated, like the
DLCOBJ, the work would would need to be done in a stored procedure.
The following is possible from the client... albeit like many things,
might best be done by a stored procedure on the server to limit the
activity betwixt:
pseudo-code:
attemp_count = 0
do while attempt_count < excessive_attempt_count
select TN from TBL where AN=:account
INTO :value
update TBL set TN=:value+1
where AN=:account
and TN=:value /* ensure same tuple obtained by select */
if row-not-found /* some other work already incremented! */
then increment attempt_count
else leave
end do
if attempt_count >= excessive_attempt_count
then issue error
> I'll check the SELECT FOR UPDATE thing. I've not used that before.
> The the record, I'm using SQL CLI here underneath a C++ wrapper I
> wrote myself.
With autocommit active, the work would have to be performed in just
one statement. As responded in another message just recently, and
alluded to again above, use the SQL CALL statement to invoke a stored
procedure which is defined to do the necessary work.
Regards, Chuck