我有以下问题。
我有被回收的数据库连接(放回池中)。
例如:
{
session sql(conn_str); // take connection from pool
sql.exec("insert into ...")
} // at the end of the scope return connection to pool
然而,在某些情况下,回收可能是错误的——例如断开连接或其他一些重大错误。
所以我想自动防止连接被回收。我想使用以下技术实现std::uncaught_exception
- 因此 exec() 函数将检测异常并防止回收:
session::exec(...)
{
guard g(this)
real_exec(...);
}
守卫在哪里:
class guard {
public:
guard(session *self) : self_(self) {}
~guard() {
if(std::uncaught_exception()) {
self->mark_as_connection_that_should_not_go_to_pool();
}
}
}
现在,我知道http://www.gotw.ca/gotw/047.htm不建议
std::uncaught_exception
在其他情况下使用我也没有看到我的代码有任何问题,提供了讨论的示例。
这段代码是否有任何可能的问题。
笔记:
- 我希望此更改是非侵入性的,以便 SQL 后端能够抛出而不检查每个案例是否至关重要。
- 我不希望用户对此采取任何行动,因此对他来说是透明的。