{"id":503,"date":"2026-05-06T10:24:45","date_gmt":"2026-05-06T01:24:45","guid":{"rendered":"https:\/\/www.zeronana.net\/?p=503"},"modified":"2026-05-06T15:40:15","modified_gmt":"2026-05-06T06:40:15","slug":"%e3%80%90%e5%ae%9f%e5%8b%99%e5%90%91%e3%81%91%e3%80%91go%e3%83%88%e3%83%a9%e3%83%b3%e3%82%b6%e3%82%af%e3%82%b7%e3%83%a7%e3%83%b3%e5%87%a6%e7%90%86%e3%81%ae%e6%96%b9%e6%b3%95%ef%bd%9c%e5%ae%89%e5%85%a8","status":"publish","type":"post","link":"https:\/\/www.zeronana.net\/?p=503","title":{"rendered":"\u3010\u5b9f\u52d9\u5411\u3051\u3011Go\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u51e6\u7406\u306e\u65b9\u6cd5\uff5c\u5b89\u5168\u306aDB\u958b\u767a\u3092\u5fb9\u5e95\u89e3\u8aac"},"content":{"rendered":"\n<p>Go\u8a00\u8a9e\u3067DB\u958b\u767a\u3092\u884c\u3046\u969b\u3001\u91cd\u8981\u306b\u306a\u308b\u306e\u304c\u300c\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u51e6\u7406\u300d\u3067\u3059\u3002<br>\u8907\u6570\u306eSQL\u51e6\u7406\u3092\u5b89\u5168\u306b\u5b9f\u884c\u3059\u308b\u305f\u3081\u306b\u3001\u5b9f\u52d9\u3067\u306f\u5fc5\u9808\u306e\u77e5\u8b58\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u672c\u8a18\u4e8b\u3067\u306f\u3001Go\u3067\u306e\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u51e6\u7406\u306b\u3064\u3044\u3066\u300c\u57fa\u672c\u30fb\u5b9f\u88c5\u65b9\u6cd5\u30fb\u5b9f\u52d9\u3067\u306e\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9\u300d\u3092\u521d\u5fc3\u8005\u5411\u3051\u306b\u89e3\u8aac\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3068\u306f\uff1f<\/h2>\n\n\n\n<p>\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3068\u306f\ud83d\udc47<\/p>\n\n\n\n<p>\ud83d\udc49 <strong>\u8907\u6570\u306e\u51e6\u7406\u3092\u201c\u307e\u3068\u3081\u3066\u6210\u529f\u30fb\u5931\u6557\u201d\u3055\u305b\u308b\u4ed5\u7d44\u307f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u306a\u305c\u5fc5\u8981\u306a\u306e\u304b\uff1f<\/h2>\n\n\n\n<p>\u4f8b\u3048\u3070\ud83d\udc47<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u6b8b\u9ad8\u6e1b\u7b97<\/li>\n\n\n\n<li>\u6b8b\u9ad8\u52a0\u7b97<\/li>\n<\/ol>\n\n\n\n<p>\u7247\u65b9\u3060\u3051\u6210\u529f\u3059\u308b\u3068\u4e0d\u6574\u5408\u304c\u767a\u751f\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p>\ud83d\udc49 \u3053\u308c\u3092\u9632\u3050\u306e\u304c\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u57fa\u672c\u306e\u6d41\u308c<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Begin<br>\u2193<br>SQL\u5b9f\u884c<br>\u2193<br>Commit or Rollback<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">database\/sql\u3067\u306e\u5b9f\u88c5<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>tx, err := db.Begin()<br><br>if err != nil {<br>    return err<br>}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">SQL\u5b9f\u884c<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>_, err = tx.Exec(<br>    \"UPDATE users SET point = point - 100 WHERE id = ?\",<br>    1,<br>)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Commit<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>err = tx.Commit()<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49 \u6210\u529f\u6642\u306b\u78ba\u5b9a<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Rollback<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>tx.Rollback()<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49 \u30a8\u30e9\u30fc\u6642\u306b\u623b\u3059<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9f\u52d9\u5411\u3051\u30d1\u30bf\u30fc\u30f3<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>tx, err := db.Begin()<br><br>if err != nil {<br>    return err<br>}<br><br>defer tx.Rollback()<br><br>_, err = tx.Exec(...)<br><br>if err != nil {<br>    return err<br>}<br><br>return tx.Commit()<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49 defer Rollback\u304c\u9244\u677f<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">GORM\u3067\u306e\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3<\/h2>\n\n\n\n<p>GORM<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tx := db.Begin()<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">GORM Commit<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>tx.Commit()<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">GORM Rollback<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>tx.Rollback()<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9f\u52d9\u3067\u91cd\u8981\u306a\u30dd\u30a4\u30f3\u30c8<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u2714 ACID\u7279\u6027<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u9805\u76ee<\/th><th>\u610f\u5473<\/th><\/tr><\/thead><tbody><tr><td>Atomicity<\/td><td>\u539f\u5b50\u6027<\/td><\/tr><tr><td>Consistency<\/td><td>\u4e00\u8cab\u6027<\/td><\/tr><tr><td>Isolation<\/td><td>\u72ec\u7acb\u6027<\/td><\/tr><tr><td>Durability<\/td><td>\u6c38\u7d9a\u6027<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2714 Rollback\u5fd8\u308c\u9632\u6b62<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>defer tx.Rollback()<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2714 \u9577\u6642\u9593Transaction\u7981\u6b62<\/h3>\n\n\n\n<p>\ud83d\udc49 DB\u30ed\u30c3\u30af\u539f\u56e0\u306b\u306a\u308b<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9f\u52d9\u3067\u306e\u5fdc\u7528<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6c7a\u6e08\u51e6\u7406<\/li>\n\n\n\n<li>\u30dd\u30a4\u30f3\u30c8\u7ba1\u7406<\/li>\n\n\n\n<li>\u4f1a\u54e1\u767b\u9332<\/li>\n\n\n\n<li>\u5728\u5eab\u7ba1\u7406<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u3088\u304f\u3042\u308b\u5931\u6557<\/h2>\n\n\n\n<p>\u274c Commit\u5fd8\u308c<br>\u274c Rollback\u672a\u5b9f\u88c5<br>\u274c Transaction\u9577\u3059\u304e\u554f\u984c<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>defer Rollback<\/li>\n\n\n\n<li>\u5fc5\u8981\u6700\u5c0f\u9650\u306e\u51e6\u7406<\/li>\n\n\n\n<li>\u30a8\u30e9\u30fc\u5373\u7d42\u4e86<\/li>\n\n\n\n<li>DB\u30ed\u30c3\u30af\u610f\u8b58<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udca1 \u7d50\u8ad6<\/h2>\n\n\n\n<p>\ud83d\udc49<br><strong>\u300c\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u54c1\u8cea\uff1d\u30b7\u30b9\u30c6\u30e0\u54c1\u8cea\u300d<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Go\u8a00\u8a9e\u3067DB\u958b\u767a\u3092\u884c\u3046\u969b\u3001\u91cd\u8981\u306b\u306a\u308b\u306e\u304c\u300c\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u51e6\u7406\u300d\u3067\u3059\u3002\u8907\u6570\u306eSQL\u51e6\u7406\u3092\u5b89\u5168\u306b\u5b9f\u884c\u3059\u308b\u305f\u3081\u306b\u3001\u5b9f\u52d9\u3067\u306f\u5fc5\u9808\u306e\u77e5\u8b58\u306b\u306a\u308a\u307e\u3059 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":504,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_seo_schema_type":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[6,7],"class_list":["post-503","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go","tag-go-2","tag-go"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/www.zeronana.net\/wp-content\/uploads\/2026\/05\/go\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3.png","_links":{"self":[{"href":"https:\/\/www.zeronana.net\/index.php?rest_route=\/wp\/v2\/posts\/503","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zeronana.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zeronana.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zeronana.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zeronana.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=503"}],"version-history":[{"count":1,"href":"https:\/\/www.zeronana.net\/index.php?rest_route=\/wp\/v2\/posts\/503\/revisions"}],"predecessor-version":[{"id":505,"href":"https:\/\/www.zeronana.net\/index.php?rest_route=\/wp\/v2\/posts\/503\/revisions\/505"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.zeronana.net\/index.php?rest_route=\/wp\/v2\/media\/504"}],"wp:attachment":[{"href":"https:\/\/www.zeronana.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zeronana.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=503"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zeronana.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}