Oracle表锁不起作用

时间:2016-03-21 17:17:37

标签: java oracle locking

我有一个springframework调度服务,每两分钟在两个不同的服务器上执行此代码。正如评论所示,我假设第一个获取锁的服务器将继续处理,第二个服务器将尝试设置锁并获得ORA-00054错误然后退出(返回)。这种情况没有发生,两个服务器日志都有“JPB **继续处理”行。

有人建议可能是第一台服务器在第二台服务器尝试设置锁之前完成处理的时间。我知道这没有发生,因为部分处理是删除MY_TABLE中的记录。即使第二个服务器在第一个服务器完成处理后设置锁定,它也没有要处理的记录。我有额外的日志声明(下面没有显示)告诉我两个服务器正在处理相同的记录。

我没有正确设置锁吗?

感谢您的协助。

    // Because the same timer event will be running on both production servers and calling
    // this method, a lock must be set on the table so that the first method that gets 
    // the lock can process, and the other will exit.
    String executeStatement = "LOCK TABLE MY_TABLE IN EXCLUSIVE MODE NOWAIT";
    try {
        Statement statement = idbConn.createStatement();
        statement.execute(executeStatement);
    } catch (SQLException e) {
        // If the error is ORA-00054: resource busy, then the other server locked the table first
        // and will process the records in the table, so just return.
        if (e.getMessage().contains("ORA-00054")) {
            ora00054 = true;
            logger.debug("JPB** Trapped the ORA-00054 error setting the LOCK on the MY_TABLE table ");
        } else {
            otherException = true;
            logger.error("A non-busy error occurred executing LOCK on MY_TABLE table ");
            e.printStackTrace();
        }
    } finally {
        if (ora00054 || otherException) {
            // Close connection
            logger.debug("JPB** exiting");
            return;
        }
    }
    logger.debug("JPB** continue processing");
    // Select and process the records in MY_TABLE, delete them, then commit to save and release the lock.

0 个答案:

没有答案