如何确定是否存在H2数据库文件锁? [英] How do I determine if an H2 database file lock exists?

查看:109
本文介绍了如何确定是否存在H2数据库文件锁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我不会解释的原因(因为人们将在另一个主题上引导他们的回答而不是我手头的问题),我需要知道如何确定我的H2数据库是否被锁定。使用Java代码,如何确定我的数据库中是否存在锁定文件?

For reasons that I will not explain (because people will direct their responses at the other topic, instead of my problem at hand), I need to know how to determine whether or not my H2 database is locked. Using Java code, how do I determine whether or not the lock file exists on my database?

推荐答案

对于其他人阅读此问题我需要解释为什么你不应该自己做,并让数据库自己检测它是否被锁定。首先,数据库文件锁定是一种实现细节,可以在将来的数据库版本中进行更改。然后,存在竞争条件:如果您现在看到锁不存在,则可能在一秒之后存在。因此唯一可靠的方法是尝试锁定。所以:尝试以读写模式打开数据库。缺点:它有点慢,因为它将初始化数据库并在需要时运行恢复代码。我明白这不是你想要的,因为它很慢(对吗?)。

For others reading this question I need to explain why you you shouldn't do it yourself, and let the database detect itself whether it is locked or not. First of all, database file locking is an implementation detail that can and will change in future versions of the database. Then, there is a race condition: if you see the lock doesn't exist now, it may exist one second later. So the only reliable way is to try locking. So: try opening the database in read-write mode. Disadvantage: it is a bit slow, as it will initialize the database and also run the recovery code if needed. I understand this is not what you want, because it is slow (right?).

对于较低级别的方法,它取决于你使用的H2版本。

For lower level method, it depends which version of H2 you use.

尝试使用以下代码锁定文件本身:

Try locking the file itself using the following code:

static boolean isLocked(String fileName) {
    try {
        RandomAccessFile f = new RandomAccessFile(fileName, "r");
        try {
            FileLock lock = f.getChannel().tryLock(0, Long.MAX_VALUE, true);
            if (lock != null) {
                lock.release();
                return false;
            }
        } finally {
            f.close();
        }
    } catch (IOException e) {
        // ignore
    }
    return true;
}

或使用H2代码(来自MVStore):

Or using H2 code (from the MVStore):

static boolean isLocked(String fileName) {
    FileStore fs = new FileStore();
    try {
        fs.open(fileName, true, null);
        return false;
    } catch (IllegalStateException e) {
        return true;
    } finally {
        fs.close();
    }
}



版本1.3(稳定)和1.4禁用MVStore



只需检查文件< databaseName> .lock.db 是否存在是不够的。如果进程被终止,即使数据库未打开也可能存在。所以一些示例代码(未经测试)是:

Version 1.3 (stable) and 1.4 with the MVStore disabled

Simply checking if the file <databaseName>.lock.db exists is not enough. It might exist even the database is not open, in case the process was killed. So some sample code (not tested) is:

// file name must be:
// path + databaseName + Constants.SUFFIX_LOCK_FILE
static boolean isLocked(String fileName) {
    try {
        FileLock lock = new FileLock(new TraceSystem(null), fileName, 1000);
        lock.lock(FileLock.LOCK_FILE);
        lock.unlock();
        return false;
    } catch (Exception e) {
        return true;
    }
}

这篇关于如何确定是否存在H2数据库文件锁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