MyCATApache/Mycat-Server · error · LogException

Log already in use? in

Error message

Log already in use? ${fileName} in ${dir}

What it means

LogFileLock.acquireLock() takes an OS-level file lock (FileLock) on the XA transaction log file. If the lock cannot be obtained (lock_ stays null) — the file is already locked by another MyCat instance or a zombie process — it logs an error and throws LogException("Log already in use? <fileName> in <dir>").

Solutions

  1. Find and stop the process holding the lock (lsof / handle.exe on the log file in the tmlog directory) before restarting MyCat
  2. Ensure only one MyCat instance points at this data dir; fix deployment/failover scripts to guarantee mutual exclusion
  3. Remove stale lock artifacts after confirming no process holds the lock, then restart
  4. Move XA logs off NFS to a local filesystem where file locking is reliable

Example fix

# before (fails)
./mycat start   # on same data dir while old instance runs
# after
ps aux | grep mycat && kill <old-pid>; rm -f $MYCAT_HOME/logs/...stale-lock; ./mycat start
Defensive patterns

Strategy: retry

Validate before calling

// before starting the second instance
Process proc = Runtime.getRuntime().exec(new String[]{"fuser", dir + "/tmlog/LogFile"});
// exit code 0 => lock held; abort startup or wait for the holder to exit

Try / catch

try { xaLog.acquireLock(); } catch (LogException e) { if (e.getMessage().startsWith("Log already in use?")) { /* wait for other instance or kill stale process, then retry */ } else { throw e; } }

Prevention

When it happens

Trigger: Starting a second MyCat instance against the same data dir while another instance holds the lock on the XA log file; a crashed/killed MyCat whose OS lock is still held on Windows, or a stale lock file that some filesystems fail to release.

Common situations: Accidentally running two MyCat nodes sharing one data directory; failover scripts starting a standby before the primary fully died; NFS-mounted dirs where file locks behave unreliably; leftover processes after an unclean kill on Windows.

Understand the failure class

Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.

Related errors


AI-assisted analysis of MyCATApache/Mycat-Server@65f8d8beb7 (2026-09-11). Data as JSON: /api/errors/85d2e2a9fd46fd1e. Report an issue: GitHub.

Appendix: source

Thrown at src/main/java/io/mycat/backend/mysql/xa/LogFileLock.java:55

        try {
            File parent = new File(dir);
            if(!parent.exists()) {
                parent.mkdir();
            }
            lockfileToPreventDoubleStartup_ = new File(dir, fileName + ".lck");
            lockfilestream_ = new FileOutputStream(lockfileToPreventDoubleStartup_);
            lock_ = lockfilestream_.getChannel().tryLock();
            lockfileToPreventDoubleStartup_.deleteOnExit();
        } catch (OverlappingFileLockException failedToGetLock) {
            // happens on windows
            lock_ = null;
        } catch (IOException failedToGetLock) {
            // happens on windows
            lock_ = null;
        }
        if (lock_ == null) {
            logger.error("ERROR: the specified log seems to be in use already: " + fileName + " in " + dir + ". Make sure that no other instance is running, or kill any pending process if needed.");
            throw new LogException("Log already in use? " + fileName + " in "+ dir);
        }
    }

    public void releaseLock() {
        try {
            if (lock_ != null) {
                lock_.release();
            }
            if (lockfilestream_ != null)
                lockfilestream_.close();
        } catch (IOException e) {
            logger.warn("Error releasing file lock: " + e.getMessage());
        } finally {
            lock_ = null;
        }

        if (lockfileToPreventDoubleStartup_ != null) {
            lockfileToPreventDoubleStartup_.delete();

View on GitHub (pinned to 65f8d8beb7)