public MyCatTransactionScope(MyCatConnection con, Transaction trans,
                              MyCatTransaction simpleTransaction)
 {
     connection             = con;
     baseTransaction        = trans;
     this.simpleTransaction = simpleTransaction;
 }
 /// <include file='docs/mysqlcommand.xml' path='docs/ctor4/*'/>
 public MyCatCommand(string cmdText, MyCatConnection connection,
                     MyCatTransaction transaction)
     :
     this(cmdText, connection)
 {
     curTransaction = transaction;
 }
        /// <include file='docs/MyCatConnection.xml' path='docs/BeginTransaction1/*'/>
        public new MyCatTransaction BeginTransaction(IsolationLevel iso)
        {
            //TODO: check note in help
            if (State != ConnectionState.Open)
            {
                Throw(new InvalidOperationException(Resources.ConnectionNotOpen));
            }

            // First check to see if we are in a current transaction
            if (driver.HasStatus(ServerStatusFlags.InTransaction))
            {
                Throw(new InvalidOperationException(Resources.NoNestedTransactions));
            }

            MyCatTransaction t = new MyCatTransaction(this, iso);

            MyCatCommand cmd = new MyCatCommand("", this);

            cmd.CommandText = "SET SESSION TRANSACTION ISOLATION LEVEL ";
            switch (iso)
            {
            case IsolationLevel.ReadCommitted:
                cmd.CommandText += "READ COMMITTED";
                break;

            case IsolationLevel.ReadUncommitted:
                cmd.CommandText += "READ UNCOMMITTED";
                break;

            case IsolationLevel.RepeatableRead:
                cmd.CommandText += "REPEATABLE READ";
                break;

            case IsolationLevel.Serializable:
                cmd.CommandText += "SERIALIZABLE";
                break;

            case IsolationLevel.Chaos:
                Throw(new NotSupportedException(Resources.ChaosNotSupported));
                break;

            case IsolationLevel.Snapshot:
                Throw(new NotSupportedException(Resources.SnapshotNotSupported));
                break;
            }

            cmd.ExecuteNonQuery();

            cmd.CommandText = "BEGIN";
            cmd.ExecuteNonQuery();

            return(t);
        }
        internal void CloseFully()
        {
            if (Settings.Pooling && driver.IsOpen)
            {
                // if we are in a transaction, roll it back
                if (driver.HasStatus(ServerStatusFlags.InTransaction))
                {
                    MyCatTransaction t = new MyCatTransaction(this, IsolationLevel.Unspecified);
                    t.Rollback();
                }

                MyCatPoolManager.ReleaseConnection(driver);
            }
            driver.Close();
            driver = null;
        }
        void IPromotableSinglePhaseNotification.Initialize()
        {
            string valueName = Enum.GetName(
                typeof(System.Transactions.IsolationLevel), baseTransaction.IsolationLevel);

            System.Data.IsolationLevel dataLevel = (System.Data.IsolationLevel)Enum.Parse(
                typeof(System.Data.IsolationLevel), valueName);
            MyCatTransaction simpleTransaction = connection.BeginTransaction(dataLevel);

            // We need to save the per-thread scope stack locally.
            // We cannot always use thread static variable in rollback: when scope
            // times out, rollback is issued by another thread.
            if (globalScopeStack == null)
            {
                globalScopeStack = new Stack <MyCatTransactionScope>();
            }

            scopeStack = globalScopeStack;
            scopeStack.Push(new MyCatTransactionScope(connection, baseTransaction,
                                                      simpleTransaction));
        }
        public Task <MyCatTransaction> BeginTransactionAsync(IsolationLevel iso, CancellationToken cancellationToken)
        {
            var result = new TaskCompletionSource <MyCatTransaction>();

            if (cancellationToken == CancellationToken.None || !cancellationToken.IsCancellationRequested)
            {
                try
                {
                    MyCatTransaction tranResult = BeginTransaction(iso);
                    result.SetResult(tranResult);
                }
                catch (Exception ex)
                {
                    result.SetException(ex);
                }
            }
            else
            {
                result.SetCanceled();
            }

            return(result.Task);
        }
Example #7
0
        /// <summary>
        /// Executes a single command against a MySQL database, possibly inside an existing transaction.
        /// </summary>
        /// <param name="connection"><see cref="MyCatConnection"/> object to use for the command</param>
        /// <param name="transaction"><see cref="MyCatTransaction"/> object to use for the command</param>
        /// <param name="commandText">Command text to use</param>
        /// <param name="commandParameters">Array of <see cref="MyCatParameter"/> objects to use with the command</param>
        /// <param name="ExternalConn">True if the connection should be preserved, false if not</param>
        /// <returns><see cref="MyCatDataReader"/> object ready to read the results of the command</returns>
        private static MyCatDataReader ExecuteReader(MyCatConnection connection, MyCatTransaction transaction, string commandText, MyCatParameter[] commandParameters, bool ExternalConn)
        {
            //create a command and prepare it for execution
            MyCatCommand cmd = new MyCatCommand();

            cmd.Connection  = connection;
            cmd.Transaction = transaction;
            cmd.CommandText = commandText;
            cmd.CommandType = CommandType.Text;

            if (commandParameters != null)
            {
                foreach (MyCatParameter p in commandParameters)
                {
                    cmd.Parameters.Add(p);
                }
            }

            //create a reader
            MyCatDataReader dr;

            // call ExecuteReader with the appropriate CommandBehavior
            if (ExternalConn)
            {
                dr = cmd.ExecuteReader();
            }
            else
            {
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }

            // detach the SqlParameters from the command object, so they can be used again.
            cmd.Parameters.Clear();

            return(dr);
        }
Example #8
0
        private static Task <MyCatDataReader> ExecuteReaderAsync(MyCatConnection connection, MyCatTransaction transaction, string commandText, MyCatParameter[] commandParameters, bool ExternalConn, CancellationToken cancellationToken)
        {
            var result = new TaskCompletionSource <MyCatDataReader>();

            if (cancellationToken == CancellationToken.None || !cancellationToken.IsCancellationRequested)
            {
                try
                {
                    var reader = ExecuteReader(connection, transaction, commandText, commandParameters, ExternalConn);
                    result.SetResult(reader);
                }
                catch (Exception ex)
                {
                    result.SetException(ex);
                }
            }
            else
            {
                result.SetCanceled();
            }
            return(result.Task);
        }
Example #9
0
 /// <summary>
 /// Async version of ExecuteReader
 /// </summary>
 /// <param name="connection"><see cref="MyCatConnection"/> object to use for the command</param>
 /// <param name="transaction"><see cref="MyCatTransaction"/> object to use for the command</param>
 /// <param name="commandText">Command text to use</param>
 /// <param name="commandParameters">Array of <see cref="MyCatParameter"/> objects to use with the command</param>
 /// <param name="ExternalConn">True if the connection should be preserved, false if not</param>
 /// <returns><see cref="MyCatDataReader"/> object ready to read the results of the command</returns>
 private static Task <MyCatDataReader> ExecuteReaderAsync(MyCatConnection connection, MyCatTransaction transaction, string commandText, MyCatParameter[] commandParameters, bool ExternalConn)
 {
     return(ExecuteReaderAsync(connection, transaction, commandText, commandParameters, ExternalConn, CancellationToken.None));
 }