【发布时间】:2014-04-09 06:52:06
【问题描述】:
禁用事务有什么意义? http://ellislab.com/codeigniter/user-guide/database/transactions.html
$this->db->trans_off()
我看不到用法。是为了测试目的吗?
“当事务被禁用时,你的查询将被自动提交,就像在没有事务的情况下运行查询一样。”
我有一个名为user 的表,其中存在一个列nameOfUser。 nameOfUser2 - 列不存在。
测试1 此代码将尝试使用 普通事务 进行 2 次插入:
$this->db->trans_start();
$this->db->insert('user', array('nameOfUser' => 'testCVVCOOL'));
$this->db->insert('user', array('nameOfUser2' => 'test2'));
$this->db->trans_complete();
但是因为nameOfUser2-column 在第二次插入中不存在列而被回滚(没有插入任何内容)。
测试2 此代码将尝试在 transaction disabled
的情况下进行 2 次插入$this->db->trans_off();
$this->db->trans_start();
$this->db->insert('user', array('nameOfUser' => 'testCVVCOOL'));
$this->db->insert('user', array('nameOfUser2' => 'test2'));
$this->db->trans_complete();
上面会将 testCVVCOOL 字符串插入到user-table 中,即使第二次插入有错误 ($this->db->insert('user', array('nameOfUser2' => 'test2'));)
您什么时候需要以这种方式禁用事务?
【问题讨论】:
-
我不确定,但我相信这更像是交易状态的休息,因为在您执行 trans_start() 之后,如果您有任何疑问,您可能需要将交易模式重置为关闭交易结束后执行。
标签: codeigniter activerecord transactions