2525@ Singleton
2626public class SQLiteFilePersister {
2727 private final AtomicBoolean executing = new AtomicBoolean (false );
28- @ ConfigProperty (name = "quarkus.datasource.jdbc.url " )
29- String jdbcUrl ;
28+ @ ConfigProperty (name = "power-server.db.backup.location " )
29+ String dbFilelocation ;
3030 @ Inject
3131 DataSource dataSource ;
3232 private Path dbFile ;
3333 private Path backupDBFile ;
3434
3535 @ PostConstruct
3636 void init () {
37- int prefixLength = "jdbc:sqlite:" .length ();
38- int queryParamsIdx = jdbcUrl .indexOf ('?' );
39- int length = (queryParamsIdx != -1 ) ? queryParamsIdx : jdbcUrl .length ();
40- var dbFileName = jdbcUrl .substring (prefixLength , length );
41- dbFile = Paths .get (dbFileName );
42- backupDBFile = dbFile .toAbsolutePath ().getParent ().resolve (dbFile .getFileName () + "_backup" );
37+ dbFile = Paths .get (dbFilelocation );
38+ backupDBFile = dbFile .toAbsolutePath ().getParent ().resolve (dbFile .getFileName () + ".backup" );
4339 }
4440
4541 // Periodical backup
@@ -56,24 +52,25 @@ public void onShutdown(@Observes ShutdownEvent event) {
5652 void backup () {
5753 if (executing .compareAndSet (false , true )) {
5854 try {
59- Log .trace ("Starting DB backup for file: " + dbFile );
55+
56+ Log .trace ("Persisting database to: " + dbFile );
6057 try (var conn = dataSource .getConnection ();
6158 var stmt = conn .createStatement ()) {
6259 // Execute the backup
6360 stmt .executeUpdate ("backup to " + backupDBFile );
6461 // Atomically substitute the DB file with its backup
6562 Files .move (backupDBFile , dbFile , StandardCopyOption .ATOMIC_MOVE , StandardCopyOption .REPLACE_EXISTING );
6663 } catch (SQLException e ) {
67- throw new RuntimeException ("Failed to backup the database" , e );
64+ throw new RuntimeException ("Failed to persist the database" , e );
6865 } catch (IOException e ) {
6966 throw new RuntimeException ("Failed to create backup files or folders" , e );
7067 }
71- Log .info ("Backup of " + dbFile + " completed" );
68+ Log .info ("Persisting " + dbFile + " completed" );
7269 } finally {
7370 executing .set (false );
7471 }
7572 } else {
76- Log .trace ("Skipping backup as one is already in progress" );
73+ Log .trace ("Skipping database persistence as the operation is already in progress" );
7774 }
7875 }
7976
0 commit comments