transaction.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2011 Google Inc. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package datastore
  5. import (
  6. "errors"
  7. "golang.org/x/net/context"
  8. "google.golang.org/appengine/internal"
  9. pb "google.golang.org/appengine/internal/datastore"
  10. )
  11. func init() {
  12. internal.RegisterTransactionSetter(func(x *pb.Query, t *pb.Transaction) {
  13. x.Transaction = t
  14. })
  15. internal.RegisterTransactionSetter(func(x *pb.GetRequest, t *pb.Transaction) {
  16. x.Transaction = t
  17. })
  18. internal.RegisterTransactionSetter(func(x *pb.PutRequest, t *pb.Transaction) {
  19. x.Transaction = t
  20. })
  21. internal.RegisterTransactionSetter(func(x *pb.DeleteRequest, t *pb.Transaction) {
  22. x.Transaction = t
  23. })
  24. }
  25. // ErrConcurrentTransaction is returned when a transaction is rolled back due
  26. // to a conflict with a concurrent transaction.
  27. var ErrConcurrentTransaction = errors.New("datastore: concurrent transaction")
  28. // RunInTransaction runs f in a transaction. It calls f with a transaction
  29. // context tc that f should use for all App Engine operations.
  30. //
  31. // If f returns nil, RunInTransaction attempts to commit the transaction,
  32. // returning nil if it succeeds. If the commit fails due to a conflicting
  33. // transaction, RunInTransaction retries f, each time with a new transaction
  34. // context. It gives up and returns ErrConcurrentTransaction after three
  35. // failed attempts. The number of attempts can be configured by specifying
  36. // TransactionOptions.Attempts.
  37. //
  38. // If f returns non-nil, then any datastore changes will not be applied and
  39. // RunInTransaction returns that same error. The function f is not retried.
  40. //
  41. // Note that when f returns, the transaction is not yet committed. Calling code
  42. // must be careful not to assume that any of f's changes have been committed
  43. // until RunInTransaction returns nil.
  44. //
  45. // Since f may be called multiple times, f should usually be idempotent.
  46. // datastore.Get is not idempotent when unmarshaling slice fields.
  47. //
  48. // Nested transactions are not supported; c may not be a transaction context.
  49. func RunInTransaction(c context.Context, f func(tc context.Context) error, opts *TransactionOptions) error {
  50. xg := false
  51. if opts != nil {
  52. xg = opts.XG
  53. }
  54. attempts := 3
  55. if opts != nil && opts.Attempts > 0 {
  56. attempts = opts.Attempts
  57. }
  58. for i := 0; i < attempts; i++ {
  59. if err := internal.RunTransactionOnce(c, f, xg); err != internal.ErrConcurrentTransaction {
  60. return err
  61. }
  62. }
  63. return ErrConcurrentTransaction
  64. }
  65. // TransactionOptions are the options for running a transaction.
  66. type TransactionOptions struct {
  67. // XG is whether the transaction can cross multiple entity groups. In
  68. // comparison, a single group transaction is one where all datastore keys
  69. // used have the same root key. Note that cross group transactions do not
  70. // have the same behavior as single group transactions. In particular, it
  71. // is much more likely to see partially applied transactions in different
  72. // entity groups, in global queries.
  73. // It is valid to set XG to true even if the transaction is within a
  74. // single entity group.
  75. XG bool
  76. // Attempts controls the number of retries to perform when commits fail
  77. // due to a conflicting transaction. If omitted, it defaults to 3.
  78. Attempts int
  79. }