Beispiel #1
0
        public void ExecuteTransaction(IList <DataRequest> requests)
        {
            // the same connector will not execute transactions in parallel
            lock (_transactionSync)
            {
                var status = new TransactionState();

                status.Initialize(requests, CacheClients);

                status.CheckStatus(TransactionState.Status.Initialized);

                status.TryExecuteAsSingleStage();

                if (status.CurrentStatus == TransactionState.Status.Completed)
                {
                    TransactionStatistics.ExecutedAsSingleStage();

                    return;
                }


                // stage zero : send the transaction request to the servers and wait for them to acquire write locks
                status.AcquireLock();

                status.CheckStatus(TransactionState.Status.LocksAcquired);


                // first stage: the durable transaction is written in the transaction log
                status.ProceedWithFirstStage();


                // second stage: commit or rollback only the servers that processed successfully the first stage
                // (if a server answered with an exception response the transaction was already rolled back on this server)

                if (status.CurrentStatus == TransactionState.Status.FirstStageCompleted)
                {
                    // commit the transaction
                    status.CommitSecondStage();

                    status.CheckStatus(TransactionState.Status.SecondStageCompleted);

                    // close the transaction
                    status.CloseTransaction();

                    status.CheckStatus(TransactionState.Status.Completed);

                    TransactionStatistics.NewTransactionCompleted();
                }
                else
                {
                    status.Rollback();

                    throw new CacheException(
                              $"Error in two stage transaction. The transaction was successfully rolled back: {status.ExceptionType}", status.ExceptionType);
                }
            }
        }
Beispiel #2
0
        public void Delete(IEnumerable <long> ids, bool inTransaction = true)
        {
            TransactionState txn = null;

            if (inTransaction)
            {
                txn = Database.StartTransaction();
            }

            try
            {
                var pages = ids.SplitIntoPages(500);
                foreach (var page in pages)
                {
                    var sql     = new SqlStatement();
                    var builder = new StringBuilder();

                    builder.Append("delete from ");
                    builder.Append(sql.Dialect.FormatTableName(this.EntityType.NativeName));
                    builder.Append(" where ");
                    builder.Append(sql.Dialect.FormatColumnName(this.EntityType.GetKeyFields()[0].NativeName));
                    builder.Append(" in (");
                    sql.AddValueList <long>(builder, page);
                    builder.Append(")");

                    sql.CommandText = builder.ToString();
                    Database.ExecuteNonQuery(sql);
                }

                // ok...
                if (txn != null)
                {
                    txn.Commit();
                }
            }
            catch (Exception ex)
            {
                if (txn != null)
                {
                    txn.Rollback(ex);
                }

                throw new InvalidOperationException("The operation failed", ex);
            }
            finally
            {
                if (txn != null)
                {
                    txn.Dispose();
                }
            }
        }