{"id":4277,"date":"2023-07-18T22:49:58","date_gmt":"2023-07-18T14:49:58","guid":{"rendered":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=4277"},"modified":"2023-07-18T22:49:58","modified_gmt":"2023-07-18T14:49:58","slug":"%e5%a4%9a%e7%ba%bf%e7%a8%8b%e4%ba%8b%e5%8a%a1%e5%a4%84%e7%90%86-%e6%89%8b%e5%8a%a8%e5%9b%9e%e6%bb%9a","status":"publish","type":"post","link":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=4277","title":{"rendered":"\u591a\u7ebf\u7a0b\u4e8b\u52a1\u5904\u7406-\u624b\u52a8\u56de\u6eda"},"content":{"rendered":"\n<p>@Service<br>@Slf4j<br>public class TestTxService {<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Autowired\nprivate PlatformTransactionManager txManager;\n\n@Autowired\nprivate OtherService otherService; \/\/ \u5176\u4ed6\u670d\u52a1\n\npublic void testTx() {\n    \/\/ \u5b9a\u4e49\u603b\u7684\u7ebf\u7a0b\u6570\n    int totalThreadCount = 3;\n\n    \/\/ \u5b9a\u4e49\u4e8b\u52a1\u7684\u57fa\u672c\u5c5e\u6027\n    DefaultTransactionDefinition def = new DefaultTransactionDefinition();\n    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);\n\n    \/\/ \u521b\u5efa\u4e00\u4e2a\u56fa\u5b9a\u5927\u5c0f\u7684\u7ebf\u7a0b\u6c60\n    ExecutorService executorService = Executors.newFixedThreadPool(totalThreadCount);\n\n    \/\/ \u521b\u5efa\u4e00\u4e2aCompletionService\u6765\u7ba1\u7406\u5f02\u6b65\u6267\u884c\u7684\u7ed3\u679c\n    CompletionService&lt;Void&gt; completionService = new ExecutorCompletionService&lt;&gt;(executorService);\n\n    \/\/ \u63d0\u4ea4\u4efb\u52a1\u5230\u7ebf\u7a0b\u6c60\n    for (int i = 0; i &lt; totalThreadCount; i++) {\n        completionService.submit(() -&gt; {\n            \/\/ \u5f00\u59cb\u4e00\u4e2a\u65b0\u7684\u4e8b\u52a1\n            TransactionStatus status = txManager.getTransaction(def);\n            try {\n                \/\/ \u8c03\u7528\u5916\u90e8\u670d\u52a1\u5904\u7406\n                otherService.doSomething();\n\n                \/\/ \u968f\u673a\u629b\u51fa\u5f02\u5e38\u6765\u6a21\u62df\u8fd0\u884c\u65f6\u9519\u8bef\n                if (ThreadLocalRandom.current().nextBoolean()) {\n                    throw new RuntimeException(\"Manual exception\");\n                }\n\n                \/\/ \u63d0\u4ea4\u4e8b\u52a1\n                txManager.commit(status);\n            } catch (Exception e) {\n                \/\/ \u5982\u679c\u51fa\u73b0\u5f02\u5e38\uff0c\u8bb0\u5f55\u5f02\u5e38\u4fe1\u606f\u5e76\u56de\u6eda\u4e8b\u52a1\n                log.error(\"An error occurred: {}\", e.getMessage());\n                txManager.rollback(status);\n                throw e;\n            }\n\n            \/\/ \u6ca1\u6709\u8fd4\u56de\u7ed3\u679c\n            return null;\n        });\n    }\n\n    \/\/ \u5173\u95ed\u7ebf\u7a0b\u6c60\uff0c\u4e0d\u518d\u63a5\u53d7\u65b0\u7684\u4efb\u52a1\n    executorService.shutdown();\n\n    \/\/ \u7b49\u5f85\u6240\u6709\u4efb\u52a1\u5b8c\u6210\n    for (int i = 0; i &lt; totalThreadCount; i++) {\n        try {\n            \/\/ \u5982\u679c\u4efb\u52a1\u6267\u884c\u629b\u51fa\u4e86\u5f02\u5e38\uff0c\u8fd9\u91cc\u4f1a\u5f97\u77e5\n            completionService.take().get();\n        } catch (InterruptedException e) {\n            \/\/ \u5982\u679c\u7b49\u5f85\u88ab\u4e2d\u65ad\uff0c\u6062\u590d\u4e2d\u65ad\u72b6\u6001\u5e76\u629b\u51fa\u5f02\u5e38\n            Thread.currentThread().interrupt();\n            throw new RuntimeException(e);\n        } catch (ExecutionException e) {\n            \/\/ \u5982\u679c\u4efb\u52a1\u6267\u884c\u629b\u51fa\u4e86\u5f02\u5e38\uff0c\u5305\u88c5\u5e76\u629b\u51fa\u4e00\u4e2a\u65b0\u7684\u5f02\u5e38\n            throw new RuntimeException(\"An exception was thrown in a thread\", e.getCause());\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>}<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>@Service@Slf4jpublic class TestTxService { }<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[154,219],"_links":{"self":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4277"}],"collection":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4277"}],"version-history":[{"count":2,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4277\/revisions"}],"predecessor-version":[{"id":4279,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4277\/revisions\/4279"}],"wp:attachment":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}