I am generating userId manually i.e. previous userId+1.
but if two or more users reading previous userId and inserting new record.
So,there is a possibility that, any two or more users inserting record with same userId
I am generating userId manually i.e. previous userId+1.
but if two or more users reading previous userId and inserting new record.
So,there is a possibility that, any two or more users inserting record with same userId
You can put reading the previous userid and inserting the record in the same transaction.
The transaction must be set to use the proper Isoltaion level: 'Serializable' or 'Repeatable reads' at least.
Following this approach, the increment of the userid will fall after the begining of the transaction and before the commit.
The RDBMS will take care of race condition.
You should queue somehow your processes and make sure only one is processed in a given time.
The easiest way is to lock something before the select previous userid
query, and release the lock after you inserted already the record.
Otherwise this is not a good practice. RDBMS systems have better solutions for this. Search for sequence, identity or auto_increment in your RDBMS's documentation.