本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.6KB

  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, readOnly bool, previousTransaction *pb.Transaction) (*pb.Transaction, error) {
  44. if transactionFromContext(c) != nil {
  45. return nil, 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 previousTransaction != nil {
  56. req.PreviousTransaction = previousTransaction
  57. }
  58. if readOnly {
  59. req.Mode = pb.BeginTransactionRequest_READ_ONLY.Enum()
  60. } else {
  61. req.Mode = pb.BeginTransactionRequest_READ_WRITE.Enum()
  62. }
  63. if err := Call(c, "datastore_v3", "BeginTransaction", req, &t.transaction); err != nil {
  64. return nil, err
  65. }
  66. // Call f, rolling back the transaction if f returns a non-nil error, or panics.
  67. // The panic is not recovered.
  68. defer func() {
  69. if t.finished {
  70. return
  71. }
  72. t.finished = true
  73. // Ignore the error return value, since we are already returning a non-nil
  74. // error (or we're panicking).
  75. Call(c, "datastore_v3", "Rollback", &t.transaction, &basepb.VoidProto{})
  76. }()
  77. if err := f(withTransaction(c, t)); err != nil {
  78. return &t.transaction, err
  79. }
  80. t.finished = true
  81. // Commit the transaction.
  82. res := &pb.CommitResponse{}
  83. err := Call(c, "datastore_v3", "Commit", &t.transaction, res)
  84. if ae, ok := err.(*APIError); ok {
  85. /* TODO: restore this conditional
  86. if appengine.IsDevAppServer() {
  87. */
  88. // The Python Dev AppServer raises an ApplicationError with error code 2 (which is
  89. // Error.CONCURRENT_TRANSACTION) and message "Concurrency exception.".
  90. if ae.Code == int32(pb.Error_BAD_REQUEST) && ae.Detail == "ApplicationError: 2 Concurrency exception." {
  91. return &t.transaction, ErrConcurrentTransaction
  92. }
  93. if ae.Code == int32(pb.Error_CONCURRENT_TRANSACTION) {
  94. return &t.transaction, ErrConcurrentTransaction
  95. }
  96. }
  97. return &t.transaction, err
  98. }
上海开阖软件有限公司 沪ICP备12045867号-1