-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
208 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 3 additions & 5 deletions
8
src/OrchardCore/OrchardCore.Data.Abstractions/IDbConnectionAccessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,9 @@ | ||
using System; | ||
using System.Data; | ||
using System.Threading.Tasks; | ||
using System.Data.Common; | ||
|
||
namespace OrchardCore.Data.Abstractions | ||
namespace OrchardCore.Data | ||
{ | ||
public interface IDbConnectionAccessor | ||
{ | ||
Task<IDbConnection> GetConnectionAsync(); | ||
DbConnection CreateConnection(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,21 @@ | ||
using System; | ||
using System.Data; | ||
using System.Data.Common; | ||
using System.Threading.Tasks; | ||
using OrchardCore.Data.Abstractions; | ||
using YesSql; | ||
|
||
namespace OrchardCore.Data | ||
{ | ||
public class DbConnectionAccessor : IDbConnectionAccessor, IDisposable | ||
public class DbConnectionAccessor : IDbConnectionAccessor | ||
{ | ||
private readonly IStore _store; | ||
private DbConnection _connection; | ||
private bool _disposed = false; | ||
|
||
public DbConnectionAccessor(IStore store) | ||
{ | ||
_store = store ?? throw new ArgumentNullException(nameof(store)); | ||
} | ||
|
||
public async Task<IDbConnection> GetConnectionAsync() | ||
public DbConnection CreateConnection() | ||
{ | ||
if (_connection == null) | ||
{ | ||
_connection = _store.Configuration.ConnectionFactory.CreateConnection() as DbConnection; | ||
} | ||
|
||
if (_connection.State == ConnectionState.Closed) | ||
{ | ||
await _connection?.OpenAsync(); | ||
} | ||
|
||
return _connection; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (!_disposed) | ||
{ | ||
if (disposing) | ||
{ | ||
_connection.Dispose(); | ||
} | ||
} | ||
_disposed = true; | ||
return _store.Configuration.ConnectionFactory.CreateConnection(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.