Erster Versuch, eine ASYNC Version des DBManagers zu starten
This commit is contained in:
parent
5f28c8c7ea
commit
af1a071664
@ -15,6 +15,8 @@ using System.Diagnostics;
|
|||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using log4net;
|
using log4net;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
namespace bsmd.database
|
namespace bsmd.database
|
||||||
{
|
{
|
||||||
@ -32,6 +34,7 @@ namespace bsmd.database
|
|||||||
private bool _closeConnectionAfterUse = false;
|
private bool _closeConnectionAfterUse = false;
|
||||||
private readonly List<string> truncatedFieldCollection = new List<string>();
|
private readonly List<string> truncatedFieldCollection = new List<string>();
|
||||||
private Dictionary<Type, string> messageHistoryTypeDict;
|
private Dictionary<Type, string> messageHistoryTypeDict;
|
||||||
|
private SemaphoreSlim _asyncSemaphore = new SemaphoreSlim(1);
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@ -99,6 +102,22 @@ namespace bsmd.database
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<bool> ConnectAsync(string dbConnectionString)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_con = new SqlConnection(dbConnectionString);
|
||||||
|
await _con.OpenAsync();
|
||||||
|
this.ConnectionString = dbConnectionString;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch(Exception ex)
|
||||||
|
{
|
||||||
|
_log.ErrorFormat("DBManager cannot connect:{0}", ex.Message);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void Disconnect()
|
public void Disconnect()
|
||||||
{
|
{
|
||||||
if (this._con?.State == ConnectionState.Open)
|
if (this._con?.State == ConnectionState.Open)
|
||||||
@ -785,6 +804,13 @@ namespace bsmd.database
|
|||||||
this.Connect(this.ConnectionString);
|
this.Connect(this.ConnectionString);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task CheckConnectionAsync()
|
||||||
|
{
|
||||||
|
if ((this._con == null) ||
|
||||||
|
(this._con.State == ConnectionState.Closed))
|
||||||
|
await this.ConnectAsync(this.ConnectionString);
|
||||||
|
}
|
||||||
|
|
||||||
private void LogNonQueryResult(string query, int queryResult)
|
private void LogNonQueryResult(string query, int queryResult)
|
||||||
{
|
{
|
||||||
switch (queryResult)
|
switch (queryResult)
|
||||||
@ -1664,6 +1690,40 @@ namespace bsmd.database
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region async DB access methods
|
||||||
|
|
||||||
|
internal async Task<SqlDataReader> PerformCommandAsync(SqlCommand cmd)
|
||||||
|
{
|
||||||
|
SqlDataReader reader = null;
|
||||||
|
|
||||||
|
await _asyncSemaphore.WaitAsync();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await this.CheckConnectionAsync();
|
||||||
|
cmd.Connection = this._con;
|
||||||
|
reader = await cmd.ExecuteReaderAsync();
|
||||||
|
}
|
||||||
|
catch (SqlException ex)
|
||||||
|
{
|
||||||
|
Trace.WriteLine("SQL Exception:" + ex.Message);
|
||||||
|
_log.Error("Error performing command", ex);
|
||||||
|
_log.DebugFormat("Query: {0}", cmd.CommandText);
|
||||||
|
_log.Debug("Parameters:");
|
||||||
|
for (int i = 0; i < cmd.Parameters.Count; i++)
|
||||||
|
{
|
||||||
|
_log.DebugFormat("{0}:{1}", cmd.Parameters[i].ParameterName, cmd.Parameters[i].Value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_asyncSemaphore.Release();
|
||||||
|
}
|
||||||
|
|
||||||
|
return reader;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user