Esempio n. 1
0
        public bool RunInTransaction(RunInTransactionDelegate block)
        {
            Log.D(TAG, "BEGIN transaction...");
            ForestDBBridge.Check(err => Native.c4db_beginTransaction(Forest, err));
            var success = false;

            try {
                success = block();
            } catch (CouchbaseLiteException) {
                Log.W(TAG, "Failed to run transaction");
                success = false;
                throw;
            } catch (Exception e) {
                success = false;
                throw new CouchbaseLiteException("Error running transaction", e)
                      {
                          Code = StatusCode.Exception
                      };
            } finally {
                Log.D(TAG, "END transaction (success={0})", success);
                ForestDBBridge.Check(err => Native.c4db_endTransaction(Forest, success, err));
                if (!InTransaction && Delegate != null)
                {
                    Delegate.StorageExitedTransaction(success);
                }
            }

            return(success);
        }
        public bool RunInTransaction(RunInTransactionDelegate block)
        {
            var status = false;
            var t      = Factory.StartNew(() =>
            {
                var keepGoing = false;
                int retries   = 0;
                do
                {
                    keepGoing = false;
                    if (!BeginTransaction())
                    {
                        throw Misc.CreateExceptionAndLog(Log.To.Database, StatusCode.DbError, TAG,
                                                         "Error beginning begin transaction");
                    }

                    try {
                        status = block();
                    } catch (CouchbaseLiteException e) {
                        if (e.Code == StatusCode.DbBusy)
                        {
                            // retry if locked out
                            if (transactionCount > 1)
                            {
                                break;
                            }

                            if (++retries > TRANSACTION_MAX_RETRIES)
                            {
                                Log.To.Database.E(TAG, "Db busy, too many retries, giving up");
                                break;
                            }

                            Log.To.Database.I(TAG, "Db busy, retrying transaction ({0})", retries);
                            Thread.Sleep(TRANSACTION_MAX_RETRY_DELAY);
                            keepGoing = true;
                        }
                        else
                        {
                            Log.To.Database.E(TAG, "Failed to run transaction, rethrowing...");
                            status = false;
                            throw;
                        }
                    } catch (Exception e) {
                        status = false;
                        throw Misc.CreateExceptionAndLog(Log.To.Database, e, TAG, "Error running transaction");
                    } finally {
                        EndTransaction(status);
                    }
                } while(keepGoing);
            });

            try {
                t.Wait();
            } catch (Exception e) {
                throw Misc.UnwrapAggregate(e);
            }

            return(status);
        }
Esempio n. 3
0
 /// <summary>
 /// Runs the delegate within a transaction. If the delegate returns false, 
 /// the transaction is rolled back.
 /// </summary>
 /// <returns>True if the transaction was committed, otherwise false.</returns>
 /// <param name="transactionDelegate">The delegate to run within a transaction.</param>
 public bool RunInTransaction(RunInTransactionDelegate transactionDelegate)
 {
     return Storage.RunInTransaction(() => transactionDelegate() ? 
         new Status(StatusCode.Ok) : new Status(StatusCode.Reserved)).Code == StatusCode.Ok;
 }
        /// <summary>
        /// Runs the delegate within a transaction. If the delegate returns false, 
        /// the transaction is rolled back.
        /// </summary>
        /// <returns>True if the transaction was committed, otherwise false.</returns>
        /// <param name="transactionDelegate">The delegate to run within a transaction.</param>
        public Boolean RunInTransaction(RunInTransactionDelegate transactionDelegate)
        {
            var shouldCommit = true;

            BeginTransaction();

            try
            {
                shouldCommit = transactionDelegate();
            }
            catch (Exception e)
            {
                shouldCommit = false;
                Log.E(Tag, e.ToString(), e);
                throw new RuntimeException(e);
            }
            finally
            {
                EndTransaction(shouldCommit);
            }

            return shouldCommit;
        }
        public bool RunInTransaction(RunInTransactionDelegate block)
        {
            Log.D(TAG, "BEGIN transaction...");
            ForestDBBridge.Check(err => Native.c4db_beginTransaction(Forest, err));
            var success = false;
            try {
                success = block();
            } catch(CouchbaseLiteException) {
                Log.W(TAG, "Failed to run transaction");
                success = false;
                throw;
            } catch(Exception e) {
                success = false;
                throw new CouchbaseLiteException("Error running transaction", e) { Code = StatusCode.Exception };
            } finally {
                Log.D(TAG, "END transaction (success={0})", success);
                ForestDBBridge.Check(err => Native.c4db_endTransaction(Forest, success, err));
                if (!InTransaction && Delegate != null) {
                    Delegate.StorageExitedTransaction(success);
                }
            }

            return success;
        }
