简单如下
class Ma < ActiveRecord::Base
end
class Mb < ActiveRecord::Base
end
class Mc < ActiveRecord::Base
def xxx
transaction do
Ma.create attr1: "xxx"
raise "error"
Mb.create attr1: "xxx"
end
end
end
大致如上,没贴细节代码,在 transaction 中间 raise 了,但是数据库 Ma 还是多了一条记录,为何
#4 楼 @yanhao 从 log 看,在 rollback 前居然有个 commit 操作,不知怎么回事
D, [2013-04-21T15:32:34.024600 #15630] DEBUG -- : (0.3ms) BEGIN
D, [2013-04-21T15:32:34.096260 #15630] DEBUG -- : (0.6ms) BEGIN
D, [2013-04-21T15:32:34.104295 #15630] DEBUG -- : SQL (2.8ms) INSERT INTO "records" ("note_id") VALUES ($1) RETURNING "id" [["note_id", 338]]
D, [2013-04-21T15:32:34.125080 #15630] DEBUG -- : (20.0ms) COMMIT
D, [2013-04-21T15:32:34.127409 #15630] DEBUG -- : (0.7ms) ROLLBACK
log 如上 代码如下
module MnModel
class Note < ActiveRecord::Base
establish_connection DB_CONFIGURATIONS
has_many :fields
has_many :records
has_many :items, through: :fields
def create_record_with_date(attributes={})
record_with_data = {}
transaction do
record = Record.create note_id: id
record_with_data.merge! record.serializable_hash
raise "error"
fields.each do |f|
item = Item.create record_id: record.id, field_id: f.id, content: attributes[f.name]
record_with_data.merge!(f.name => item.content)
end
end
return record_with_data
rescue => e
return {}
end
end
end
看 log 是 create 本身就有了一个 begin/commit,再外面的 transaction 的 rollback 已经没效果了?
ps:ruby+activerecord;不在 rails 中
看一下这节“Nested transactions”,http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html