1.odb事务
odb事务有如下接口
namespace odb
{
class transaction
{
public:
typedef odb::database database_type;
typedef odb::connection connection_type;
explicit transaction (transaction_impl*, bool make_current = true);
transaction ();
void
reset (transaction_impl*, bool make_current = true); //重置当前线程的事务, 使这个事务可以重新使用
void commit (); //提交
void rollback (); //回滚
database_type& database (); //获得当前事务所工作的数据库
connection_type& connection (); //获得当前事务所工作的连接
bool finilized () const; //检查当前事务是否结束
public:
static bool
has_current (); //事务是否已经结束
static transaction& current (); //获得当前线程的事务
static void current (transaction& t); //将t变为当前线程的活跃事务
static bool
reset_current (); //清楚当前线程的活跃事务
public:
...
};
}
事务脱离作用域的时候会被自动回滚。如果我们试图提交或者回滚一个已经结束的事务会抛出异常,odb::transaction_already_finalized。
我们要尽量在事务内部声明变量,以避免事务回滚时应用程序和数据库中对象不一致的问题。
2.odb连接
odb :: connection类表示到数据库的连接。 通常情况下,我们不会直接使用连接,而是让ODB运行时根据需要获取和释放连接。
3.odb查询过程迭代器失效的问题
odb查询在odb的官方文档中介绍很详细。我们简单介绍如下。
typedef odb::query<person> query; typedef odb::result<person> result; query q(query::first == "John"); result r = db->query(q);
当我们查询到结果之后,常见的操作如下。此时切记,在进行操作时切勿提交事务或者回滚事务否则会发生迭代器失效。
for (auto i : r)
{
//某种操作;
}