Method: Google::Cloud::Datastore::Dataset#transaction
- Defined in:
- lib/google/cloud/datastore/dataset.rb
#transaction(deadline: nil, previous_transaction: nil) {|tx| ... } ⇒ Object
Creates a Datastore Transaction.
Transactions using the block syntax are committed upon block completion and are automatically retried when known errors are raised during commit. All other errors will be passed on.
All changes are accumulated in memory until the block completes.
Transactions will be automatically retried when possible, until
deadline is reached. This operation makes separate API requests to
begin and commit the transaction.
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 |
# File 'lib/google/cloud/datastore/dataset.rb', line 712 def transaction deadline: nil, previous_transaction: nil deadline = validate_deadline deadline backoff = 1.0 start_time = Time.now tx = Transaction.new \ service, previous_transaction: previous_transaction return tx unless block_given? begin yield tx tx.commit rescue Google::Cloud::UnavailableError => e # Re-raise if deadline has passed raise e if Time.now - start_time > deadline # Sleep with incremental backoff sleep backoff *= 1.3 # Create new transaction and retry the block tx = Transaction.new service, previous_transaction: tx.id retry rescue StandardError begin tx.rollback rescue StandardError raise TransactionError, "Transaction failed to commit and rollback." end raise TransactionError, "Transaction failed to commit." end end |