168 lines
5.6 KiB
C#
Executable File
168 lines
5.6 KiB
C#
Executable File
using System;
|
|
using System.Data;
|
|
using System.Data.Common;
|
|
using System.Diagnostics;
|
|
using System.Runtime.InteropServices;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Opsoft.NetCore.O3App.Shared.Data
|
|
{
|
|
[ComVisible(false)]
|
|
internal class OPDbConnection : DbConnection, IDisposable
|
|
{
|
|
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
|
|
private readonly DbConnection innerConnection;
|
|
|
|
private readonly bool checkWhere;
|
|
|
|
private bool disposedValue;
|
|
|
|
public override string ConnectionString { get; set; }
|
|
|
|
public override string Database => innerConnection.Database;
|
|
|
|
public override string DataSource => innerConnection.DataSource;
|
|
|
|
public override string ServerVersion => innerConnection.ServerVersion;
|
|
|
|
public override ConnectionState State => innerConnection.State;
|
|
|
|
public override int ConnectionTimeout => innerConnection.ConnectionTimeout;
|
|
|
|
public string Provider { get; private set; }
|
|
|
|
public DiagnosticSource Listener { get; private set; }
|
|
|
|
internal OPDbConnection(DbConnection connection, bool checkWhere, string provider, DiagnosticSource diagnosticListener)
|
|
{
|
|
this.checkWhere = checkWhere;
|
|
innerConnection = connection;
|
|
innerConnection.StateChange += OnStateChange;
|
|
Provider = provider;
|
|
Listener = diagnosticListener;
|
|
}
|
|
|
|
private void OnStateChange(object sender, StateChangeEventArgs e)
|
|
{
|
|
base.OnStateChange(e);
|
|
}
|
|
|
|
internal DbConnection GetConnection()
|
|
{
|
|
return innerConnection;
|
|
}
|
|
|
|
internal string GetDatabaseType()
|
|
{
|
|
return innerConnection.GetType().Name.ToLower();
|
|
}
|
|
|
|
public override void ChangeDatabase(string databaseName)
|
|
{
|
|
innerConnection.ChangeDatabase(databaseName);
|
|
}
|
|
|
|
public override void Close()
|
|
{
|
|
Guid operationId = Listener.BeforeCloseConnection(Provider, this, "Close");
|
|
try
|
|
{
|
|
innerConnection.Close();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Listener.CloseConnectionError(Provider, operationId, this, ex, "Close");
|
|
//Log.Error(ex, ex.Message);
|
|
throw;
|
|
}
|
|
finally
|
|
{
|
|
Listener.AfterCloseConnection(Provider, operationId, this, "Close");
|
|
}
|
|
}
|
|
|
|
public override void Open()
|
|
{
|
|
Guid operationId = Listener.BeforeOpenConnection(Provider, this, "Open");
|
|
try
|
|
{
|
|
innerConnection.Open();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Listener.OpenConnectionError(Provider, operationId, this, ex, "Open");
|
|
//Log.Error(ex, ex.Message);
|
|
throw;
|
|
}
|
|
finally
|
|
{
|
|
Listener.AfterOpenConnection(Provider, operationId, this, "Open");
|
|
}
|
|
}
|
|
|
|
public override async Task OpenAsync(CancellationToken cancellationToken)
|
|
{
|
|
Guid operationId = Listener.BeforeOpenConnection(Provider, this, "OpenAsync");
|
|
try
|
|
{
|
|
await innerConnection.OpenAsync(cancellationToken);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Listener.OpenConnectionError(Provider, operationId, this, ex, "OpenAsync");
|
|
//Log.Error(ex, ex.Message);
|
|
throw;
|
|
}
|
|
finally
|
|
{
|
|
Listener.AfterOpenConnection(Provider, operationId, this, "OpenAsync");
|
|
}
|
|
}
|
|
|
|
protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel)
|
|
{
|
|
return new OPDbTransaction(innerConnection.BeginTransaction(isolationLevel), this);
|
|
}
|
|
|
|
protected override DbCommand CreateDbCommand()
|
|
{
|
|
return new OPDbCommand(innerConnection.CreateCommand(), this, checkWhere);
|
|
}
|
|
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
if (disposedValue)
|
|
{
|
|
return;
|
|
}
|
|
if (disposing)
|
|
{
|
|
Guid operationId = Listener.BeforeCloseConnection(Provider, this, "Dispose");
|
|
try
|
|
{
|
|
innerConnection.Dispose();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Listener.CloseConnectionError(Provider, operationId, this, ex, "Dispose");
|
|
//Log.Error(ex, ex.Message);
|
|
throw;
|
|
}
|
|
finally
|
|
{
|
|
Listener.AfterCloseConnection(Provider, operationId, this, "Dispose");
|
|
}
|
|
}
|
|
else if (innerConnection.State == ConnectionState.Open)
|
|
{
|
|
//Log.Error("垃圾回收时检测到数据库连接有泄漏,请及时关闭数据库连接或使用using语法" + Environment.NewLine + ConnectionString);
|
|
throw new OverflowException("垃圾回收时检测到数据库连接有泄漏,请及时关闭数据库连接或使用using语法" + Environment.NewLine + ConnectionString);
|
|
}
|
|
innerConnection.StateChange -= OnStateChange;
|
|
base.Dispose(disposing);
|
|
disposedValue = true;
|
|
}
|
|
}
|
|
}
|