Reputation: 55
I am working on a C# standalone data entry application. This system is proposed to use by a centralized database(MYSQL 8.0) with many computers. Data will enter using the wizard. Clicking next ->next and insert data only at the end of the wizard Currently am getting the next ID by Client-side(c#)
SELECT MAX(id) + 1 AS ID
FROM TABLE_NAME
This method is ok for single-user applications. but when the database centralized and used by many computers it will crash IDs generated by this method.
Example - there are 3 (A, B) computers with installed the data entry system connected to a centralized database. assume the last ID in TABLE is 5. Computer 'A' going to add data. Computer A will get the next ID as 6. Also, Computer 'B' starting to enter data at the same time. COmputer 'B' also get the next ID as 6.Then IDs will crash.
Can anyone suggest a solution to get the next id without the crash from the server-side? Please Provide an example.
Upvotes: 1
Views: 659
Reputation: 920
The problem is that if you do try to get next ID (especially in a multi-client environment) it is not guaranteed to be the value by the time you perform the INSERT since one client could request and then another client request, both receiving the same response, and then one of the client performs the INSERT and ID matches what was returned while the other client's ID will not.
Thee 2 most basic approaches that you may want to evaluate in your given scenario are:
Upvotes: 1