- Batch size: Increased from 20 to 50 records
- Commit interval: Increased from 5s to 10s
- Checkpoint interval: Increased from 5s to 10s
- Result: Fewer write operations = fewer read conflicts
All monitoring scripts now use read-only mode:
sqlite3 "file:data/sensor_data.db?mode=ro" "SELECT ..."This prevents:
- "database is locked" errors
- "file is not a database" errors during checkpoints
- Accidental writes from read operations
Added comprehensive examples in the README for safe reading in:
- Bash: One-liner monitoring scripts with error handling
- Python: Context managers with retry logic
- JavaScript: Both callback and async/await patterns
read_safe.py: Simple example of safe reading with monitoring modemonitor_db.sh: Updated to use read-only connectionscheck_db.sh: Updated to use read-only connections
With these optimizations:
- Write performance: Slightly improved due to larger batches
- Read reliability: Greatly improved - minimal conflicts
- Checkpoint frequency: Reduced from ~12/min to ~6/min
- Read windows: Consistent 10-second windows between commits
-
Always use read-only mode:
sqlite3 "file:path/to/db?mode=ro" -
Set appropriate timeouts:
conn = sqlite3.connect(db_path, timeout=30.0)
-
Handle transient errors gracefully:
except sqlite3.OperationalError as e: if "database is locked" in str(e): time.sleep(0.5) retry()
-
Don't hold connections open:
with get_connection() as conn: # Quick query result = conn.execute(query).fetchall() # Connection auto-closed
To verify the optimizations are working:
# Watch for checkpoint operations (should be ~6/minute)
while true; do
echo "[$(date '+%H:%M:%S')] WAL size: $(ls -lh data/sensor_data.db-wal 2>/dev/null | awk '{print $5}')"
sleep 10
doneIf you still experience read issues:
-
Check WAL mode is active:
sqlite3 data/sensor_data.db "PRAGMA journal_mode;" -
Verify checkpoint frequency: Look for "checkpoint" in logs
-
Ensure read-only mode: Always use
file:URI syntax with?mode=ro
The database is now optimized for:
- ✅ Multiple concurrent readers
- ✅ Continuous writing without blocking reads
- ✅ Minimal "database locked" errors
- ✅ Consistent read windows every 10 seconds