Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RAII的数据库连接池 #87

Open
Iseulf opened this issue Aug 16, 2023 · 0 comments
Open

RAII的数据库连接池 #87

Iseulf opened this issue Aug 16, 2023 · 0 comments

Comments

@Iseulf
Copy link

Iseulf commented Aug 16, 2023

class SqlConnRAII {
public:
    SqlConnRAII(MYSQL** sql, SqlConnPool *connpool) {
        assert(connpool);
        *sql = connpool->GetConn();
        sql_ = *sql;
        connpool_ = connpool;
    }
    
    ~SqlConnRAII() {
        if(sql_) { connpool_->FreeConn(sql_); }
    }
    
private:
    MYSQL *sql_;
    SqlConnPool* connpool_;
};

这里采用的是初始化外部变量sql的做法,会FreeConn(sql)执行了两次。为什么不把SqlConnRAII写成unique_ptr那种智能指针的形式呢。

如源码这里,定义了一个匿名变量,那析构函数就会立刻执行,那sql就会立刻被释放,而后源码中又执行了一次FreeConn。
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant