Skip to content

Commit

Permalink
优化 TransAction,中间处理异常时,自动全部回滚
Browse files Browse the repository at this point in the history
  • Loading branch information
fasiondog committed Oct 6, 2024
1 parent 14e9e0d commit 8381d8c
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions hikyuu_cpp/hikyuu/utilities/db_connect/AutoTransAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,7 @@ namespace hku {

/**
* 自动事务处理,在代码块中自动启动事务,并在代码块退出后自动提交
* @code
* try {
* AutoTransAction trans(driver);
* ...
* } catch (...) {
* driver->rollback();
* }
* @endcode
* @note 事务提交失败时,这里并没有处理回滚
* @note 当有多个数据更改是,如果在程序处理中间发送异常时,可能导致数据被部分提交
* @ingroup DBConnect
*/
class AutoTransAction {
Expand Down Expand Up @@ -63,9 +55,10 @@ class AutoTransAction {
};

/**
* 手动事务处理,允许嵌套提交事务,退出时自动提交
* 手动事务处理,允许嵌套启动事务,必须手工启动和提交事务,自动退出时不会自动提交事务,而是回滚!
* @details 必须有一次有效的手工启动事务,多次嵌套启动事务将被视为一次事务处理。
* 手工提交一次事务后,如有新的事务处理,须再次手工启动事务。
* @note 嵌套时,可能发送内部事务已提交,但外部事务处理失败被回滚(仅回滚相应事务处理的部分)的情况
* @ingroup DBConnect
*/
class TransAction {
Expand All @@ -74,17 +67,23 @@ class TransAction {
* 构造函数
* @param driver 数据库连接指针
*/
explicit TransAction(const DBConnectPtr& driver) : m_driver(driver), m_committed(true) {}
explicit TransAction(const DBConnectPtr& driver)
: m_driver(driver), m_committed(false), m_started(true) {
HKU_CHECK(driver, "Null DBConnectPtr!");
m_driver->transaction();
}

/** 析构函数 */
~TransAction() {
try {
if (!m_committed) {
m_driver->commit();
}
} catch (...) {
HKU_ERROR("Transaction commit failed!");
m_driver.reset();
// 如果没有主动提交事务,视为需要回滚
if (!m_started) {
HKU_WARN("Not manul begin transaction!");
return;
}

if (m_started && !m_committed) {
HKU_WARN("The transaction is rolled back!");
m_driver->rollback();
}
}

Expand All @@ -95,25 +94,23 @@ class TransAction {

/** 启动事务 */
void begin() {
if (m_committed) {
if (!m_started) {
m_driver->transaction();
m_committed = false;
m_started = true;
}
}

/** 结束并提交事务 */
void end() {
HKU_CHECK(m_started, "No transaction has started!");
if (!m_committed) {
m_driver->commit();
m_committed = true;
m_started = false;
}
}

/** 回滚事务 */
void rollback() {
m_driver->rollback();
}

private:
TransAction() = delete;
TransAction(const AutoTransAction&) = delete;
Expand All @@ -122,6 +119,7 @@ class TransAction {
private:
DBConnectPtr m_driver;
bool m_committed;
bool m_started;
};

} // namespace hku
Expand Down

0 comments on commit 8381d8c

Please sign in to comment.