I have created a multi-tenant application in which users creates record which has an unique number like a primary key. Every record that is added to the system increases that unique number and labels itself with that number.
My problem is, when two users try to insert a record at the same time, I get the same unique number. I want that number to be unique.
I tried to work with threads, assumed the second thread will wait for the first one to get the correct number and continue. But it didn't go as I planned.
I have also tried table triggers. When a new record inserted, I just updated the field with the latest unique ID. But still when two record comes at the same time, got same unique ID.
I expect to create unique id column, like a primary key. I understand that it could be done with in database or code.