在多台计算机上运行调度程序

时间:2015-05-10 20:31:46

标签: java db2 locking scheduling

我有一个Java服务,它将来每隔y秒创建一次x游戏(计算每个游戏的开始时间和结束时间)并将它们插入到DB2表中。该代码基本上查找上次创建的游戏的结束时间(如果它存在且不在过去),并使用它来创建未来的游戏。否则,它使用游戏持续时间(所有游戏都相同)和当前时间来创建新游戏。

此服务将同时在多台计算机上运行,​​因此存在上次游戏检查不准确的风险。一种解决方案是锁定整个表格,但这并不能很好地扩展。有没有更有效处理这种情况的想法?

1 个答案:

答案 0 :(得分:1)

DB2(以及大多数企业数据库供应商)具有适当的锁定机制,可用于高度并发的工作负载,以满足数据库ACID属性。您不必担心自己在表上放置显式锁。

在DB2中,隔离级别确定在访问数据时如何锁定数据或将数据与其他进程隔离。以下是有关不同隔离级别的详细信息:

UR: Allows an application to access uncommitted changes of other transactions.
CS: Locks any row accessed by a transaction of an application while the cursor is positioned on the row. This lock remains in effect until the next row is fetched or the transaction is terminated.
RS: Locks only those rows that an application retrieves within a unit of work. It ensures that any qualifying row read during a unit of work is not changed by other application processes until the unit of work completes.
RR: Locks all the rows an application references within a unit of work. 

您可以在本文中详细了解每个隔离级别的限制,以便为查询选择最合适的级别:

http://www.ibm.com/developerworks/data/library/techarticle/dm-1107db2isolationlevel/