{"id":6708,"date":"2024-11-14T09:18:44","date_gmt":"2024-11-14T01:18:44","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6708\/"},"modified":"2024-11-14T09:18:44","modified_gmt":"2024-11-14T01:18:44","slug":"spring%e4%b8%adtransactional%e4%ba%8b%e5%8a%a1%e4%bb%80%e4%b9%88%e6%97%b6%e5%80%99%e6%8f%90%e4%ba%a4%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6708\/","title":{"rendered":"Spring\u4e2d@Transactional\u4e8b\u52a1\u4ec0\u4e48\u65f6\u5019\u63d0\u4ea4\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/172975451043312.jpg\" class=\"aligncenter\" title=\"Spring\u4e2d@Transactional\u4e8b\u52a1\u4ec0\u4e48\u65f6\u5019\u63d0\u4ea4\uff1f\u63d2\u56fe\" alt=\"Spring\u4e2d@Transactional\u4e8b\u52a1\u4ec0\u4e48\u65f6\u5019\u63d0\u4ea4\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>spring\u4e2d@transactional\u4e8b\u52a1\u63d0\u4ea4\u65f6\u95f4\u70b9<\/strong><\/p>\n<p>\u5728spring\u4e2d\uff0c\u4f7f\u7528@transactional\u6ce8\u89e3\u53ef\u4ee5\u58f0\u660e\u65b9\u6cd5\u4e3a\u4e8b\u52a1\u65b9\u6cd5\uff0c\u5bf9\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\u3002\u7136\u800c\uff0c\u4e8b\u52a1\u7684\u63d0\u4ea4\u70b9\u662f\u4f55\u65f6\u5462\uff1f\u662f\u65b9\u6cd5\u6267\u884c\u7ed3\u675f\u540e\u8fd8\u662f\u6267\u884c\u7ed3\u675f\u524d\uff1f<\/p>\n<p>\u8fd9\u4e2a\u95ee\u9898\u56f0\u6270\u4e86\u8bb8\u591a\u5f00\u53d1\u8005\uff0c\u5c24\u5176\u662f\u5728\u5e76\u53d1\u60c5\u51b5\u4e0b\uff0c\u51c6\u786e\u638c\u63e1\u4e8b\u52a1\u63d0\u4ea4\u70b9\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<p><strong>\u65b9\u6cd5\u6267\u884c\u7ed3\u675f\u540e<\/strong><\/p>\n<p>@transactional\u6ce8\u89e3\u88abspring\u7684aop\u6846\u67b6\u5b9e\u73b0\u3002spring\u4f7f\u7528\u521b\u5efa\u4e8b\u52a1\u65b9\u6cd5\u7684\u4ee3\u7406\uff0c\u5e76\u5728\u65b9\u6cd5\u6267\u884c\u524d\u540e\u7684\u7279\u5b9a\u5207\u70b9\u6267\u884c\u64cd\u4f5c\uff0c\u5305\u62ec\u4e8b\u52a1\u7684\u5f00\u542f\u548c\u63d0\u4ea4\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u4e8b\u52a1\u5b9e\u9645\u4e0a\u662f\u5728\u65b9\u6cd5\u6267\u884c\u7ed3\u675f\u540e\u63d0\u4ea4\u7684\u3002<\/p>\n<p><strong>\u5e76\u53d1\u60c5\u51b5\u4e0b\u9632\u6b62\u8d85\u5356<\/strong><\/p>\n<p>\u5728\u5e76\u53d1\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u65b9\u6cd5\u6267\u884c\u540e\u7acb\u5373\u89e3\u9501\uff0c\u5c31\u6709\u53ef\u80fd\u51fa\u73b0\u8d85\u5356\u95ee\u9898\u3002\u5047\u8bbe\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8c03\u7528\u65b9\u6cd5\uff0c\u7ebf\u7a0b1\u6267\u884c\u5b8c\u6210\u540e\u4f46\u4e8b\u52a1\u5c1a\u672a\u63d0\u4ea4\u3002\u8fd9\u65f6\uff0c\u7ebf\u7a0b2\u83b7\u53d6\u9501\u5e76\u6267\u884c\u65b9\u6cd5\uff0c\u5b83\u5728\u53ef\u91cd\u590d\u8bfb\u9694\u79bb\u7ea7\u522b\u4e0b\u65e0\u6cd5\u770b\u5230\u7ebf\u7a0b1\u5bf9\u5e93\u5b58\u7684\u64cd\u4f5c\uff0c\u4ece\u800c\u5bfc\u81f4\u8d85\u5356\u3002<\/p>\n<p>\u8981\u9632\u6b62\u8d85\u5356\uff0c\u9700\u8981\u786e\u4fdd\u4e8b\u52a1\u5728\u89e3\u9501\u4e4b\u524d\u63d0\u4ea4\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u5bf9\u65b9\u6cd5\u8fdb\u884c\u9002\u5f53\u7684\u91cd\u6784\uff0c\u5c06\u9501\u64cd\u4f5c\u653e\u5728\u4e8b\u52a1\u63d0\u4ea4\u4e4b\u540e\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>public Result func(long seckillId, long userId) {\n    \/\/ \u6267\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u2014\u2014\u67e5\u8be2\u5546\u54c1\u5e93\u5b58\u6570\u91cf\n    \/\/ \u5982\u679c \u5e93\u5b58\u6570\u91cf \u6ee1\u8db3\u8981\u6c42\n    lock.lock();\n    try {\n        \/\/ \u6267\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u2014\u2014\u51cf\u5c11\u5e93\u5b58\u6570\u91cf\u2014\u2014\u6a21\u62df\u5356\u51fa\u8d27\u7269\u64cd\u4f5c\n        return Result.SUCCESS;\n    } finally {\n        lock.unlock();\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4e0b\uff0c\u4e8b\u52a1\u5728\u89e3\u9501\u4e4b\u524d\u63d0\u4ea4\uff0c\u786e\u4fdd\u4e86\u5e76\u53d1\u60c5\u51b5\u4e0b\u4e0d\u4f1a\u51fa\u73b0\u8d85\u5356\u95ee\u9898\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fSpring\u4e2d@Transactional\u4e8b\u52a1\u4ec0\u4e48\u65f6\u5019\u63d0\u4ea4\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>spring\u4e2d@transactional\u4e8b\u52a1\u63d0\u4ea4\u65f6\u95f4\u70b9 \u5728spring\u4e2d\uff0c\u4f7f\u7528@transactional\u6ce8\u89e3\u53ef\u4ee5\u58f0\u660e\u65b9\u6cd5\u4e3a\u4e8b\u52a1\u65b9\u6cd5\uff0c\u5bf9\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\u3002\u7136\u800c\uff0c\u4e8b\u52a1\u7684\u63d0\u4ea4\u70b9\u662f\u4f55\u65f6\u5462\uff1f\u662f\u65b9\u6cd5\u6267\u884c\u7ed3\u675f\u540e\u8fd8\u662f\u6267\u884c\u7ed3\u675f\u524d\uff1f \u8fd9\u4e2a\u95ee\u9898\u56f0\u6270\u4e86\u8bb8\u591a\u5f00\u53d1\u8005\uff0c\u5c24\u5176\u662f\u5728\u5e76\u53d1\u60c5\u51b5\u4e0b\uff0c\u51c6\u786e\u638c\u63e1\u4e8b\u52a1\u63d0\u4ea4\u70b9\u81f3\u5173\u91cd\u8981\u3002 \u65b9\u6cd5\u6267\u884c\u7ed3\u675f\u540e @transactional\u6ce8\u89e3\u88abspring\u7684aop\u6846\u67b6\u5b9e\u73b0\u3002spring\u4f7f\u7528\u521b\u5efa\u4e8b\u52a1\u65b9\u6cd5\u7684\u4ee3\u7406\uff0c\u5e76\u5728\u65b9\u6cd5\u6267\u884c\u524d\u540e\u7684\u7279\u5b9a\u5207\u70b9\u6267\u884c\u64cd\u4f5c\uff0c\u5305\u62ec\u4e8b\u52a1\u7684\u5f00\u542f\u548c\u63d0\u4ea4\u3002 \u56e0\u6b64\uff0c\u4e8b\u52a1\u5b9e\u9645\u4e0a\u662f\u5728\u65b9\u6cd5\u6267\u884c\u7ed3\u675f\u540e\u63d0\u4ea4\u7684\u3002 \u5e76\u53d1\u60c5\u51b5\u4e0b\u9632\u6b62\u8d85\u5356 \u5728\u5e76\u53d1\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u65b9\u6cd5\u6267\u884c\u540e\u7acb\u5373\u89e3\u9501\uff0c\u5c31\u6709\u53ef\u80fd\u51fa\u73b0\u8d85\u5356\u95ee\u9898\u3002\u5047\u8bbe\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8c03\u7528\u65b9\u6cd5\uff0c\u7ebf\u7a0b1\u6267\u884c\u5b8c\u6210\u540e\u4f46\u4e8b\u52a1\u5c1a\u672a\u63d0\u4ea4\u3002\u8fd9\u65f6\uff0c\u7ebf\u7a0b2\u83b7\u53d6\u9501\u5e76\u6267\u884c\u65b9\u6cd5\uff0c\u5b83\u5728\u53ef\u91cd\u590d\u8bfb\u9694\u79bb\u7ea7\u522b\u4e0b\u65e0\u6cd5\u770b\u5230\u7ebf\u7a0b1\u5bf9\u5e93\u5b58\u7684\u64cd\u4f5c\uff0c\u4ece\u800c\u5bfc\u81f4\u8d85\u5356\u3002 \u8981\u9632\u6b62\u8d85\u5356\uff0c\u9700\u8981\u786e\u4fdd\u4e8b\u52a1\u5728\u89e3\u9501\u4e4b\u524d\u63d0\u4ea4\u3002 \u89e3\u51b3\u65b9\u6848 \u5bf9\u65b9\u6cd5\u8fdb\u884c\u9002\u5f53\u7684\u91cd\u6784\uff0c\u5c06\u9501\u64cd\u4f5c\u653e\u5728\u4e8b\u52a1\u63d0\u4ea4\u4e4b\u540e\u3002\u4f8b\u5982\uff1a public Result func(long seckillId, long userId) { \/\/ \u6267\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u2014\u2014\u67e5\u8be2\u5546\u54c1\u5e93\u5b58\u6570\u91cf \/\/ \u5982\u679c \u5e93\u5b58\u6570\u91cf \u6ee1\u8db3\u8981\u6c42 lock.lock(); try { \/\/ \u6267\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u2014\u2014\u51cf\u5c11\u5e93\u5b58\u6570\u91cf\u2014\u2014\u6a21\u62df\u5356\u51fa\u8d27\u7269\u64cd\u4f5c return Result.SUCCESS; } finally { lock.unlock(); } } \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u79cd\u65b9\u5f0f\u4e0b\uff0c\u4e8b\u52a1\u5728\u89e3\u9501\u4e4b\u524d\u63d0\u4ea4\uff0c\u786e\u4fdd\u4e86\u5e76\u53d1\u60c5\u51b5\u4e0b\u4e0d\u4f1a\u51fa\u73b0\u8d85\u5356\u95ee\u9898\u3002 \u4ee5\u4e0a\u5c31\u662fSpring\u4e2d@Transactional\u4e8b\u52a1\u4ec0\u4e48\u65f6\u5019\u63d0\u4ea4\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-6708","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6708","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=6708"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6708\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}