/
transaction.go
79 lines (68 loc) · 2.85 KB
/
transaction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* Tencent is pleased to support the open source community by making 蓝鲸 available.
* Copyright (C) 2017-2018 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package local
import (
"context"
"fmt"
"configcenter/src/common"
"configcenter/src/common/blog"
"configcenter/src/common/metadata"
"go.mongodb.org/mongo-driver/mongo"
)
// CommitTransaction 提交事务
func (c *Mongo) CommitTransaction(ctx context.Context, cap *metadata.TxnCapable) error {
rid := ctx.Value(common.ContextRequestIDField)
reloadSession, err := c.tm.PrepareTransaction(cap, c.dbc)
if err != nil {
blog.Errorf("commit transaction, but prepare transaction failed, err: %v, rid: %v", err, rid)
return err
}
// reset the transaction state, so that we can commit the transaction after start the
// transaction immediately.
mongo.CmdbPrepareCommitOrAbort(reloadSession)
// we commit the transaction with the session id
err = reloadSession.CommitTransaction(ctx)
if err != nil {
return fmt.Errorf("commit transaction: %s failed, err: %v, rid: %v", cap.SessionID, err, rid)
}
err = c.tm.RemoveSessionKey(cap.SessionID)
if err != nil {
// this key has ttl, it's ok if we not delete it, cause this key has a ttl.
blog.Errorf("commit transaction, but delete txn session: %s key failed, err: %v, rid: %v", cap.SessionID, err, rid)
// do not return.
}
return nil
}
// AbortTransaction 取消事务
func (c *Mongo) AbortTransaction(ctx context.Context, cap *metadata.TxnCapable) error {
rid := ctx.Value(common.ContextRequestIDField)
reloadSession, err := c.tm.PrepareTransaction(cap, c.dbc)
if err != nil {
blog.Errorf("abort transaction, but prepare transaction failed, err: %v, rid: %v", err, rid)
return err
}
// reset the transaction state, so that we can abort the transaction after start the
// transaction immediately.
mongo.CmdbPrepareCommitOrAbort(reloadSession)
// we abort the transaction with the session id
err = reloadSession.AbortTransaction(ctx)
if err != nil {
return fmt.Errorf("abort transaction: %s failed, err: %v, rid: %v", cap.SessionID, err, rid)
}
err = c.tm.RemoveSessionKey(cap.SessionID)
if err != nil {
// this key has ttl, it's ok if we not delete it, cause this key has a ttl.
blog.Errorf("abort transaction, but delete txn session: %s key failed, err: %v, rid: %v", cap.SessionID, err, rid)
// do not return.
}
return nil
}