{"id":5629,"date":"2015-02-06T16:34:46","date_gmt":"2015-02-06T07:34:46","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=5629"},"modified":"2018-11-14T16:33:49","modified_gmt":"2018-11-14T07:33:49","slug":"rails-nested-transactions","status":"publish","type":"post","link":"https:\/\/www.techscore.com\/blog\/2015\/02\/06\/rails-nested-transactions\/","title":{"rendered":"\u30cd\u30b9\u30c8\u3057\u305f\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u5185\u3067 ActiveRecord::Rollback \u3092 raise \u3057\u3066\u3082\u63e1\u308a\u3064\u3076\u3055\u308c\u308b\u3060\u3051\u3060"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u4e09\u82eb\u3067\u3059\u3002<\/p>\n<p>ActiveRecord\u306e\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u306b\u3064\u3044\u3066\u3061\u3087\u3063\u3068\u3057\u305ftips\u3067\u3059\u3002<\/p>\n<p>\u4ee5\u4e0b\u306eActiveRecord\u3092\u4f7f\u3063\u305fruby\u30b3\u30fc\u30c9\u304c\u3069\u306e\u3088\u3046\u306a\u30af\u30a8\u30ea\u3092\u767a\u884c\u3059\u308b\u3060\u308d\u3046\uff1f<\/p>\n<pre class=\"\">Entity.transaction do\r\n  Entity.transaction do \r\n    Entity.create!(name: \"hoge\")\r\n  end\r\n  Entity.transaction do\r\n    Entity.create!(name: \"moge\")\r\n    raise ActiveRecord::Rollback\r\n  end\r\nend<\/pre>\n<p>\u304a\u305d\u3089\u304f\u591a\u6570\u306e\u4eba\u304c\u671f\u5f85\u3059\u308b\u52d5\u4f5c\u306f\u3053\u3046\uff08\u4ee5\u4e0b\u3001\u30af\u30a8\u30ea\u4f8b\u306fPostgreSQL\u306e\u3082\u306e\u3067\u3059\uff09<\/p>\n<pre class=\"\">BEGIN\r\nINSERT INTO \"entities\" (\"name\") values ('hoge') RETURNING \"id\"\r\nINSERT INTO \"entities\" (\"name\") values ('moge') RETURNING \"id\"\r\nROLLBACK -- \u30cd\u30b9\u30c8\u3057\u305f\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u304c\u843d\u3061\u305f\u3089\u3001\u305d\u308a\u3083\u5168\u30ed\u30fc\u30eb\u30d0\u30c3\u30af\u3067\u3057\u3087\uff5e<\/pre>\n<p>\u5b9f\u969b\u306e\u52d5\u4f5c\u306f\u3053\u3046\uff01<\/p>\n<pre class=\"\">BEGIN\r\nINSERT INTO \"entities\" (\"name\") values ('hoge') RETURNING \"id\"\r\nINSERT INTO \"entities\" (\"name\") values ('moge') RETURNING \"id\"\r\nCOMMIT -- \u2190\u30ed\u30fc\u30eb\u30d0\u30c3\u30af\u3084\u3068\u8a00\u3063\u3068\u308b\u7bc4\u56f2\u307e\u3067\u30b3\u30df\u30c3\u30c8\u3055\u308c\u3068\u308b\u3084\u306a\u3044\u304b\uff01\uff01\uff01\uff01<\/pre>\n<p>\u4e16\u9593\u306f\u53b3\u3057\u3044\uff01\uff01<br \/>\n\u5168\u30ed\u30fc\u30eb\u30d0\u30c3\u30af\u3057\u305f\u3051\u308c\u3070\u66f8\u304f\u3079\u304d\u30b3\u30fc\u30c9\u306f\u3053\u3046\uff01\uff01\uff01\uff01<\/p>\n<pre class=\"\">Entity.transaction do\r\n  Entity.transaction do\r\n    Entity.create!(name: \"hoge\")\r\n  end\r\n  Entity.transaction do\r\n    Entity.create!(name: \"moge\")\r\n    raise \"Rollback!\" # \u30cd\u30b9\u30c8\u3057\u305f\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u30d6\u30ed\u30c3\u30af\u3092\u7a81\u304d\u7834\u308b\u3088\u3046\u306a\u4f8b\u5916\u3092\u6295\u3052\u3088\u3046\r\n  end\r\nend<\/pre>\n<p>\u30cd\u30b9\u30c8\u3057\u305f\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u30d6\u30ed\u30c3\u30af\u3060\u3051\u30ed\u30fc\u30eb\u30d0\u30c3\u30af\u3057\u305f\u3051\u308c\u3070\u66f8\u304f\u3079\u304d\u30b3\u30fc\u30c9\u306f\u3053\u3046\uff01<\/p>\n<pre class=\"\">Entity.transaction do\r\n  Entity.transaction do \r\n    Entity.create!(name: \"hoge\")\r\n  end\r\n  Entity.transaction(requires_new: true) do # requires_new \u3092\u6307\u5b9a\u3057\u3088\u3046\r\n    Entity.create!(name: \"moge\")\r\n    raise ActiveRecord::Rollback\r\n  end\r\nend<\/pre>\n<p>\u305d\u306e\u969b\u306e\u30af\u30a8\u30ea\u306f\u3053\u3046\uff01<\/p>\n<pre class=\"\">BEGIN\r\nINSERT INTO \"entities\" (\"name\") values ('hoge') RETURNING \"id\"\r\nSAVEPOINT active_record_1 -- SAVEPOINT \u3092\u767a\u884c\u3057\u3066\u304f\u308c\u308b\r\nINSERT INTO \"entities\" (\"name\") values ('moge') RETURNING \"id\"\r\nROLLBACK TO SAVEPOINT active_record_1 -- SAVEPOINT \u307e\u3067\u30ed\u30fc\u30eb\u30d0\u30c3\u30af\u3057\u3066\u304f\u308c\u308b\r\nCOMMIT -- \u3081\u3067\u305f\u3057\u3081\u3067\u305f\u3057\u3002<\/pre>\n<p>\u3067\u306f\u3001\u3053\u306e\u308f\u304b\u308a\u306b\u304f\u3044\u6319\u52d5\u3001\u30c9\u30ad\u30e5\u30e1\u30f3\u30c6\u30fc\u30b7\u30e7\u30f3\u3055\u308c\u3066\u3044\u308b\u306e\u304b\u3069\u3046\u304b\u3002<\/p>\n<p>\u3082\u3061\u308d\u3093\u30c9\u30ad\u30e5\u30e1\u30f3\u30c6\u30fc\u30b7\u30e7\u30f3\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u8aad\u307e\u305a\u306b\u601d\u3044\u8fbc\u307f\u3067\u5b9f\u88c5\u3057\u3066\u306f\u3044\u3051\u306a\u3044\u3002<br \/>\n\u7279\u306b\u3001\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u306e\u3088\u3046\u306a\u30bb\u30f3\u30b7\u30c6\u30a3\u30d6\u306a\u554f\u984c\u306f\u30fb\u30fb\u30fb\u3002\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u304c\u5fc5\u8981\u3068\u3055\u308c\u308b\u51e6\u7406\u306f\u30bb\u30f3\u30b7\u30c6\u30a3\u30d6\u30fb\u30fb\u30fb\u3002<\/p>\n<p>\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306f\u82f1\u8a9e\u306a\u306e\u3067\u8a33\u3057\u305f\u3082\u306e\u3092\u4ee5\u4e0b\u306b\u8a18\u8f09\u3057\u307e\u3059\u3002<br \/>\n\u4e0a\u3067\u66f8\u3044\u305f\u3053\u3068\u304c\u307b\u307c\u305d\u306e\u307e\u307e\u66f8\u304b\u308c\u3066\u3044\u307e\u3059\u3002\u6700\u521d\u304b\u3089\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u8aad\u3093\u3067\u3044\u308c\u3070\u30fb\u30fb\u30fb\u3002<\/p>\n<p>\u8a72\u5f53\u7b87\u6240\u306f\u4ee5\u4e0b\u3067\u3059\u3002<br \/>\n<a href=\"https:\/\/github.com\/rails\/rails\/blob\/v4.2.0\/activerecord\/lib\/active_record\/transactions.rb#L142\">https:\/\/github.com\/rails\/rails\/blob\/v4.2.0\/activerecord\/lib\/active_record\/transactions.rb#L142<br \/>\n<\/a>\u8a33\u304c\u9593\u9055\u3063\u3066\u308b\u53ef\u80fd\u6027\u3082\u3042\u308b\u306e\u3067\u3001\u5fae\u5999\u306b\u611f\u3058\u305f\u5834\u5408\u306f\u662f\u975e\u539f\u6587\u3092\u8aad\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<h1>Nested transactions<br \/>\n\uff08\u30cd\u30b9\u30c8\u3057\u305f\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\uff09<\/h1>\n<p>+transaction+ calls can be nested. By default, this makes all database<br \/>\nstatements in the nested transaction block become part of the parent<br \/>\ntransaction. For example, the following behavior may be surprising:<\/p>\n<p>\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u30e1\u30bd\u30c3\u30c9\u306f\u30cd\u30b9\u30c8\u3057\u3066\u547c\u3076\u3053\u3068\u304c\u3067\u304d\u308b\u3002<br \/>\n\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u306f\u3001\u30cd\u30b9\u30c8\u3057\u305f\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u5185\u3067\u306e\u30b9\u30c6\u30fc\u30c8\u30e1\u30f3\u30c8\u306f\u3059\u3079\u3066\u89aa\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u306e\u4e00\u90e8\u3068\u3057\u3066\u5b9f\u884c\u3055\u308c\u308b\u3002<br \/>\n\u4ee5\u4e0b\u306e\u4f8b\u306f\u3001\u3082\u3057\u304b\u3057\u305f\u3089\u9a5a\u304f\u304b\u3082\u3057\u308c\u306a\u3044\u3002\uff08\u8a33\u6ce8\uff1a\u305d\u3089\u9a5a\u304f\u3084\u308d\uff01\uff09<\/p>\n<pre class=\"\">User.transaction do\r\n  User.create(username: 'Kotori')\r\n  User.transaction do\r\n    User.create(username: 'Nemu')\r\n    raise ActiveRecord::Rollback\r\n  end\r\nend<\/pre>\n<p>creates both \"Kotori\" and \"Nemu\". Reason is the &lt;tt&gt;ActiveRecord::Rollback&lt;\/tt&gt;<br \/>\nexception in the nested block does not issue a ROLLBACK. Since these exceptions<br \/>\nare captured in transaction blocks, the parent block does not see it and the<br \/>\nreal transaction is committed.<\/p>\n<p>\u4e0a\u8a18\u306e\u4f8b\u3067\u306fKotori\u3068Nemu\u304c\u4f5c\u6210\u3055\u308c\u308b\u3002\u7406\u7531\u306f ActiveRecord::Rollback \u306f\u30cd\u30b9\u30c8\u3057\u305f\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u30d6\u30ed\u30c3\u30af\u3092\u30ed\u30fc\u30eb\u30d0\u30c3\u30af\u3057\u306a\u3044\u304b\u3089\u3060\u3002<br \/>\nActiveRecord::Rollback\u306e\u3088\u3046\u306a\u4f8b\u5916\u306f\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u30d6\u30ed\u30c3\u30af\u306e\u4e2d\u3067\u6355\u6349\u3055\u308c\u3001\u89aa\u306e\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u30d6\u30ed\u30c3\u30af\u304b\u3089\u306f\u691c\u77e5\u3067\u304d\u306a\u3044\u3057\u3001\u5b9f\u969b\u306e\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u306f\u30b3\u30df\u30c3\u30c8\u3055\u308c\u308b\u3002<\/p>\n<p>In order to get a ROLLBACK for the nested transaction you may ask for a real<br \/>\nsub-transaction by passing &lt;tt&gt;requires_new: true&lt;\/tt&gt;. If anything goes wrong,<br \/>\nthe database rolls back to the beginning of the sub-transaction without rolling<br \/>\nback the parent transaction. If we add it to the previous example:<\/p>\n<p>\u30cd\u30b9\u30c8\u3057\u305f\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3092\u30ed\u30fc\u30eb\u30d0\u30c3\u30af\u3059\u308b\u305f\u3081\u306b\u306f\u3001requires_new: true \u3092\u6307\u5b9a\u3057\u3066\u5b9f\u969b\u306e\u30b5\u30d6\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3068\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3002<br \/>\n\u3082\u3057\u3001\u4f55\u304b\u826f\u304f\u306a\u3044\u3053\u3068\u304c\u8d77\u304d\u305f\u3068\u304d\u306b\u30b5\u30d6\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3060\u3051\u3092\u30ed\u30fc\u30eb\u30d0\u30c3\u30af\u3057\u3001\u89aa\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3092\u30ed\u30fc\u30eb\u30d0\u30c3\u30af\u3057\u305f\u304f\u306a\u3044\u3068\u304d\u306f\u524d\u306e\u4f8b\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u4fee\u6b63\u3059\u308c\u3070\u3088\u3044\u3002<\/p>\n<pre class=\"\">User.transaction do\r\n  User.create(username: 'Kotori')\r\n  User.transaction(requires_new: true) do\r\n    User.create(username: 'Nemu')\r\n    raise ActiveRecord::Rollback\r\n  end\r\nend<\/pre>\n<p>only \"Kotori\" is created. This works on MySQL and PostgreSQL. SQLite3 version &gt;= '3.6.8' also supports it.<\/p>\n<p>Kotori\u3060\u3051\u304c\u4f5c\u6210\u3055\u308c\u308b\u3002\u3053\u308c\u306fMySQL\u3068PostgreSQL\u3067\u52d5\u4f5c\u3059\u308b\u3002SQLite3\u3067\u306f 3.6.8 \u4ee5\u4e0a\u3067\u30b5\u30dd\u30fc\u30c8\u3055\u308c\u308b\u3002<\/p>\n<p>Most databases don't support true nested transactions. At the time of<br \/>\nwriting, the only database that we're aware of that supports true nested<br \/>\ntransactions, is MS-SQL. Because of this, Active Record emulates nested<br \/>\ntransactions by using savepoints on MySQL and PostgreSQL. See<br \/>\nhttp:\/\/dev.mysql.com\/doc\/refman\/5.6\/en\/savepoint.html<br \/>\nfor more information about savepoints.<\/p>\n<p>\u307b\u3068\u3093\u3069\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306f\u30cd\u30b9\u30c8\u3057\u305f\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u306a\u3044\u3002<br \/>\n\u3053\u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u66f8\u3044\u3066\u3044\u308b\u6642\u70b9\u3067\u306f\u30cd\u30b9\u30c8\u3057\u305f\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u3066\u3044\u308b\u306e\u306fMS-SQL\u3060\u3051\u3060\u3002<br \/>\n\u306a\u306e\u3067\u3001ActiveRecord\u306f\u30cd\u30b9\u30c8\u3057\u305fMySQL\u3068PostgreSQL\u306b\u304a\u3044\u3066\u30cd\u30b9\u30c8\u3057\u305f\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3092SAVEPOINT\u3092\u5229\u7528\u3057\u3066\u30a8\u30df\u30e5\u30ec\u30fc\u30c8\u3057\u3066\u3044\u308b\u3002<br \/>\nSAVEPOINT\u306b\u3064\u3044\u3066\u306e\u60c5\u5831\u306fMySQL\u306e\u4ee5\u4e0b\u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306b\u8a73\u3057\u3044\u3002<br \/>\nhttp:\/\/dev.mysql.com\/doc\/refman\/5.6\/en\/savepoint.html<br \/>\n\uff08\u8a33\u6ce8\uff1a\u65e5\u672c\u8a9e\u3067PostgreSQL\u306e\u5834\u5408\u306f\u3053\u3061\u3089\u53c2\u7167 <a href=\"http:\/\/www.postgresql.jp\/document\/9.3\/html\/sql-savepoint.html\">http:\/\/www.postgresql.jp\/document\/9.3\/html\/sql-savepoint.html<\/a> \uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u4e09\u82eb\u3067\u3059\u3002<\/p>\n<p>ActiveRecord\u306e\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u306b\u3064\u3044\u3066\u3061\u3087\u3063\u3068\u3057\u305ftips\u3067\u3059\u3002<br \/><a href=\"https:\/\/www.techscore.com\/blog\/2015\/02\/06\/rails-nested-transactions\/\">\u7d9a\u304d\u3092\u8aad\u3080...<\/a><\/p>\n","protected":false},"author":15,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18],"tags":[96,22],"_links":{"self":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/5629"}],"collection":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/comments?post=5629"}],"version-history":[{"count":9,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/5629\/revisions"}],"predecessor-version":[{"id":5647,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/5629\/revisions\/5647"}],"wp:attachment":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/media?parent=5629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/categories?post=5629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/tags?post=5629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}