diff --git a/core/src/main/java/org/bitcoinj/store/SPVBlockStore.java b/core/src/main/java/org/bitcoinj/store/SPVBlockStore.java index a845e7001b8..73cbcf7464b 100644 --- a/core/src/main/java/org/bitcoinj/store/SPVBlockStore.java +++ b/core/src/main/java/org/bitcoinj/store/SPVBlockStore.java @@ -119,6 +119,12 @@ public SPVBlockStore(NetworkParameters params, File file, int capacity, boolean boolean exists = file.exists(); // Set up the backing file. randomAccessFile = new RandomAccessFile(file, "rw"); + FileChannel channel = randomAccessFile.getChannel(); + // Lock the file. + fileLock = channel.tryLock(); + if (fileLock == null) + throw new ChainFileLockedException("Store file is already locked by another process"); + fileLength = getFileSize(capacity); if (!exists) { log.info("Creating new SPV block chain file " + file); @@ -140,11 +146,6 @@ else if (fileLength < randomAccessFile.length()) } } - FileChannel channel = randomAccessFile.getChannel(); - fileLock = channel.tryLock(); - if (fileLock == null) - throw new ChainFileLockedException("Store file is already locked by another process"); - // Map it into memory read/write. The kernel will take care of flushing writes to disk at the most // efficient times, which may mean that until the map is deallocated the data on disk is randomly // inconsistent. However the only process accessing it is us, via this mapping, so our own view will