Hi,
In my application, i m trying to generate a sequential and unique number for the users.
what i m doing is to use a table to store all the existing numbers, and use SQL:
to get the max number, and plus one as the next sequential number for the user. When the number is generated, the number is saved into the table, hence next time the SQL will get this number as the max number.
To avoid duplicated numbers, i added a UNIQUE constraint to the table, so the column will not accept same number to appear twice.
But there could be a problem when concurrent requests are to be processed at the same time, because SELECT for the max number and INSERT for the max number + 1 are executed in order, two transactions can get the same max number, when plus one and save, the first transaction is able to INSERT, but the second one will encounter a "duplicated value" exception.
I use two computer to do a test, and when the exception happens, it pops up a window with a warning message like "duplicated values are not accepted" something.
I m wondering if there's a way that i can catch this exception at the client side, and do something to it, e.g. regenerate a number. Is the exception in the DSResponse.getData()? and how can i get it?
Thanks!
P.S. i can't test it under hosted mode, because the URL seems to work only locally, can't be accessed from another computer
In my application, i m trying to generate a sequential and unique number for the users.
what i m doing is to use a table to store all the existing numbers, and use SQL:
Code:
SELECT MAX(num) + 1 FROM table
To avoid duplicated numbers, i added a UNIQUE constraint to the table, so the column will not accept same number to appear twice.
But there could be a problem when concurrent requests are to be processed at the same time, because SELECT for the max number and INSERT for the max number + 1 are executed in order, two transactions can get the same max number, when plus one and save, the first transaction is able to INSERT, but the second one will encounter a "duplicated value" exception.
I use two computer to do a test, and when the exception happens, it pops up a window with a warning message like "duplicated values are not accepted" something.
I m wondering if there's a way that i can catch this exception at the client side, and do something to it, e.g. regenerate a number. Is the exception in the DSResponse.getData()? and how can i get it?
Thanks!
P.S. i can't test it under hosted mode, because the URL seems to work only locally, can't be accessed from another computer
Comment