transaction.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2014 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 internal
  5. // This file implements hooks for applying datastore transactions.
  6. import (
  7. "errors"
  8. "reflect"
  9. "github.com/golang/protobuf/proto"
  10. netcontext "golang.org/x/net/context"
  11. basepb "google.golang.org/appengine/internal/base"
  12. pb "google.golang.org/appengine/internal/datastore"
  13. )
  14. var transactionSetters = make(map[reflect.Type]reflect.Value)
  15. // RegisterTransactionSetter registers a function that sets transaction information
  16. // in a protocol buffer message. f should be a function with two arguments,
  17. // the first being a protocol buffer type, and the second being *datastore.Transaction.
  18. func RegisterTransactionSetter(f interface{}) {
  19. v := reflect.ValueOf(f)
  20. transactionSetters[v.Type().In(0)] = v
  21. }
  22. // applyTransaction applies the transaction t to message pb
  23. // by using the relevant setter passed to RegisterTransactionSetter.
  24. func applyTransaction(pb proto.Message, t *pb.Transaction) {
  25. v := reflect.ValueOf(pb)
  26. if f, ok := transactionSetters[v.Type()]; ok {
  27. f.Call([]reflect.Value{v, reflect.ValueOf(t)})
  28. }
  29. }
  30. var transactionKey = "used for *Transaction"
  31. func transactionFromContext(ctx netcontext.Context) *transaction {
  32. t, _ := ctx.Value(&transactionKey).(*transaction)
  33. return t
  34. }
  35. func withTransaction(ctx netcontext.Context, t *transaction) netcontext.Context {
  36. return netcontext.WithValue(ctx, &transactionKey, t)
  37. }
  38. type transaction struct {
  39. transaction pb.Transaction
  40. finished bool
  41. }
  42. var ErrConcurrentTransaction = errors.New("internal: concurrent transaction")
  43. func RunTransactionOnce(c netcontext.Context, f func(netcontext.Context) error, xg bool) error {
  44. if transactionFromContext(c) != nil {
  45. return errors.New("nested transactions are not supported")
  46. }
  47. // Begin the transaction.
  48. t := &transaction{}
  49. req := &pb.BeginTransactionRequest{
  50. App: proto.String(FullyQualifiedAppID(c)),
  51. }
  52. if xg {
  53. req.AllowMultipleEg = proto.Bool(true)
  54. }
  55. if err := Call(c, "datastore_v3", "BeginTransaction", req, &t.transaction); err != nil {
  56. return err
  57. }
  58. // Call f, rolling back the transaction if f returns a non-nil error, or panics.
  59. // The panic is not recovered.
  60. defer func() {
  61. if t.finished {
  62. return
  63. }
  64. t.finished = true
  65. // Ignore the error return value, since we are already returning a non-nil
  66. // error (or we're panicking).
  67. Call(c, "datastore_v3", "Rollback", &t.transaction, &basepb.VoidProto{})
  68. }()
  69. if err := f(withTransaction(c, t)); err != nil {
  70. return err
  71. }
  72. t.finished = true
  73. // Commit the transaction.
  74. res := &pb.CommitResponse{}
  75. err := Call(c, "datastore_v3", "Commit", &t.transaction, res)
  76. if ae, ok := err.(*APIError); ok {
  77. /* TODO: restore this conditional
  78. if appengine.IsDevAppServer() {
  79. */
  80. // The Python Dev AppServer raises an ApplicationError with error code 2 (which is
  81. // Error.CONCURRENT_TRANSACTION) and message "Concurrency exception.".
  82. if ae.Code == int32(pb.Error_BAD_REQUEST) && ae.Detail == "ApplicationError: 2 Concurrency exception." {
  83. return ErrConcurrentTransaction
  84. }
  85. if ae.Code == int32(pb.Error_CONCURRENT_TRANSACTION) {
  86. return ErrConcurrentTransaction
  87. }
  88. }
  89. return err
  90. }