|
|
@ -20,49 +20,80 @@ namespace Apewer.Source |
|
|
|
|
|
|
|
#region Connection
|
|
|
|
|
|
|
|
string _key = TextUtility.Key(); |
|
|
|
Timeout _timeout = null; |
|
|
|
IDbConnection _conn = null; |
|
|
|
bool _disposed = false; |
|
|
|
|
|
|
|
/// <summary></summary>
|
|
|
|
internal protected object Locker = new object(); |
|
|
|
|
|
|
|
/// <summary>此连接的唯一标识。</summary>
|
|
|
|
public virtual string Key { get => _key; } |
|
|
|
|
|
|
|
/// <summary></summary>
|
|
|
|
public Timeout Timeout { get => _timeout; set => _timeout = value ?? Timeout.Default; } |
|
|
|
public virtual Timeout Timeout { get => _timeout; set => _timeout = value ?? Timeout.Default; } |
|
|
|
|
|
|
|
/// <summary>获取当前的 SqlConnection 对象。</summary>
|
|
|
|
public IDbConnection Connection { get => _conn; } |
|
|
|
|
|
|
|
/// <summary></summary>
|
|
|
|
public bool Online { get => _conn == null ? false : (_conn.State == ConnectionState.Open); } |
|
|
|
/// <summary>此连接已释放。</summary>
|
|
|
|
public virtual bool Disposed { get => _disposed; } |
|
|
|
|
|
|
|
/// <summary>连接处于打开状态。</summary>
|
|
|
|
public virtual bool Online { get => _conn == null ? false : (_conn.State == ConnectionState.Open); } |
|
|
|
|
|
|
|
/// <summary>连接字符串。</summary>
|
|
|
|
public abstract string ConnectionString { get; } |
|
|
|
|
|
|
|
/// <summary>关闭连接后,执行的方法。</summary>
|
|
|
|
public virtual Action<IDbAdo> OnClosed { get; set; } |
|
|
|
|
|
|
|
/// <summary>连接数据库,若未连接则尝试连接,获取连接成功的状态。</summary>
|
|
|
|
public string Connect() |
|
|
|
{ |
|
|
|
if (_conn == null) _conn = GetConnection(); |
|
|
|
if (_disposed) |
|
|
|
{ |
|
|
|
if (ThrowAdoException) throw new ObjectDisposedException(GetType().Name); |
|
|
|
return "当前连接已释放,无法再次连接。"; |
|
|
|
} |
|
|
|
|
|
|
|
if (_conn == null) _conn = GetConnection(); |
|
|
|
if (_conn.State == ConnectionState.Open) return null; |
|
|
|
|
|
|
|
if (ThrowAdoException) |
|
|
|
{ |
|
|
|
_conn.Open(); |
|
|
|
if (_conn.State != ConnectionState.Open) |
|
|
|
{ |
|
|
|
var message = $"连接后状态为 {_conn.State},无法验证打开状态。"; |
|
|
|
Logger.Error(this, "Connect", message); |
|
|
|
throw new SqlException(message); |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
try |
|
|
|
{ |
|
|
|
_conn.Open(); |
|
|
|
if (_conn.State == ConnectionState.Open) return null; |
|
|
|
var message = $"连接后状态为 {_conn.State},无法验证打开状态。"; |
|
|
|
Logger.Error(this, "Connect", message); |
|
|
|
return "连接失败," + message; |
|
|
|
if (_conn.State != ConnectionState.Open) |
|
|
|
{ |
|
|
|
var message = $"连接后状态为 {_conn.State},无法验证打开状态。"; |
|
|
|
Logger.Error(this, "Connect", message); |
|
|
|
return "连接失败," + message; |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
Logger.Error(this, "Connect", ex.Message); |
|
|
|
Close(); |
|
|
|
if (ThrowAdoException) throw; |
|
|
|
return ex.Message; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>更改已打开的数据库。</summary>
|
|
|
|
public string Change(string store) |
|
|
|
public virtual string Change(string store) |
|
|
|
{ |
|
|
|
if (store.IsEmpty()) |
|
|
|
{ |
|
|
@ -89,6 +120,9 @@ namespace Apewer.Source |
|
|
|
/// <summary>关闭连接,并释放对象所占用的系统资源。</summary>
|
|
|
|
public virtual void Close() |
|
|
|
{ |
|
|
|
if (_disposed) return; |
|
|
|
_disposed = true; |
|
|
|
|
|
|
|
if (_conn != null) |
|
|
|
{ |
|
|
|
if (_transaction != null) |
|
|
@ -97,8 +131,10 @@ namespace Apewer.Source |
|
|
|
else Rollback(); |
|
|
|
_transaction = null; |
|
|
|
} |
|
|
|
try { ClearPool(); } catch { } |
|
|
|
try { _conn.Close(); } catch { } |
|
|
|
|
|
|
|
var onClosed = OnClosed; |
|
|
|
if (onClosed != null) onClosed.Invoke(this); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -108,9 +144,6 @@ namespace Apewer.Source |
|
|
|
Close(); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>清理连接池,释放当前连接。</summary>
|
|
|
|
protected virtual void ClearPool(bool all = false) { } |
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Transaction
|
|
|
@ -121,7 +154,7 @@ namespace Apewer.Source |
|
|
|
private bool _autocommit = false; |
|
|
|
|
|
|
|
/// <summary>获取已启动的事务。</summary>
|
|
|
|
public IDbTransaction Transaction { get => _transaction; } |
|
|
|
public virtual IDbTransaction Transaction { get => _transaction; } |
|
|
|
|
|
|
|
string Begin(bool commit, Class<IsolationLevel> isolation) |
|
|
|
{ |
|
|
@ -160,7 +193,7 @@ namespace Apewer.Source |
|
|
|
/// <para>Unspecified = -1<br />正在使用与指定隔离级别不同的隔离级别,但是无法确定该级别。</para>
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="commit">在连接的生命周期结束时未结束事务,指定 TRUE 将自动提交,指定 FALSE 将自动回滚。</param>
|
|
|
|
public string Begin(bool commit = false) => Begin(commit, null); |
|
|
|
public virtual string Begin(bool commit = false) => Begin(commit, null); |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <para>使用指定的事务锁定行为启动事务。</para>
|
|
|
@ -174,10 +207,10 @@ namespace Apewer.Source |
|
|
|
/// </summary>
|
|
|
|
/// <param name="commit">在连接的生命周期结束时未结束事务,指定 TRUE 将自动提交,指定 FALSE 将自动回滚。</param>
|
|
|
|
/// <param name="isolation">指定事务锁定行为,不指定时将使用默认值。</param>
|
|
|
|
public string Begin(bool commit, IsolationLevel isolation) => Begin(commit, new Class<IsolationLevel>(isolation)); |
|
|
|
public virtual string Begin(bool commit, IsolationLevel isolation) => Begin(commit, new Class<IsolationLevel>(isolation)); |
|
|
|
|
|
|
|
/// <summary>提交事务。</summary>
|
|
|
|
public string Commit() |
|
|
|
public virtual string Commit() |
|
|
|
{ |
|
|
|
if (_transaction == null) |
|
|
|
{ |
|
|
@ -203,7 +236,7 @@ namespace Apewer.Source |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>从挂起状态回滚事务。</summary>
|
|
|
|
public string Rollback() |
|
|
|
public virtual string Rollback() |
|
|
|
{ |
|
|
|
if (_transaction == null) |
|
|
|
{ |
|
|
|