{"id":10006556,"date":"2022-09-20T22:22:50","date_gmt":"2022-09-20T14:22:50","guid":{"rendered":"https:\/\/ityet.com\/?p=10006556"},"modified":"2022-09-20T22:22:50","modified_gmt":"2022-09-20T14:22:50","slug":"%e9%9d%a2%e8%af%95%e7%aa%81%e5%87%bb85%ef%bc%9a%e4%b8%ba%e4%bb%80%e4%b9%88%e4%ba%8b%e5%8a%a1transactional%e4%bc%9a%e5%a4%b1%e6%95%88%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006556","title":{"rendered":"\u9762\u8bd5\u7a81\u51fb85\uff1a\u4e3a\u4ec0\u4e48\u4e8b\u52a1@Transactional\u4f1a\u5931\u6548\uff1f"},"content":{"rendered":"<p>\u5bfc\u81f4 @Transactional \u5931\u6548\u7684\u5e38\u89c1\u573a\u666f\u6709\u4ee5\u4e0b 5 \u4e2a\uff1a<\/p>\n<ol>\n<li>\u975e public \u4fee\u9970\u7684\u65b9\u6cd5\uff1b<\/li>\n<li>timeout \u8d85\u65f6\u65f6\u95f4\u8bbe\u7f6e\u8fc7\u5c0f\uff1b<\/li>\n<li>\u4ee3\u7801\u4e2d\u4f7f\u7528 try\/catch \u5904\u7406\u5f02\u5e38\uff1b<\/li>\n<li>\u8c03\u7528\u7c7b\u5185\u90e8\u7684 @Transactional \u65b9\u6cd5\uff1b<\/li>\n<li>\u6570\u636e\u5e93\u4e0d\u652f\u6301\u4e8b\u52a1\u3002<\/li>\n<\/ol>\n<p>\u5f88\u591a\u4eba\u53ea\u77e5\u9053\u7b54\u6848\u4f46\u4e0d\u77e5\u9053\u539f\u56e0\uff0c\u8fd9\u5c31\u50cf\u53ea\u8c08\u604b\u7231\u4e0d\u7ed3\u5a5a\u4e00\u6837\uff0c\u662f\u4e0d\u80fd\u8ba9\u4eba\u63a5\u53d7\u7684\uff0c\u6240\u4ee5\u672c\u7bc7\u6211\u4eec\u5c31\u6765\u8ba8\u8bba\u4e00\u4e0b\uff0c\u5bfc\u81f4\u4e8b\u52a1\u5931\u6548\u7684\u80cc\u540e\u539f\u56e0\u5230\u5e95\u662f\u5565\uff1f<\/p>\n<p>\u5728\u4ee5\u4e0a 5 \u79cd\u573a\u666f\u4e2d\uff0c\u7b2c 2 \u79cd\uff08timeout \u8d85\u65f6\u65f6\u95f4\u8bbe\u7f6e\u8fc7\u5c0f\uff09\u548c\u7b2c 5 \u79cd\uff08\u6570\u636e\u5e93\u4e0d\u652f\u6301\u4e8b\u52a1\uff09\u5f88\u597d\u7406\u89e3\uff0c\u6211\u4eec\u8fd9\u91cc\u5c31\u4e0d\u8d58\u8ff0\u4e86\uff0c\u672c\u6587\u6211\u4eec\u91cd\u70b9\u6765\u8ba8\u8bba\u5176\u4ed6 3 \u79cd\u60c5\u51b5\u3002<\/p>\n<h2>1.\u975e public \u4fee\u9970\u7684\u65b9\u6cd5<\/h2>\n<p>\u975e public \u4fee\u9970\u7684\u65b9\u6cd5\u4e0a\uff0c\u5373\u4f7f\u52a0\u4e86 @Transactional \u4e8b\u52a1\u4f9d\u7136\u4e0d\u4f1a\u751f\u6548\uff0c\u539f\u56e0\u662f<strong>\u56e0\u4e3a @Transactional \u4f7f\u7528\u7684\u662f Spring AOP \u5b9e\u73b0\u7684\uff0c\u800c Spring AOP \u662f\u901a\u8fc7\u52a8\u6001\u4ee3\u7406\u5b9e\u73b0\u7684\uff0c\u800c @Transactional \u5728\u751f\u6210\u4ee3\u7406\u65f6\u4f1a\u5224\u65ad\uff0c\u5982\u679c\u65b9\u6cd5\u4e3a\u975e public \u4fee\u9970\u7684\u65b9\u6cd5\uff0c\u5219\u4e0d\u751f\u6210\u4ee3\u7406\u5bf9\u8c61\uff0c\u8fd9\u6837\u4e5f\u5c31\u6ca1\u529e\u6cd5\u81ea\u52a8\u6267\u884c\u4e8b\u52a1\u4e86<\/strong>\uff0c\u5b83\u7684\u90e8\u5206\u5b9e\u73b0\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">protected TransactionAttribute computeTransactionAttribute(Method method, Class&lt;?&gt; targetClass) {\n   \/\/ Don't allow no-public methods as required.\n   \/\/ \u975e public \u65b9\u6cd5\uff0c\u8bbe\u7f6e\u4e3a null\n   if (allowPublicMethodsOnly() &amp;&amp; !Modifier.isPublic(method.getModifiers())) {\n      return null;\n   }\n   \/\/ \u540e\u9762\u4ee3\u7801\u7701\u7565....\n }<\/code><\/pre>\n<h2>2.try\/catch \u5bfc\u81f4\u4e8b\u52a1\u5931\u6548<\/h2>\n<p>@Transactional \u6267\u884c\u6d41\u7a0b\u662f\uff1a <strong>@Transactional \u4f1a\u5728\u65b9\u6cd5\u6267\u884c\u524d\uff0c\u4f1a\u81ea\u52a8\u5f00\u542f\u4e8b\u52a1\uff1b\u5728\u65b9\u6cd5\u6210\u529f\u6267\u884c\u5b8c\uff0c\u4f1a\u81ea\u52a8\u63d0\u4ea4\u4e8b\u52a1\uff1b\u5982\u679c\u65b9\u6cd5\u5728\u6267\u884c\u671f\u95f4\uff0c\u51fa\u73b0\u4e86\u5f02\u5e38\uff0c\u90a3\u4e48\u5b83\u4f1a\u81ea\u52a8\u56de\u6eda\u4e8b\u52a1\u3002<\/strong><\/p>\n<p>\u7136\u800c<strong>\u5982\u679c\u5728\u65b9\u6cd5\u4e2d\u81ea\u884c\u6dfb\u52a0\u4e86 try\/catch \u4e4b\u540e\uff0c\u4e8b\u52a1\u5c31\u4e0d\u4f1a\u81ea\u52a8\u56de\u6eda\u4e86<\/strong>\uff0c\u8fd9\u662f\u600e\u4e48\u56de\u4e8b\u5462\uff1f<br \/>\u9020\u6210\u8fd9\u4e2a\u95ee\u9898\u7684\u4e3b\u8981\u539f\u56e0\u548c @Transactional \u6ce8\u89e3\u7684\u5b9e\u73b0\u6709\u5173\uff0c\u5b83\u7684\u90e8\u5206\u5b9e\u73b0\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">protected Object invokeWithinTransaction(Method method, Class&lt;?&gt; targetClass, final InvocationCallback invocation)\n      throws Throwable {\n   \/\/ If the transaction attribute is null, the method is non-transactional.\n   final TransactionAttribute txAttr = getTransactionAttributeSource().getTransactionAttribute(method, targetClass);\n   final PlatformTransactionManager tm = determineTransactionManager(txAttr);\n   final String joinpointIdentification = methodIdentification(method, targetClass);\n\n   if (txAttr == null || !(tm instanceof CallbackPreferringPlatformTransactionManager)) {\n       \/\/ Standard transaction demarcation with getTransaction and commit\/rollback calls.\n       \/\/ \u81ea\u52a8\u5f00\u542f\u4e8b\u52a1\n      TransactionInfo txInfo = createTransactionIfNecessary(tm, txAttr, joinpointIdentification);\n      Object retVal = null;\n      try {\n         \/\/ This is an around advice: Invoke the next interceptor in the chain.\n         \/\/ This will normally result in a target object being invoked.\n         \/\/ \u53cd\u5c04\u8c03\u7528\u4e1a\u52a1\u65b9\u6cd5\n         retVal = invocation.proceedWithInvocation();\n      }\n      catch (Throwable ex) {\n          \/\/ target invocation exception\n          \/\/ \u5f02\u5e38\u65f6\uff0c\u5728 catch \u903b\u8f91\u4e2d\uff0c\u81ea\u52a8\u56de\u6eda\u4e8b\u52a1\n         completeTransactionAfterThrowing(txInfo, ex);\n         throw ex;\n      }\n      finally {\n         cleanupTransactionInfo(txInfo);\n      }\n       \/\/ \u81ea\u52a8\u63d0\u4ea4\u4e8b\u52a1\n      commitTransactionAfterReturning(txInfo);\n      return retVal;\n   }\n\n   else {\n     \/\/ .....\n   }\n}<\/code><\/pre>\n<p>\u4ece\u4e0a\u8ff0\u5b9e\u73b0\u6e90\u7801\u6211\u4eec\u53ef\u4ee5\u770b\u51fa\uff1a<strong>\u5f53\u6267\u884c\u7684\u65b9\u6cd5\u4e2d\u51fa\u73b0\u4e86\u5f02\u5e38\uff0c@Transactional \u624d\u80fd\u611f\u77e5\u5230\uff0c\u7136\u540e\u518d\u6267\u884c\u4e8b\u52a1\u56de\u6eda\uff0c\u800c\u5f53\u5f00\u53d1\u8005\u81ea\u884c\u6dfb\u52a0\u4e86 try\/catch \u4e4b\u540e\uff0c@Transactional \u5c31\u611f\u77e5\u4e0d\u5230\u5f02\u5e38\u4e86\uff0c\u4ece\u800c\u5c31\u4e0d\u4f1a\u89e6\u53d1\u4e8b\u52a1\u7684\u81ea\u52a8\u56de\u6eda\u4e86\uff0c\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u5f53 @Transactional \u9047\u5230 try\/catch \u4e4b\u540e\u5c31\u4e0d\u4f1a\u81ea\u52a8\u56de\u6eda\uff08\u4e8b\u52a1\uff09\u7684\u539f\u56e0\u3002<\/strong><\/p>\n<h2>3.\u8c03\u7528\u7c7b\u5185\u7528\u7684 @Transactional \u65b9\u6cd5<\/h2>\n<p>\u5f53\u8c03\u7528\u7c7b\u5185\u90e8\u7684 @Transactional \u4fee\u9970\u7684\u65b9\u6cd5\u65f6\uff0c\u4e8b\u52a1\u4e5f\u4e0d\u4f1a\u751f\u6548\uff0c\u5982\u4e0b\u4ee3\u7801\u6240\u793a\uff1a<\/p>\n<pre><code class=\"java\">@RequestMapping(&quot;\/save&quot;)\npublic int saveMappping(UserInfo userInfo) {\n    return save(userInfo);\n}\n@Transactional\npublic int save(UserInfo userInfo) {\n    \/\/ \u975e\u7a7a\u6548\u9a8c\n    if (userInfo == null ||\n        !StringUtils.hasLength(userInfo.getUsername()) ||\n        !StringUtils.hasLength(userInfo.getPassword()))\n        return 0;\n    int result = userService.save(userInfo);\n    int num = 10 \/ 0; \/\/ \u6b64\u5904\u8bbe\u7f6e\u4e00\u4e2a\u5f02\u5e38\n    return result;\n}<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u5728\u6dfb\u52a0\u7528\u6237\u4e4b\u540e\u5373\u4f7f\u9047\u5230\u4e86\u5f02\u5e38\uff0c\u7a0b\u5e8f\u4e5f\u6ca1\u6709\u6267\u884c\u56de\u6eda\uff0c\u8fd9\u662f<strong>\u56e0\u4e3a @Transactional \u662f\u57fa\u4e8e Spring AOP \u5b9e\u73b0\u7684\uff0c\u800c Spring AOP \u53c8\u662f\u57fa\u4e8e\u52a8\u6001\u4ee3\u7406\u5b9e\u73b0\u7684\uff0c\u800c\u5f53\u8c03\u7528\u7c7b\u5185\u90e8\u7684\u65b9\u6cd5\u65f6\uff0c\u4e0d\u662f\u901a\u8fc7\u4ee3\u7406\u5bf9\u8c61\u5b8c\u6210\u7684\uff0c\u800c\u662f\u901a\u8fc7 this \u5bf9\u8c61\u5b9e\u73b0\u7684\uff0c\u8fd9\u6837\u5c31\u7ed5\u8fc7\u4e86\u4ee3\u7406\u5bf9\u8c61\uff0c\u4ece\u800c\u4e8b\u52a1\u5c31\u5931\u6548\u4e86\u3002<\/strong><\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u975e public \u4fee\u9970\u7684\u65b9\u6cd5\u5728 @Transactional \u5b9e\u73b0\u65f6\u505a\u4e86\u5224\u65ad\uff0c\u5982\u679c\u662f\u975e public \u5219\u4e0d\u4f1a\u751f\u6210\u4ee3\u7406\u5bf9\u8c61\uff0c\u6240\u4ee5\u4e8b\u52a1\u5c31\u5931\u6548\u4e86\uff1b\u800c\u8c03\u7528\u7c7b\u5185\u90e8\u7684 @Transactional \u4fee\u9970\u7684\u65b9\u6cd5\u65f6\uff0c\u4e5f\u662f\u56e0\u4e3a\u6ca1\u6709\u6210\u529f\u8c03\u7528\u4ee3\u7406\u5bf9\u8c61\uff0c\u662f\u901a\u8fc7 this \u6765\u8c03\u7528\u65b9\u6cd5\u7684\uff0c\u6240\u4ee5\u4e8b\u52a1\u4e5f\u5931\u6548\u4e86\uff1b@Transactional \u5728\u9047\u5230\u5f00\u53d1\u8005\u81ea\u5b9a\u4e49\u7684 try\/catch \u4e5f\u4f1a\u5931\u6548\uff0c\u8fd9\u662f\u56e0\u4e3a  @Transactional \u53ea\u6709\u611f\u77e5\u5230\u4e86\u5f02\u5e38\u624d\u4f1a\u81ea\u52a8\u56de\u6eda\uff08\u4e8b\u52a1\uff09\uff0c\u4f46\u5982\u679c\u7528\u6237\u81ea\u5b9a\u4e49\u4e86 try\/catch\uff0c\u90a3\u4e48 @Transactional \u5c31\u611f\u77e5\u4e0d\u5230\u5f02\u5e38\uff0c\u6240\u4ee5\u4e5f\u5c31\u4e0d\u4f1a\u81ea\u52a8\u56de\u6eda\u4e8b\u52a1\u4e86\u3002<\/p>\n<h4>\u53c2\u8003 &#038; \u9e23\u8c22<\/h4>\n<p>blog.csdn.net\/qq_20597727\/article\/details\/84900994<\/p>\n<blockquote>\n<p>\u662f\u975e\u5ba1\u4e4b\u4e8e\u5df1\uff0c\u6bc1\u8a89\u542c\u4e4b\u4e8e\u4eba\uff0c\u5f97\u5931\u5b89\u4e4b\u4e8e\u6570\u3002<\/p>\n<p>\u516c\u4f17\u53f7\uff1aJava\u9762\u8bd5\u771f\u9898\u89e3\u6790<\/p>\n<p>\u9762\u8bd5\u5408\u96c6\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=KZDsPETBWlxbJkPZCQmHSA%3D%3D.7Slk6XFY6GvKsXemiQnX91UtvphecZO1BnkBIhSeHBlc7a4CJgWCn6fTzjP%2FMQ41\" rel=\"nofollow\">https:\/\/gitee.com\/mydb\/interview<\/a><\/p>\n<\/blockquote>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042517439\">https:\/\/segmentfault.com\/a\/1190000042517439<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5bfc\u81f4 @Transactional \u5931\u6548\u7684\u5e38\u89c1\u573a\u666f\u6709\u4ee5\u4e0b 5 \u4e2a\uff1a \u975e public \u4fee\u9970\u7684\u65b9\u6cd5\uff1b timeo [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006556","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006556","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006556"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006556\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006556"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006556"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}