dependency-check-core/src/main/java/org/owasp/dependencycheck/utils/H2DBLock.java
/**
     * Obtains a lock on the H2 database.
     *
     * @throws H2DBLockException thrown if a lock could not be obtained
     */
Obtains a lock on the H2 database.
@throws H2DBLockException thrown if a lock could not be obtained
Obtains a lock on the H2 database . @throws H2DBLockException thrown if a lock could not be obtained
public void lock() throws H2DBLockException {
        if (ConnectionFactory.isH2Connection()) {
            try {
                final File dir = Settings.getDataDirectory();
                lockFile = new File(dir, "dc.update.lock");
                if (lockFile.isFile() && getFileAge(lockFile) > 5 && !lockFile.delete()) {
                    LOGGER.warn("An old db update lock file was found but the system was unable to delete "
                            + "the file. Consider manually deleting {}", lockFile.getAbsolutePath());
                }
                int ctr = 0;
                do {
                    try {
                        if (!lockFile.exists() && lockFile.createNewFile()) {
                            file = new RandomAccessFile(lockFile, "rw");
                            lock = file.getChannel().lock();
                        }
                    } catch (IOException ex) {
                        LOGGER.trace("Expected error as another thread has likely locked the file", ex);
                    } finally {
                        if (lock == null && file != null) {
                            try {
                                file.close();
                            } catch (IOException ex) {
                                LOGGER.trace("Unable to close the ulFile", ex);
                            }
                        }
                    }
                    if (lock == null || !lock.isValid()) {
                        try {
                            LOGGER.debug("Sleeping thread {} for 5 seconds because we could not obtain the update lock.",
                                    Thread.currentThread().getName());
                            Thread.sleep(5000);
                        } catch (InterruptedException ex) {
                            LOGGER.trace("ignorable error, sleep was interrupted.", ex);
                            Thread.currentThread().interrupt();
                        }
                    }
                } while (++ctr < 60 && (lock == null || !lock.isValid()));
                if (lock == null || !lock.isValid()) {
                    throw new H2DBLockException("Unable to obtain the update lock, skipping the database update. Skippinig the database update.");
                }
            } catch (IOException ex) {
                throw new H2DBLockException(ex.getMessage(), ex);
            }
        }
    }
public void lock ( ) throws H2DBLockException { if ( ConnectionFactory . isH2Connection ( ) ) { try { final File dir = Settings . getDataDirectory ( ) ; lockFile = new File ( dir , "dc.update.lock" ) ; if ( lockFile . isFile ( ) && getFileAge ( lockFile ) > 5 && ! lockFile . delete ( ) ) { LOGGER . warn ( "An old db update lock file was found but the system was unable to delete " + "the file. Consider manually deleting {}" , lockFile . getAbsolutePath ( ) ) ; } int ctr = 0 ; do { try { if ( ! lockFile . exists ( ) && lockFile . createNewFile ( ) ) { file = new RandomAccessFile ( lockFile , "rw" ) ; lock = file . getChannel ( ) . lock ( ) ; } } catch ( IOException ex ) { LOGGER . trace ( "Expected error as another thread has likely locked the file" , ex ) ; } finally { if ( lock == null && file != null ) { try { file . close ( ) ; } catch ( IOException ex ) { LOGGER . trace ( "Unable to close the ulFile" , ex ) ; } } } if ( lock == null || ! lock . isValid ( ) ) { try { LOGGER . debug ( "Sleeping thread {} for 5 seconds because we could not obtain the update lock." , Thread . currentThread ( ) . getName ( ) ) ; Thread . sleep ( 5000 ) ; } catch ( InterruptedException ex ) { LOGGER . trace ( "ignorable error, sleep was interrupted." , ex ) ; Thread . currentThread ( ) . interrupt ( ) ; } } } while ( ++ ctr < 60 && ( lock == null || ! lock . isValid ( ) ) ) ; if ( lock == null || ! lock . isValid ( ) ) { throw new H2DBLockException ( "Unable to obtain the update lock, skipping the database update. Skippinig the database update." ) ; } } catch ( IOException ex ) { throw new H2DBLockException ( ex . getMessage ( ) , ex ) ; } } }
Obtains a lock on the H2 database.
Obtains a lock on the H2 database .