Let's say I have a table called items
. User of my webapp can delete row of the items
table, but I don't want to let the table empty.
So currently I have code like this in my application:
if (itemsCount() <= 1) {
don't delete;
}
else {
delete;
}
But I realize this code is vulnerable to concurrency problem. For example if currently the size of items
is 2, and there are two thread executing this code at almost the exact same time, the table might become empty.
I think this problem is pretty common for people writing webapps. People should've already solved it. What are the available solutions for this?