Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.sql.SQLException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

Expand All @@ -31,6 +33,7 @@ public class SQLiteFilePersister {
DataSource dataSource;
private Path dbFile;
private Path backupDBFile;
private final ExecutorService executor = Executors.newSingleThreadExecutor();

@PostConstruct
void init() {
Expand All @@ -52,27 +55,30 @@ public void onShutdown(@Observes ShutdownEvent event) {

void backup() {
if (executing.compareAndSet(false, true)) {
try {

Log.trace("Persisting database to: " + dbFile);
try (var conn = dataSource.getConnection();
var stmt = conn.createStatement()) {
// Execute the backup
stmt.executeUpdate("backup to " + backupDBFile);
// Atomically substitute the DB file with its backup
Files.move(backupDBFile, dbFile, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
} catch (SQLException e) {
throw new RuntimeException("Failed to persist the database", e);
} catch (IOException e) {
throw new RuntimeException("Failed to create backup files or folders", e);
}
Log.info("Persisting " + dbFile + " completed");
} finally {
executing.set(false);
}
executor.submit(this::doBackup);
} else {
Log.trace("Skipping database persistence as the operation is already in progress");
}
}

private void doBackup() {
try {
Log.trace("Persisting database to: " + dbFile);
try (var conn = dataSource.getConnection();
var stmt = conn.createStatement()) {
// Execute the backup
stmt.executeUpdate("backup to " + backupDBFile);
// Atomically substitute the DB file with its backup
Files.move(backupDBFile, dbFile, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
} catch (SQLException e) {
throw new RuntimeException("Failed to persist the database", e);
} catch (IOException e) {
throw new RuntimeException("Failed to create backup files or folders", e);
}
Log.info("Persisting " + dbFile + " completed");
} finally {
executing.set(false);
}
}

}
Loading