Enable write-ahead logging for keystore db.
Write-ahead logging wasn't previously enabled for the keystore2 sqlite
databases out of concern that it might make it impossible to open the
database when the file system is full. Work to correct that problem,
to ensure that sqlite databases can always be opened in WAL mode even
when the WAL file cannot be created, is in progress, so this CL goes
ahead and puts the database in WAL mode. The approach is a little
wasteful, since it re-sends the pragma on every connection, but that
ensures that it gets done and shouldn't impose any significant
overhead.
In the event that setting WAL mode fails, we log an error and continue
on.
Test: CtsKeystoreTestCases
Change-Id: I7d5618760019dce68576f72575321c54c3c24415
diff --git a/keystore2/src/database.rs b/keystore2/src/database.rs
index 4ab4258..b6c459a 100644
--- a/keystore2/src/database.rs
+++ b/keystore2/src/database.rs
@@ -987,6 +987,12 @@
return Err(e);
}
}
+ let result: String = conn
+ .pragma_update_and_check(None, "journal_mode", &"WAL", |row| row.get(0))
+ .expect("Attempting to set journal mode failed.");
+ if result != "wal" {
+ error!("Failed to put DB in WAL mode. This will make keystore slow.");
+ }
break;
}
diff --git a/keystore2/vpnprofilestore/lib.rs b/keystore2/vpnprofilestore/lib.rs
index 8b3bc2b..5883ee1 100644
--- a/keystore2/vpnprofilestore/lib.rs
+++ b/keystore2/vpnprofilestore/lib.rs
@@ -24,6 +24,7 @@
};
use anyhow::{Context, Result};
use keystore2::{async_task::AsyncTask, legacy_blob::LegacyBlobLoader, utils::watchdog as wd};
+use log::error;
use rusqlite::{
params, Connection, OptionalExtension, Transaction, TransactionBehavior, NO_PARAMS,
};
@@ -46,6 +47,15 @@
db.conn.busy_handler(None).context("Failed to set busy handler.")?;
db.init_tables().context("Trying to initialize vpnstore db.")?;
+
+ let result: String = db
+ .conn
+ .pragma_update_and_check(None, "journal_mode", &"WAL", |row| row.get(0))
+ .expect("Attempting to set journal mode failed.");
+ if result != "wal" {
+ error!("Failed to put DB in WAL mode. This will make keystore slow.");
+ }
+
Ok(db)
}