2

我有一个win-form在 .net 4.0 中开发并使用SQL 2008as的应用程序DBMS

用户可以从项目列表中打开一个项目,每次打开时,我都会使用userId更新数据库,以说明特定项目是由特定用户打开的,因此其他用户无法同时打开同一个项目。此外,当用户关闭该项目时,我会将其重置为其他人可以打开的正常状态。

但是我想知道如果某些系统崩溃/卡住或类似情况如何更新数据库。有没有什么好的解决方案来处理这些情况?

4

3 回答 3

2

一种解决方案是将时间戳列添加到数据表中。每次用户访问一个项目(除了其他工作)您设置此时间戳值。

然后,您可以有一个单独的任务/服务来迭代记录并偶尔释放任何过期的锁。

于 2012-09-14T15:44:05.597 回答
1

So you're trying to implement your own locking mechanism. See how to mitigate the problems of Deadlocking that should give you an idea. Basically either you use notifications or stamps to track lock's owner status (if it's dead or still using the resource), preventing resource starvation.

Requested example

There are 2 users. User A and User B.

User A open a view within your app, then two things happen:

  • You update the DB and set the stamp to the current time.
  • You start a new background thread who will be responsible to mantain the lock. This background thread must wait N-Delta where N is the maximum minutes the resource can be locked without renew and delta is the delta for update time. So, for example, suppose a view can be locked for maximum 15 minutes without user activity, then your thread must wait 15-1 (taking that will probably take 1 minute to notify DB "better be caution with this time").

Then User B try to access to that view, you check on the db for the resource and for the lock table and if there's a lock on that view whose time isn't superior from 15 minutes then you deny access. BUT if time is most that 15 minutes you grant the access to the User B and remove the access from the User A.

If either User A or User B successfully close the view, then you just remove the entrance from the DB.

This is a very simple example but should give you an idea of what I mean.

于 2012-09-14T15:45:17.983 回答
0

我以前也遇到过类似的情况。在这种情况下,您将不得不假设用户对该项目采取行动的最长时间(例如 15 分钟)。创建一个名为 item_lock 的表,其中包含 2 列:item_id、lock_aquired_time。每次用户单击任何项​​目时,都会在此表中插入一条记录。如果任何其他用户尝试打开该项目.. 将在他的屏幕上弹出一条通知,说明该资源已被 XYZ 锁定以执行操作。此外,您将需要创建一个每分钟运行一次的服务,并将删除任何超过 15 分钟的记录。
PS:每当任何用户完成对任何项目的操作时..该行将从 item_lock 表中删除。

于 2012-09-14T15:52:40.907 回答