Rails的3嵌套事务。除了在孩子块 [英] Rails 3. Nested transactions. Exception in a child block
问题描述
为什么不ActiveRecord的回滚变化嵌套事务异常上升在孩子块之后?
Why doesn't ActiveRecord rollback changes in nested transactions after exception was risen in a child block?
下面举例说明:
1
>> Client.transaction do
?> Client.create(:name => 'Pavel')
>> Client.transaction do
?> Client.create(:name => 'Elena')
>> raise ActiveRecord::Rollback
>> end
>> end
=> nil
>> Client.all.map(&:name)
=> ["Pavel", "Elena"] # instead of []
2
>> Client.transaction do
?> Client.create(:name => 'Pavel')
>> Client.transaction(:requires_new => true) do
?> Client.create(:name => 'Elena')
>> raise ActiveRecord::Rollback
>> end
>> end
=> nil
>> Client.all.map(&:name)
=> ["Pavel", "Elena"] # instead of ["Pavel"]
感谢。
的Debian GNU / Linux的5.0.6;
Debian GNU/Linux 5.0.6;
红宝石1.9.2;
的Ruby on Rails 3.0.1;
Ruby on Rails 3.0.1;
SQLite的3.7.3。
SQLite 3.7.3.
推荐答案
我有同样的问题,我可以准确复制您的结果。如果我提出的ActiveRecord ::回滚在外部块,那么整个事务回滚,但除此之外,没有被回滚。
I'm having the same problem, and I can duplicate your result exactly. If I raise ActiveRecord::Rollback in the outer block, then the whole transaction rolls back, but otherwise, nothing gets rolled back.
显然,ActiveRecord的当前版本不知道该怎么办嵌套事务与sqlite3的,尽管ActiveRecord的应该是实现使用保存点的嵌套事务,并自3.6.8 SQLite的一直支持保存点。
Apparently, the current version of ActiveRecord does not know how to do nested transactions with SQLite3, even though ActiveRecord is supposed to implement nested transactions using savepoints, and SQLite has supported savepoints since 3.6.8.
进一步证明,这仅仅是不支持的ActiveRecord的呢,试试这个......
As further evidence that this is simply not supported by ActiveRecord yet, try this...
> List.connection.supports_savepoints?
=> false
Ubuntu的11.04 - 在纳蒂独角鲸;
Ubuntu 11.04 - the Natty Narwhal;
红宝石1.8.7(2010-04-19补丁级别253)的i486-linux的],MBARI 0x8770,Ruby企业版2010.02;
ruby 1.8.7 (2010-04-19 patchlevel 253) [i486-linux], MBARI 0x8770, Ruby Enterprise Edition 2010.02;
的Ruby on Rails 3.0.3;
Ruby on Rails 3.0.3;
sqlite3的宝石1.3.3
sqlite3 gem 1.3.3
SQLite的3.7.2;
SQLite 3.7.2;
这篇关于Rails的3嵌套事务。除了在孩子块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!