From 65a2bebd51f1554ad6f39f3dc56bb65f09d742bf Mon Sep 17 00:00:00 2001 From: Leendert de Borst Date: Mon, 25 Nov 2024 13:34:28 +0100 Subject: [PATCH] Update SQLite connection config (#407) --- .../AliasServerDb/AliasServerDbContext.cs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Databases/AliasServerDb/AliasServerDbContext.cs b/src/Databases/AliasServerDb/AliasServerDbContext.cs index 3d054f7c..dc2db172 100644 --- a/src/Databases/AliasServerDb/AliasServerDbContext.cs +++ b/src/Databases/AliasServerDb/AliasServerDbContext.cs @@ -237,7 +237,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) /// DbContextOptionsBuilder instance. protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { - // If the options are not already configured, use the appsettings.json file. if (!optionsBuilder.IsConfigured) { var configuration = new ConfigurationBuilder() @@ -245,11 +244,14 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) .AddJsonFile("appsettings.json") .Build(); + // Add SQLite connection with enhanced settings optionsBuilder - .UseSqlite(configuration.GetConnectionString("AliasServerDbContext")) + .UseSqlite( + configuration.GetConnectionString("AliasServerDbContext") + ";Mode=ReadWriteCreate;Cache=Shared", + options => options.CommandTimeout(60)) .UseLazyLoadingProxies(); - // Set busy timeout using PRAGMA to avoid "The database file is locked" error. + // Set additional PRAGMA settings var connection = Database.GetDbConnection(); if (connection.State != System.Data.ConnectionState.Open) { @@ -258,7 +260,13 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) using (var command = connection.CreateCommand()) { - command.CommandText = "PRAGMA busy_timeout = 5000;"; + // Increase busy timeout + command.CommandText = @" + PRAGMA busy_timeout = 30000; + PRAGMA journal_mode = WAL; + PRAGMA synchronous = FULL; + PRAGMA temp_store = MEMORY; + PRAGMA mmap_size = 1073741824;"; command.ExecuteNonQuery(); } }