/// <summary> /// 如果与方法 BeginTransaction 匹配,则提交数据库事务。 /// </summary> /// <returns>成功提交事务则为 true,否则为 false。</returns> public virtual bool CommitTransaction() { if (_transactionScope == null) { return(false); } Tracer.Debug("Commiting transcation."); _transaction.Commit(); _connMaster?.TryClose(); _transactionScope.Dispose(); _transaction = null; _transactionScope = null; return(true); }
/// <summary> /// 如果与方法 BeginTransaction 匹配,则回滚数据库事务。 /// </summary> /// <returns>成功回滚事务则为 true,否则为 false。</returns> public virtual bool RollbackTransaction() { if (_transactionScope == null) { return(false); } Tracer.Debug("Rollbacking transcation."); _transaction.Rollback(); _connMaster?.TryClose(); _transactionScope.Dispose(); _transaction = null; _transactionScope = null; return(true); }
/// <summary> /// 使用指定锁定行为启动一个数据库事务。 /// </summary> /// <param name="level">事务的锁定行为。</param> /// <returns>如果当前实例首次启动事务,则为 true,否则为 false。</returns> public virtual bool BeginTransaction(IsolationLevel level = IsolationLevel.ReadCommitted) { if (Transaction != null) { return(false); } if (TransactionScopeConnections.GetConnection(this) != null) { return(false); } Connection.TryOpen(); Tracer.Debug("Starting transcation."); _transaction = Connection.BeginTransaction(Provider.AmendIsolationLevel(level)); _transactionScope = new DbTransactionScope(ConnectionString, _transaction); return(true); }