开发者

Database:Locking items for a specific period of time

开发者 https://www.devze.com 2023-03-14 17:50 出处:网络
We are planning to use hibernate framework for an application which is something like an e-commerce appli开发者_StackOverflow社区cation.

We are planning to use hibernate framework for an application which is something like an e-commerce appli开发者_StackOverflow社区cation.

We have a requirement in which, if an user checks out an item and proceeds to the gateway, we will lock the item for 7 minutes, after which the item is released.

How can we design the above requirement? Ideas are appreciated.


You probably don't want to use a database level lock for this. Most databases and database configurations are not built around the concept of long-held locks.

The most generic approach I can think of is to build some kind of locking service in your application. The locking service has synchronized methods like tryObtainLock which will obtain the lock and return true if the lock is available or return false if the lock was not available (at which point you probably send an error to the user informing them the item is locked).

The locking service can then store in its own table a list of locks, who asked for them, and when they were obtained. Every so often you can check to see if any locks have been held for over 7 minutes and if so, release them and notify the person who obtained the lock that they no longer have it.

0

精彩评论

暂无评论...
验证码 换一张
取 消