vondip vondip - 2 months ago 4
Ajax Question

nhibernate doesn't get a chance to update?

I am building a small web application, where the user is granted the ability to rate items.
In my application I am using nhibernate and asp.net mvc.

All the rating requests are sent by jquery (ajax/post).
When the user votes an item, I check if the item has been previously voted. If so, I update the last voting value to the new one received. If not, I just add a new rating to my table.

I have noticed something very strange. This works well, but when I click several times really fast something gets screwed up. I get multiple ratings, it seems as if nhibernate doesn't bother checking if the user has previously voted and just returns a false value.

Is this possible? How can I see what's going under the hood?

thank you


You probably have a concurrency problem. I assume that you get a thread and transaction per click. Clicking very fast results in parallel transactions which can't see what others are doing.

You have a typical problem that items which aren't in the database (the new votes) can't be locked.

The solutions are:

  • Use lock to avoid multiple votes of the same user being stored at the same time. This doesn't work when you have multiple servers (or AppDomains) on the same database, because the lock is restricted to the AppDomain.
  • Use table locks in the database to lock out the whole votes table that only one transaction can add votes at the same time.