Skip to content

Commit

Permalink
Update SQLite connection config (#407)
Browse files Browse the repository at this point in the history
  • Loading branch information
lanedirt committed Nov 25, 2024
1 parent 22e29c6 commit 65a2beb
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/Databases/AliasServerDb/AliasServerDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,19 +237,21 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
/// <param name="optionsBuilder">DbContextOptionsBuilder instance.</param>
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()
.SetBasePath(Directory.GetCurrentDirectory())
.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)
{
Expand All @@ -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();
}
}
Expand Down

0 comments on commit 65a2beb

Please sign in to comment.