-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtransaction.h
More file actions
77 lines (59 loc) · 2.22 KB
/
Copy pathtransaction.h
File metadata and controls
77 lines (59 loc) · 2.22 KB
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
//
//
// Tencent is pleased to support the open source community by making tRPC available.
//
// Copyright (C) 2024 THL A29 Limited, a Tencent company.
// All rights reserved.
//
// If you have downloaded a copy of the tRPC source code from Tencent,
// please note that tRPC source code is licensed under the GNU General Public License Version 2.0 (GPLv2),
// A copy of the GPLv2 is included in this file.
//
//
#pragma once
#include "trpc/client/mysql/executor/mysql_executor.h"
namespace trpc::mysql {
class TransactionHandle : public RefCounted<TransactionHandle> {
public:
/// @note When the handle has been moved, it's state would be set to kInValid
enum class TxState { kNotInited, kStarted, kRollBacked, kCommitted, kInValid };
explicit TransactionHandle(RefPtr<MysqlExecutor>&& executor) : executor_(std::move(executor)) {}
TransactionHandle() : executor_(nullptr) {}
TransactionHandle& operator=(TransactionHandle&& other) noexcept {
state_ = other.state_;
executor_ = std::move(other.executor_);
other.state_ = TxState::kInValid;
other.executor_ = nullptr;
return *this;
}
TransactionHandle(TransactionHandle&& other) noexcept {
state_ = other.state_;
executor_ = std::move(other.executor_);
other.state_ = TxState::kInValid;
other.executor_ = nullptr;
}
TransactionHandle& operator=(const TransactionHandle& other) = delete;
TransactionHandle(const TransactionHandle& other) = delete;
~TransactionHandle() {
// usually executor_ would be nullptr(be reclaimed)
if (executor_) {
TRPC_FMT_ERROR("TransactionHandle destructed but executor is not reclaimed.");
executor_->Close();
}
state_ = TxState::kInValid;
}
void SetState(TxState state) { state_ = state; }
TxState GetState() { return state_; }
bool SetExecutor(RefPtr<MysqlExecutor>&& executor) {
if (executor_) return false;
executor_ = std::move(executor);
return true;
}
RefPtr<MysqlExecutor> GetExecutor() { return executor_; }
RefPtr<MysqlExecutor>&& TransferExecutor() { return std::move(executor_); }
private:
RefPtr<MysqlExecutor> executor_{nullptr};
TxState state_{TxState::kNotInited};
};
using TxHandlePtr = RefPtr<TransactionHandle>;
} // namespace trpc::mysql