{"id":4280,"date":"2023-07-18T23:02:07","date_gmt":"2023-07-18T15:02:07","guid":{"rendered":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=4280"},"modified":"2023-07-18T23:02:07","modified_gmt":"2023-07-18T15:02:07","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%9a2","status":"publish","type":"post","link":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=4280","title":{"rendered":"\u591a\u7ebf\u7a0b\u4e8b\u52a1\u5904\u7406-\u624b\u52a8\u56de\u6eda2"},"content":{"rendered":"\n<p>1\u3001\u63d0\u53d6\u5b50\u4efb\u52a1\u6267\u884c\u7684\u4ee3\u7801<\/p>\n\n\n\n<p>@Service<br>@Slf4j<br>public class TransactionService {<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Autowired\nprivate PlatformTransactionManager txManager;\n\npublic &lt;T&gt; void processInTransaction(Callable&lt;T&gt; callable) {\n    DefaultTransactionDefinition def = new DefaultTransactionDefinition();\n    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);\n    TransactionStatus status = txManager.getTransaction(def);\n    try {\n        callable.call();\n        txManager.commit(status);\n    } catch (Exception e) {\n        log.error(\"An error occurred: {}\", e.getMessage());\n        txManager.rollback(status);\n        throw new RuntimeException(e);\n    }\n}<\/code><\/pre>\n\n\n\n<p>}<br><\/p>\n\n\n\n<p>2\u3001\u4e3b\u65b9\u6cd5\u4e2d\u4f7f\u7528\uff0c\u5e76\u589e\u52a0\u6bcf\u4e00\u4e2a\u5b50\u4efb\u52a1\u7684\u5f02\u5e38\u5904\u7406<\/p>\n\n\n\n<p>@Service<br>public class TestTxService {<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Autowired\nprivate TransactionService transactionService;\n\n\/\/ List of services\nprivate List&lt;Callable&lt;Void>> services;\n\npublic TestTxService(@Autowired Service1 service1, \n                     @Autowired Service2 service2, \n                     @Autowired Service3 service3) {\n    this.services = Arrays.asList(\n            () -> {\n                try {\n                    service1.doSomething();\n                    return null;\n                } catch (SpecificException1 e) {\n                    \/\/ handle exception specific to service 1\n                    log.error(\"Error in Service1: {}\", e.getMessage());\n                    \/\/ Additional handling code...\n                    throw e;\n                }\n            },\n            () -> {\n                try {\n                    service2.doSomething();\n                    return null;\n                } catch (SpecificException2 e) {\n                    \/\/ handle exception specific to service 2\n                    log.error(\"Error in Service2: {}\", e.getMessage());\n                    \/\/ Additional handling code...\n                    throw e;\n                }\n            },\n            () -> {\n                try {\n                    service3.doSomething();\n                    return null;\n                } catch (SpecificException3 e) {\n                    \/\/ handle exception specific to service 3\n                    log.error(\"Error in Service3: {}\", e.getMessage());\n                    \/\/ Additional handling code...\n                    throw e;\n                }\n            }\n    );\n}\n\npublic void testTx() {\n        int totalThreadCount = services.size(); \/\/ Use size of services list\n        ExecutorService executorService = Executors.newFixedThreadPool(totalThreadCount);\n        CompletionService&lt;Void> completionService = new ExecutorCompletionService&lt;>(executorService);\n\n        for (Callable&lt;Void> service : services) {\n            completionService.submit(() -> {\n                transactionService.processInTransaction(service);\n                return null;\n            });\n        }\n\n        executorService.shutdown();\n\n        for (int i = 0; i &lt; totalThreadCount; i++) {\n            try {\n                completionService.take().get();\n            } catch (InterruptedException e) {\n                Thread.currentThread().interrupt();\n                throw new RuntimeException(e);\n            } catch (ExecutionException e) {\n                throw new RuntimeException(\"An exception was thrown in a thread\", e.getCause());\n            }\n        }\n    }\n<\/code><\/pre>\n\n\n\n<p>}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u63d0\u53d6\u5b50\u4efb\u52a1\u6267\u884c\u7684\u4ee3\u7801 @Service@Slf4jpublic class TransactionServ [&hellip;]<\/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\/4280"}],"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=4280"}],"version-history":[{"count":2,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4280\/revisions"}],"predecessor-version":[{"id":4282,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4280\/revisions\/4282"}],"wp:attachment":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}