Mis*_*ale 4 java mysql concurrency locking
我在一家游戏网吧工作,我们这里有一个系统(smartlaunch)可以跟踪游戏许可证。我编写了一个与该系统接口的程序(实际上,它是后端 MySQL 数据库)。该程序旨在在客户端 PC 上运行,并且 (1) 查询数据库以从可用池中选择一个未使用的许可证,然后 (2) 将此许可证标记为客户端 PC 正在使用。
问题是,我有一个并发错误。该程序旨在在多台机器上同时启动,当发生这种情况时,一些机器通常会尝试获取相同的许可证。我认为这是因为步骤 (1) 和 (2) 没有同步,即一个程序确定许可证 #5 可用并选择它,但在它可以将 #5 标记为正在使用之前,另一台 PC 上的另一个程序副本尝试获取相同的许可证。
我试图通过使用事务和表锁定来解决这个问题,但它似乎没有任何区别 - 我这样做对吗?下面是有问题的代码:
public LicenseKey Acquire() throws SmartLaunchException, SQLException {
Connection conn = SmartLaunchDB.getConnection();
int PCID = SmartLaunchDB.getCurrentPCID();
conn.createStatement().execute("LOCK TABLE `licensekeys` WRITE");
String sql = "SELECT * FROM `licensekeys` WHERE `InUseByPC` = 0 AND LicenseSetupID = ? ORDER BY `ID` DESC LIMIT 1";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setInt(1, this.id);
ResultSet results = statement.executeQuery();
if (results.next()) {
int licenseID = results.getInt("ID");
sql = "UPDATE `licensekeys` SET `InUseByPC` = ? WHERE `ID` = ?";
statement = conn.prepareStatement(sql);
statement.setInt(1, PCID);
statement.setInt(2, licenseID);
statement.executeUpdate();
statement.close();
conn.commit();
conn.createStatement().execute("UNLOCK TABLES");
return new LicenseKey(results.getInt("ID"), this, results.getString("LicenseKey"), results.getInt("LicenseKeyType"));
} else {
throw new SmartLaunchException("All licenses of type " + this.name + "are in use");
}
}
Run Code Online (Sandbox Code Playgroud)
你必须做两件事:
SELECT ... FOR UPDATE 比 LOCK TABLE 更好,因为它可以通过行级锁定来实现,而不是自动锁定整个表