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

tx_at.go 2.1 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package sql
  18. import (
  19. "github.com/pkg/errors"
  20. "seata.apache.org/seata-go/pkg/datasource/sql/undo"
  21. )
  22. // ATTx
  23. type ATTx struct {
  24. tx *Tx
  25. }
  26. // Commit do commit action
  27. // case 1. no open global-transaction, just do local transaction commit
  28. // case 2. not need flush undolog, is XA mode, do local transaction commit
  29. // case 3. need run AT transaction
  30. func (tx *ATTx) Commit() error {
  31. tx.tx.beforeCommit()
  32. return tx.commitOnAT()
  33. }
  34. func (tx *ATTx) Rollback() error {
  35. err := tx.tx.Rollback()
  36. if err != nil {
  37. originTx := tx.tx
  38. if originTx.tranCtx.OpenGlobalTransaction() && originTx.tranCtx.IsBranchRegistered() {
  39. originTx.report(false)
  40. }
  41. }
  42. return err
  43. }
  44. // commitOnAT
  45. func (tx *ATTx) commitOnAT() error {
  46. originTx := tx.tx
  47. if err := originTx.register(originTx.tranCtx); err != nil {
  48. return err
  49. }
  50. undoLogMgr, err := undo.GetUndoLogManager(originTx.tranCtx.DBType)
  51. if err != nil {
  52. return err
  53. }
  54. if err = undoLogMgr.FlushUndoLog(originTx.tranCtx, originTx.conn.targetConn); err != nil {
  55. if rerr := originTx.report(false); rerr != nil {
  56. return errors.WithStack(rerr)
  57. }
  58. return errors.WithStack(err)
  59. }
  60. if err := originTx.commitOnLocal(); err != nil {
  61. if rerr := originTx.report(false); rerr != nil {
  62. return errors.WithStack(rerr)
  63. }
  64. return errors.WithStack(err)
  65. }
  66. originTx.report(true)
  67. return nil
  68. }