Skip to content

Commit

Permalink
Cache ServerVersion.AutoDetect (#4256)
Browse files Browse the repository at this point in the history
* Cache ServerVersion.AutoDetect

Adjusted database connections so that they cache ServerVersion.AutoDetect result on first hit and reuse.

This resolves a current bug that exhausts or times out connections to database.

* Change to dictionary

* Update Source/ACE.Database/DatabaseManager.cs

Co-authored-by: gmriggs <gmriggs@gmail.com>

* Update DatabaseManager.cs

* Update DatabaseManager.cs

---------

Co-authored-by: gmriggs <gmriggs@gmail.com>
  • Loading branch information
LtRipley36706 and gmriggs authored Dec 18, 2024
1 parent 7eee40e commit 74cce13
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
15 changes: 15 additions & 0 deletions Source/ACE.Database/DatabaseManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
using System.Collections.Concurrent;

using Microsoft.EntityFrameworkCore;

using log4net;

Expand Down Expand Up @@ -69,5 +72,17 @@ public static void Stop()
if (serializedShardDb != null)
serializedShardDb.Stop();
}

private static readonly ConcurrentDictionary<string, ServerVersion> cachedServerVersions = new();

public static ServerVersion CachedServerVersionAutoDetect(string database, string connectionString)
{
if (!cachedServerVersions.TryGetValue(database, out ServerVersion serverVersion))
{
serverVersion = ServerVersion.AutoDetect(connectionString);
cachedServerVersions[database] = serverVersion;
}
return serverVersion;
}
}
}
2 changes: 1 addition & 1 deletion Source/ACE.Database/Models/Auth/AuthDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

var connectionString = $"server={config.Host};port={config.Port};user={config.Username};password={config.Password};database={config.Database};{config.ConnectionOptions}";

optionsBuilder.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString), builder =>
optionsBuilder.UseMySql(connectionString, DatabaseManager.CachedServerVersionAutoDetect(config.Database, connectionString), builder =>
{
builder.EnableRetryOnFailure(10);
});
Expand Down
2 changes: 1 addition & 1 deletion Source/ACE.Database/Models/Shard/ShardDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

var connectionString = $"server={config.Host};port={config.Port};user={config.Username};password={config.Password};database={config.Database};{config.ConnectionOptions}";

optionsBuilder.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString), builder =>
optionsBuilder.UseMySql(connectionString, DatabaseManager.CachedServerVersionAutoDetect(config.Database, connectionString), builder =>
{
builder.EnableRetryOnFailure(10);
});
Expand Down
2 changes: 1 addition & 1 deletion Source/ACE.Database/Models/World/WorldDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

var connectionString = $"server={config.Host};port={config.Port};user={config.Username};password={config.Password};database={config.Database};{config.ConnectionOptions}";

optionsBuilder.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString), builder =>
optionsBuilder.UseMySql(connectionString, DatabaseManager.CachedServerVersionAutoDetect(config.Database, connectionString), builder =>
{
builder.EnableRetryOnFailure(10);
});
Expand Down

0 comments on commit 74cce13

Please sign in to comment.