事务操作
本文将介绍事务的使用方法。
示例代码
[HttpGet]
[Route("throwEx.aspx")]
public async Task<int> throwEx()
{
    CustomerBLL bll = this.CreateBLL<CustomerBLL>();
    // 切换到主库
    using( DbContext dbContext = DbConnManager.CreateAppDb("mysqltest") ) {
        // 开启数据库事务
        dbContext.BeginTransaction();
        var args = new { id = 1, addr = "abd" };
        DataTable table = await dbContext.XmlCommand.Create("name111", args).ToDataTableAsync();
        // 提交事务
        dbContext.Commit();
    }
    
    return 100;
}
小结:
- 调用 BeginTransaction() 显式开启事务
 - 操作结束后,调用 Commit() 提交事务
 - 如果事务不提交,连接关闭时会回滚
 - 不需要写try...catch 实现回滚