Skip to content

Commit

Permalink
improve sqlite concurrency problem (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
catcherwong committed Nov 21, 2020
1 parent e5294b8 commit db73e8f
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions src/EasyCaching.SQLite/Configurations/SQLiteDatabaseProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
{
using EasyCaching.Core;
using Microsoft.Data.Sqlite;
using Microsoft.Extensions.Options;
using System.Collections.Concurrent;
using System.Data;
using System.Threading;
using System.Threading.Tasks;

/// <summary>
/// SQLite database provider.
Expand All @@ -16,16 +19,27 @@ public class SQLiteDatabaseProvider : ISQLiteDatabaseProvider

public SQLiteDatabaseProvider(string name , SQLiteOptions options)
{
this._name = name;
this._options = options.DBConfig;
_name = name;
_options = options.DBConfig;
_builder = new SqliteConnectionStringBuilder
{
DataSource = _options.DataSource,
Mode = _options.OpenMode,
Cache = _options.CacheMode
};

_conns = new ConcurrentDictionary<int, SqliteConnection>();
}

private static ConcurrentDictionary<int, SqliteConnection> _conns;

/// <summary>
/// The conn.
/// The builder
/// </summary>
private static SqliteConnection _conn;
private static SqliteConnectionStringBuilder _builder;

private readonly string _name = EasyCachingConstValue.DefaultSQLiteName;

public string DBProviderName => _name;

/// <summary>
Expand All @@ -34,19 +48,25 @@ public SQLiteDatabaseProvider(string name , SQLiteOptions options)
/// <returns>The connection.</returns>
public SqliteConnection GetConnection()
{
if(_conn == null)
var threadId = Thread.CurrentThread.ManagedThreadId;
var con = _conns.GetOrAdd(threadId, CreateNewConnection());

Task.Run(async () =>
{
SqliteConnectionStringBuilder builder = new SqliteConnectionStringBuilder()
await Task.Delay(5000).ConfigureAwait(false);
_conns.TryRemove(threadId, out var removingConn);
if (removingConn?.State == ConnectionState.Closed)
{
DataSource = _options.DataSource,
Mode = _options.OpenMode,
Cache = _options.CacheMode
};
removingConn.Dispose();
}
});

_conn = new SqliteConnection(builder.ToString());
return con;
}

}
return _conn;
private SqliteConnection CreateNewConnection()
{
return new SqliteConnection(_builder.ToString());
}
}
}

0 comments on commit db73e8f

Please sign in to comment.