0

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?

Teddy
  • 95
  • 1
  • 7

1 Answers1

0

The most common solution is to use a Transaction Manager. In your case, the Transaction Manager would coordinate the thread execution to make sure that only one thread at a time access and updates the table.

You didn't mention which language and which kind of environment you are using, but assuming Java and JEE, transaction management is quite easy. Start here.

forty-two
  • 12,204
  • 2
  • 26
  • 36