NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
scope_transaction.hpp
浏览该文件的文档.
1#ifndef NEFORCE_DATABASE_TRANSACTION_GUARD_HPP__
2#define NEFORCE_DATABASE_TRANSACTION_GUARD_HPP__
3
21
23NEFORCE_BEGIN_NAMESPACE__
24
30
44template <typename Connect>
46private:
47 Connect* conn_ = nullptr;
48 bool committed_ = false;
49
50public:
57 explicit scope_transaction(Connect& conn) :
58 conn_(&conn) {
59 conn_->begin();
60 }
61
68 ~scope_transaction() noexcept {
69 if (conn_ != nullptr && !committed_) {
70 try {
71 conn_->rollback();
72 // NOLINTNEXTLINE(bugprone-empty-catch)
73 } catch (...) {
74 // ignore
75 }
76 }
77 }
78
79 scope_transaction(const scope_transaction&) = delete;
80 scope_transaction& operator=(const scope_transaction&) = delete;
81
89 conn_(other.conn_),
90 committed_(other.committed_) {
91 other.conn_ = nullptr;
92 other.committed_ = true;
93 }
94
103 if (addressof(other) != this) {
104 if (conn_ != nullptr && !committed_) {
105 try {
106 conn_->rollback();
107 // NOLINTNEXTLINE(bugprone-empty-catch)
108 } catch (...) {
109 // ignore
110 }
111 }
112 conn_ = other.conn_;
113 committed_ = other.committed_;
114 other.conn_ = nullptr;
115 other.committed_ = true;
116 }
117 return *this;
118 }
119
127 void commit() {
128 if (conn_ != nullptr && !committed_) {
129 conn_->commit();
130 committed_ = true;
131 }
132 }
133
138 NEFORCE_NODISCARD bool committed() const noexcept { return committed_; }
139};
140
146template <typename Connect>
147NEFORCE_NODISCARD scope_transaction<Connect> make_transaction(Connect& conn) {
148 return scope_transaction<Connect>(conn);
149}
150 // Database
152
153NEFORCE_END_NAMESPACE__
154#endif // NEFORCE_DATABASE_TRANSACTION_GUARD_HPP__
scope_transaction(scope_transaction &&other) noexcept
移动构造函数
scope_transaction & operator=(scope_transaction &&other) noexcept
移动赋值运算符
~scope_transaction() noexcept
析构函数
bool committed() const noexcept
检查事务是否已提交
scope_transaction(Connect &conn)
构造函数,开始事务
数据库抽象接口层
constexpr T * addressof(T &x) noexcept
获取对象的地址
scope_transaction< Connect > make_transaction(Connect &conn)
事务守卫的便捷构造函数