Esempio n. 6
0
        /// <summary>
        /// Runs the delegate within a transaction. If the delegate returns false, 
        /// the transaction is rolled back.
        /// </summary>
        /// <returns>True if the transaction was committed, otherwise false.</returns>
        /// <param name="transactionDelegate">The delegate to run within a transaction.</param>
        public bool RunInTransaction(RunInTransactionDelegate transactionDelegate)
        {
            if (!IsOpen) {
                Log.W(TAG, "RunInTransaction called on closed database");
                return false;
            }

            return Storage.RunInTransaction(transactionDelegate);
        }
Esempio n. 7
0
 /// <summary>
 /// Runs the delegate within a transaction. If the delegate returns false,
 /// the transaction is rolled back.
 /// </summary>
 /// <returns>True if the transaction was committed, otherwise false.</returns>
 /// <param name="transactionDelegate">The delegate to run within a transaction.</param>
 public bool RunInTransaction(RunInTransactionDelegate transactionDelegate)
 {
     return(Base.RunInTransaction(transactionDelegate));
 }
        public bool RunInTransaction(RunInTransactionDelegate block)
        {
            var status = false;
            var t = Factory.StartNew(() =>
            {
                var keepGoing = false;
                int retries = 0;
                do {
                    keepGoing = false;
                    if (!BeginTransaction()) {
                        throw Misc.CreateExceptionAndLog(Log.To.Database, StatusCode.DbError, TAG,
                            "Error beginning begin transaction");
                    }

                    try {
                        status = block();
                    } catch (CouchbaseLiteException e) {
                        if (e.Code == StatusCode.DbBusy) {
                            // retry if locked out
                            if (transactionCount > 1) {
                                break;
                            }

                            if (++retries > TRANSACTION_MAX_RETRIES) {
                                Log.To.Database.E(TAG, "Db busy, too many retries, giving up");
                                break;
                            }

                            Log.To.Database.I(TAG, "Db busy, retrying transaction ({0})", retries);
                            Thread.Sleep(TRANSACTION_MAX_RETRY_DELAY);
                            keepGoing = true;
                        } else {
                            Log.To.Database.E(TAG, "Failed to run transaction, rethrowing...");
                            status = false;
                            throw;
                        }
                    } catch (Exception e) {
                        status = false;
                        throw Misc.CreateExceptionAndLog(Log.To.Database, e, TAG, "Error running transaction");
                    } finally {
                        EndTransaction(status);
                    }
                } while(keepGoing);
            });
            try {
                t.Wait();
            } catch(Exception e) {
                throw Misc.UnwrapAggregate(e);
            }

            return status;
        }
 /// <summary>
 /// Runs the delegate within a transaction. If the delegate returns false, 
 /// the transaction is rolled back.
 /// </summary>
 /// <returns>True if the transaction was committed, otherwise false.</returns>
 /// <param name="transactionDelegate">The delegate to run within a transaction.</param>
 public bool RunInTransaction(RunInTransactionDelegate transactionDelegate)
 {
     return Storage.RunInTransaction(transactionDelegate);
 }
        public bool RunInTransaction(RunInTransactionDelegate block)
        {
            Log.To.Database.V(TAG, "BEGIN transaction...");
            ForestDBBridge.Check(err => Native.c4db_beginTransaction(Forest, err));
            var success = false;
            try {
                success = block();
            } catch(CouchbaseLiteException) {
                Log.To.Database.W(TAG, "Failed to run transaction");
                success = false;
                throw;
            } catch(CBForestException e) {
                success = false;
                if(e.Domain == C4ErrorDomain.HTTP) {
                    var code = e.Code;
                    throw Misc.CreateExceptionAndLog(Log.To.Database, (StatusCode)code, TAG, "Failed to run transaction");
                }

                throw Misc.CreateExceptionAndLog(Log.To.Database, e, TAG, "Error running transaction");
            } catch(Exception e) {
                success = false;
                throw Misc.CreateExceptionAndLog(Log.To.Database, e, TAG, "Error running transaction");
            } finally {
                Log.To.Database.V(TAG, "END transaction (success={0})", success);
                ForestDBBridge.Check(err => Native.c4db_endTransaction(Forest, success, err));
                if(!InTransaction && Delegate != null) {
                    Delegate.StorageExitedTransaction(success);
                }
            }

            return success;
        }
Esempio n. 11
0
        /// <summary>
        /// Runs the delegate within a transaction. If the delegate returns false, 
        /// the transaction is rolled back.
        /// </summary>
        /// <returns>True if the transaction was committed, otherwise false.</returns>
        /// <param name="transactionDelegate">The delegate to run within a transaction.</param>
        public bool RunInTransaction(RunInTransactionDelegate transactionDelegate)
        {
            if(!IsOpen) {
                Log.To.Database.W(TAG, "{0} RunInTransaction called on closed database, returning false...", this);
                return false;
            }

            return Storage.RunInTransaction(transactionDelegate);
        }