{"id":3077,"date":"2018-09-17T15:34:00","date_gmt":"2018-09-17T07:34:00","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3077"},"modified":"2018-09-17T15:34:00","modified_gmt":"2018-09-17T07:34:00","slug":"%e6%95%99%e4%bd%a0%e4%bd%bf%e7%94%a8%e6%95%b0%e6%8d%ae%e5%ba%93%e7%9a%84%e6%82%b2%e8%a7%82%e9%94%81-for-update-%e5%88%9b%e5%bb%ba%e5%88%86%e5%b8%83%e5%bc%8f%e9%94%81","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3077","title":{"rendered":"\u6559\u4f60\u4f7f\u7528\u6570\u636e\u5e93\u7684\u60b2\u89c2\u9501 for update \u521b\u5efa\u5206\u5e03\u5f0f\u9501"},"content":{"rendered":"<p>\n\t\u9501\u4e0d\u7ba1\u662f\u5728\u5de5\u4f5c\u4e2d\u8fd8\u662f\u751f\u6d3b\u4e2d\u90fd\u5230\u5904\u5b58\u5728\u3002\u5728\u7f16\u7a0b\u7684\u4e16\u754c\u4e2d\uff0c\u4e5f\u662f\u666e\u904d\u7684\u5b58\u5728\uff0c\u7528\u4e0d\u597d\uff0c\u4f1a\u4ea7\u751f\u5f88\u591a\u9ebb\u70e6\u95ee\u9898\u3002\n<\/p>\n<p>\n\t\u524d\u4e24\u5929\u6709\u4e00\u4e2a\u7f51\u53cb\u5728\u7fa4\u91cc\u95ee\u6211 synchronized \u9501\u7684\u95ee\u9898\uff0c\u5f53\u65f6\u6ca1\u600e\u4e48\u7ec6\u8bf4\uff0c\u4ee5\u540e\u6709\u65f6\u95f4\u4e86\u6765\u5199\u7bc7\u6587\u7ae0\u3002\u672c\u6587\u4e3b\u8981\u662f\u4ecb\u7ecd\u501f\u7528\u6570\u636e\u5e93\u7684\u60b2\u89c2\u9501\u6765\u5b9e\u73b0\u4e00\u4e2a\u5206\u5e03\u5f0f\u7684\u9501\u3002\n<\/p>\n<p>\n\t\u5206\u5e03\u5f0f\u9501\u7684\u5b9e\u73b0\u6709\u5f88\u591a\u79cd\u65b9\u6cd5\uff0c\u540e\u9762\u6211\u8fd8\u4f1a\u5199\u51e0\u7bc7\u7528 Redis\u3001Zookeeper \u7b49\u6765\u5b9e\u73b0\u5206\u5e03\u5f0f\u9501\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"\u5206\u5e03\u5f0f\u9501\" src=\"https:\/\/github.com\/xmt1139057136\/xttblog\/blob\/master\/lock.jpeg?raw=true\" style=\"width: 650px;height: 300px\" \/>\n<\/p>\n<p>\n\t\u76ee\u524d\u51e0\u4e4e\u6240\u6709\u7684\u5927\u578b\u7f51\u7ad9\u548c\u5e94\u7528\u90fd\u91c7\u7528\u7684\u662f\u5206\u5e03\u5f0f\u67b6\u6784\u3002\u5728\u5206\u5e03\u5f0f\u73af\u5883\u4e2d CAP \u7406\u8bba\u81f3\u5173\u91cd\u8981\uff0c\u4e0d\u61c2 CAP \u7406\u8bba\u7684\u53ef\u4ee5\u67e5\u770b\u6211\u524d\u9762\u7684\u6587\u7ae0\u300a<a href=\"https:\/\/www.xttblog.com\/?p=2925\" name=\"\u8be6\u89e3 CAP \u5b9a\u7406 Consistency\uff08\u4e00\u81f4\u6027\uff09\u3001 Availability\uff08\u53ef\u7528\u6027\uff09\u3001Partition tolerance\uff08\u5206\u533a\u5bb9\u9519\u6027\uff09\" target=\"_blank\" title=\"\u8be6\u89e3 CAP \u5b9a\u7406 Consistency\uff08\u4e00\u81f4\u6027\uff09\u3001 Availability\uff08\u53ef\u7528\u6027\uff09\u3001Partition tolerance\uff08\u5206\u533a\u5bb9\u9519\u6027\uff09\">\u8be6\u89e3 CAP \u5b9a\u7406 Consistency\uff08\u4e00\u81f4\u6027\uff09\u3001 Availability\uff08\u53ef\u7528\u6027\uff09\u3001Partition tolerance\uff08\u5206\u533a\u5bb9\u9519\u6027\uff09<\/a>\u300b\u3002\n<\/p>\n<p>\n\t\u5206\u5e03\u5f0f\u9501\u987e\u540d\u601d\u4e49\u662f\u53d1\u751f\u5728\u5206\u5e03\u5f0f\u73af\u5883\u4e2d\u7684\u3002\u5bf9\u4e8e\u5355\u8fdb\u7a0b\u573a\u666f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8bed\u8a00\u548c\u7c7b\u5e93\u63d0\u4f9b\u7684\u9501\uff0c\u5bf9\u4e8e\u5206\u5e03\u5f0f\u9501\uff0c\u6211\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5206\u5e03\u5f0f\u9501\u3002\u4e5f\u5c31\u662f\u8bf4\u540c\u6837\u7684\u9501\u4f7f\u7528\u7684\u73af\u5883\u4e0d\u540c\uff0c\u5206\u5e03\u5f0f\u73af\u5883\u4e2d\u7528\u7684\u9501\u5c31\u53eb\u5206\u5e03\u5f0f\u9501\uff01\n<\/p>\n<p>\n\t\u6839\u636e\u4e0a\u9762\u7684\u7406\u89e3\uff0c\u5206\u5e03\u5f0f\u9501\u662f\u4e0d\u662f\u5e94\u8be5\u5177\u5907\u4e0b\u9762\u7684\u7279\u70b9\uff1a\n<\/p>\n<ol>\n<li>\n\t\t\u5206\u5e03\u5f0f\u9501\u5fc5\u987b\u4fdd\u8bc1\u5728\u5206\u5e03\u5f0f\u90e8\u7f72\u7684\u5e94\u7528\u96c6\u7fa4\u4e2d\uff0c\u540c\u4e00\u4e2a\u65b9\u6cd5\u5728\u540c\u4e00\u65f6\u95f4\u53ea\u80fd\u88ab\u4e00\u53f0\u673a\u5668\u4e0a\u7684\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884c\uff1b\n\t<\/li>\n<li>\n\t\t\u4e00\u4e2a\u7ebf\u7a0b\u83b7\u5f97\u4e86\u9501\uff0c\u5176\u4ed6\u7ebf\u7a0b\u5fc5\u987b\u7b49\u5f85\u6301\u6709\u9501\u7684\u7ebf\u7a0b\u91ca\u653e\u6389\u624d\u80fd\u518d\u83b7\u53d6\uff1b\n\t<\/li>\n<li>\n\t\t\u9501\u5fc5\u987b\u8981\u6709\u8d85\u65f6\u673a\u5236\uff08\u907f\u514d\u6b7b\u9501\uff09\n\t<\/li>\n<\/ol>\n<p>\n\t\u57fa\u4e8e\u4e0a\u9762\u7684\u7279\u70b9\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7\u6570\u636e\u5e93\u7684\u60b2\u89c2\u9501\u6765\u5b9e\u73b0\u4e00\u4e2a\u5206\u5e03\u5f0f\u9501\u3002\n<\/p>\n<p>\n\t\u4ee3\u7801\u975e\u5e38\u7684\u7b80\u5355\uff0c\u4f7f\u7528 for update \u5373\u53ef\u3002\u5177\u4f53\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\npublic class XttblogLock {\r\n\r\n    private DataSource dataSource;\r\n\r\n    private static final String cmd = &quot;select * from xttblog_lock where id = 1 for update&quot;;\r\n\r\n    public XttblogLock(DataSource ds) {\r\n        this.dataSource = ds;\r\n    }\r\n\r\n    public static interface CallBack{\r\n        public void doAction();\r\n    }\r\n    public void lock(CallBack callBack)  {\r\n        Connection conn = null;\r\n        PreparedStatement stmt = null;\r\n        ResultSet rs = null;\r\n\r\n        try {\r\n            \/\/try get lock\r\n            System.out.println(Thread.currentThread().getName() + &quot; begin try lock&quot;);\r\n            conn = dataSource.getConnection();\r\n            conn.setAutoCommit(false);\r\n            stmt = conn.prepareStatement(cmd);\r\n            rs = stmt.executeQuery();\r\n          \r\n            \/\/do business thing\r\n            callBack.doAction();\r\n            \r\n            \/\/release lock\r\n            conn.commit();\r\n            System.out.println(Thread.currentThread().getName() + &quot; release lock&quot;);\r\n\r\n        } catch (SQLException e) {\r\n            e.printStackTrace();\r\n\r\n        } finally {\r\n            \r\n            if (null != conn) {\r\n                try {\r\n                    conn.close();\r\n                } catch (SQLException e) {\r\n                    e.printStackTrace();\r\n                }\r\n            }\r\n\r\n        }\r\n    }\r\n}<\/pre>\n<p>\n\t\u4f7f\u7528\u6570\u636e\u5e93\u60b2\u89c2\u9501\u5b9e\u73b0\u5206\u5e03\u5f0f\u9501\u4e3b\u8981\u7528\u4e86\u6570\u636e\u5e93\u7684 for update \u547d\u4ee4\uff0c\u6267\u884c\u6539\u547d\u4ee4\u540e\uff0c\u5bf9\u5e94\u884c\u8bb0\u5f55\u4f1a\u88ab\u9501\u4f4f\uff0c\u5176\u5b83\u7ebf\u7a0b\u4f1a\u88ab\u963b\u585e\u4e3b\uff0c\u76f4\u5230\u83b7\u53d6\u5230\u8fd9\u884c\u8bb0\u5f55\u7684\u7ebf\u7a0b\u63d0\u4ea4\u4e86\u4e8b\u52a1\u3002\u8fd9\u91cc\u9700\u8981\u6ce8\u610f\u8981\u628a\u81ea\u52a8\u63d0\u4ea4\u8bbe\u7f6e\u4e3a false\u3002\n<\/p>\n<p>\n\t\u8be5\u9501\u7684\u8c03\u7528\u4e5f\u975e\u5e38\u7684\u7b80\u5355\uff0c\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\nfinal XttblogLock xttblogLock = new XttblogLock(dataSource);\r\nxttblogLock.lock(new CallBack() {\r\n                        \r\n    @Override\r\n    public void doAction() {\r\n        System.out.println(Thread.currentThread().getName() + &quot;beging do somthing&quot;);\r\n        try {\r\n            System.out.println(&quot;\u4e1a\u4f59\u8349\uff1awww.xttblog.com \u6b22\u8fce\u4f60\uff01&quot;);\r\n            Thread.sleep(2000);\r\n        } catch (InterruptedException e) {\r\n            e.printStackTrace();\r\n        }\r\n        System.out.println(Thread.currentThread().getName() + &quot;end do somthing&quot;);\r\n\r\n    }\r\n});<\/pre>\n<p>\n\t\u867d\u7136\u6570\u636e\u5e93\u7684 for update \u60b2\u89c2\u9501\u53ef\u4ee5\u7528\u6765\u505a\u5206\u5e03\u5f0f\u9501\uff0c\u4f46\u5b9e\u9645\u7684\u751f\u4ea7\u8fc7\u7a0b\u4e2d\u91c7\u7528\u8fd9\u79cd\u65b9\u6cd5\u7684\u975e\u5e38\u5c11\uff0c\u56e0\u4e3a\u5b83\u6027\u80fd\u4e0d\u662f\u5f88\u9ad8\u3002<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=3077\">\u6559\u4f60\u4f7f\u7528\u6570\u636e\u5e93\u7684\u60b2\u89c2\u9501 for update \u521b\u5efa\u5206\u5e03\u5f0f\u9501<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u9501\u4e0d\u7ba1\u662f\u5728\u5de5\u4f5c\u4e2d\u8fd8\u662f\u751f\u6d3b\u4e2d\u90fd\u5230\u5904\u5b58\u5728\u3002\u5728\u7f16\u7a0b\u7684\u4e16\u754c\u4e2d\uff0c\u4e5f\u662f\u666e\u904d\u7684\u5b58\u5728\uff0c\u7528\u4e0d\u597d\uff0c\u4f1a\u4ea7\u751f\u5f88\u591a\u9ebb\u70e6\u95ee\u9898\u3002 \u524d\u4e24\u5929\u6709\u4e00 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,12],"tags":[712,157,426,1694,1693],"class_list":["post-3077","post","type-post","status-publish","format-standard","hentry","category-java","category-sql","tag-reentrantlock","tag-sql","tag-synchronized","tag-1694","tag-1693"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3077","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3077"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3077\/revisions"}],"predecessor-version":[{"id":3078,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3077\/revisions\/3078"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3077"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3077"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3077"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}