Esempio n. 1
0
        private async Task <MyCatTransaction> BeginDbTransactionAsync(IsolationLevel isolationLevel, IOBehavior ioBehavior, CancellationToken cancellationToken)
        {
            if (State != ConnectionState.Open)
            {
                throw new InvalidOperationException("Connection is not open.");
            }
            if (CurrentTransaction != null)
            {
                throw new InvalidOperationException("Transactions may not be nested.");
            }

            string isolationLevelValue;

            switch (isolationLevel)
            {
            case IsolationLevel.ReadUncommitted:
                isolationLevelValue = "read uncommitted";
                break;

            case IsolationLevel.ReadCommitted:
                isolationLevelValue = "read committed";
                break;

            case IsolationLevel.Unspecified:
            // "In terms of the SQL:1992 transaction isolation levels, the default InnoDB level is REPEATABLE READ." - http://dev.mysql.com/doc/refman/5.7/en/innodb-transaction-model.html
            case IsolationLevel.RepeatableRead:
                isolationLevelValue = "repeatable read";
                break;

            case IsolationLevel.Serializable:
                isolationLevelValue = "serializable";
                break;

            case IsolationLevel.Chaos:
            case IsolationLevel.Snapshot:
            default:
                throw new NotSupportedException("IsolationLevel.{0} is not supported.".FormatInvariant(isolationLevel));
            }

            using (var cmd = new MyCatCommand("set session transaction isolation level " + isolationLevelValue + "; start transaction;", this))
                await cmd.ExecuteNonQueryAsync(ioBehavior, cancellationToken).ConfigureAwait(false);

            var transaction = new MyCatTransaction(this, isolationLevel);

            CurrentTransaction = transaction;
            return(transaction);
        }
Esempio n. 2
0
        internal async Task RollbackAsync(IOBehavior ioBehavior, CancellationToken cancellationToken)
        {
            VerifyNotDisposed();
            if (m_isFinished)
            {
                throw new InvalidOperationException("Already committed or rolled back.");
            }

            if (m_connection.CurrentTransaction == this)
            {
                using (var cmd = new MyCatCommand("rollback", m_connection, this))
                    await cmd.ExecuteNonQueryAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
                m_connection.CurrentTransaction = null;
                m_isFinished = true;
            }
            else if (m_connection.CurrentTransaction != null)
            {
                throw new InvalidOperationException("This is not the active transaction.");
            }
            else if (m_connection.CurrentTransaction == null)
            {
                throw new InvalidOperationException("There is no active transaction.");
            }
        }