{"id":15439,"date":"2024-05-30T09:16:04","date_gmt":"2024-05-30T01:16:04","guid":{"rendered":"https:\/\/92it.top\/?p=15439"},"modified":"2024-05-30T09:16:04","modified_gmt":"2024-05-30T01:16:04","slug":"springboot%e4%b8%ad%e4%bd%bf%e7%94%a8transactional%e6%b3%a8%e8%a7%a37%e5%a4%a7%e5%a4%b1%e6%95%88%e5%9c%ba%e6%99%af%e5%8f%8a%e8%a7%a3%e5%86%b3%e6%96%b9%e6%a1%88","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=15439","title":{"rendered":"Springboot\u4e2d\u4f7f\u7528@Transactional\u6ce8\u89e37\u5927\u5931\u6548\u573a\u666f\u53ca\u89e3\u51b3\u65b9\u6848"},"content":{"rendered":"\n<p>\u4eca\u5929\u54b1\u4eec\u6765\u804a\u804aSpringBoot\u4e2d\u7684\u4e00\u4e2a\u8d85\u7ea7\u91cd\u8981\u7684\u65b0\u4eba\u5bb9\u6613\u8e29\u7684\u5751\uff1a<\/p>\n\n\n\n<p>SpringBoot\u4e2d\u4e8b\u52a1\u5931\u6548\u7684\u90a3\u4e9b\u5751\uff01<\/p>\n\n\n\n<p>\u5728Java\u5f00\u53d1\u7684\u4e16\u754c\u91cc\uff0cSpringBoot\u56e0\u4e3a\u5b83\u7684\u4fbf\u6377\u548c\u5f3a\u5927\u5df2\u7ecf\u6210\u4e3a\u4e86\u4f17\u591a\u5f00\u53d1\u8005\u7684\u9996\u9009\u3002<\/p>\n\n\n\n<p>\u4f46\u662f\uff0c\u5373\u4f7f\u5728\u8fd9\u4e48\u68d2\u7684\u6846\u67b6\u4e0b\uff0c\u4e8b\u52a1\u5904\u7406\u4e5f\u4e0d\u662f\u603b\u80fd\u5982\u4f60\u6240\u613f\u3002<\/p>\n\n\n\n<p>\u6709\u65f6\u5019\uff0c\u4f60\u53ef\u80fd\u89c9\u5f97\u81ea\u5df1\u5df2\u7ecf\u914d\u7f6e\u5f97\u5f88\u5230\u4f4d\u4e86\uff0c\u4f46\u4e8b\u52a1\u5c31\u662f\u4e0d\u751f\u6548\uff0c\u8fd9\u8ba9\u4eba\u633a\u5934\u75bc\u7684\uff0c\u5bf9\u5427\uff1f<\/p>\n\n\n\n<p><strong>\u5f15\u8a00<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u4e8b\u52a1\u662f\u786e\u4fdd\u6570\u636e\u4e00\u81f4\u6027\u548c\u5b8c\u6574\u6027\u7684\u5173\u952e\u673a\u5236\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u590d\u6742\u7684\u4e1a\u52a1\u903b\u8f91\u65f6\u3002<\/p>\n\n\n\n<p>\u7136\u800c\uff0c\u5728SpringBoot\u4e2d\uff0c\u7531\u4e8e\u914d\u7f6e\u6216\u4f7f\u7528\u4e0d\u5f53\uff0c\u4e8b\u52a1\u6709\u65f6\u5019\u4f1a\u5931\u6548\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u4e00\u4e9b\u96be\u4ee5\u9884\u6599\u7684\u540e\u679c\u3002<\/p>\n\n\n\n<p>\u4e3a\u4e86\u907f\u514d\u6389\u5165\u8fd9\u4e9b\u5751\uff0c\u54b1\u4eec\u5f97\u5148\u4e86\u89e3\u5b83\u4eec\u5728\u54ea\u513f\u3002<\/p>\n\n\n\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u5e26\u4f60\u6df1\u5165\u4e86\u89e3SpringBoot\u4e8b\u52a1\u5931\u6548\u76847\u5927\u573a\u666f\uff0c\u5e76\u5206\u4eab10\u4e2a\u5b9e\u7528\u7684\u6280\u5de7\uff0c\u5e2e\u4f60\u907f\u514d\u8fd9\u4e9b\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u6bcf\u4e2a\u6280\u5de7\u6211\u90fd\u4f1a\u7ed9\u51fa\u8be6\u7ec6\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u8ba9\u4f60\u4e0d\u4ec5\u77e5\u5176\u7136\uff0c\u66f4\u77e5\u5176\u6240\u4ee5\u7136\u3002<\/p>\n\n\n\n<p><strong>\u573a\u666f\u4e00\uff1a\u79c1\u6709\u65b9\u6cd5\u4e0a\u4f7f\u7528@Transactional<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5728\u79c1\u6709\u65b9\u6cd5\u4e0a\u4f7f\u7528@Transactional\u6ce8\u89e3\u901a\u5e38\u4e0d\u4f1a\u751f\u6548\uff0c\u56e0\u4e3aSpring\u7684\u4e8b\u52a1\u7ba1\u7406\u662f\u57fa\u4e8e\u4ee3\u7406\u7684\uff0c\u800c\u79c1\u6709\u65b9\u6cd5\u65e0\u6cd5\u88ab\u4ee3\u7406\u62e6\u622a\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Service\npublic class TransactionService {\n\n    \/\/ \u8fd9\u91cc\u7684\u4e8b\u52a1\u662f\u4e0d\u4f1a\u751f\u6548\u7684\n    @Transactional\n    private void privateTransactionalMethod() {\n        \/\/ \u6570\u636e\u5e93\u64cd\u4f5c\n    }\n\n    public void publicMethod() {\n        \/\/ \u8c03\u7528\u79c1\u6709\u65b9\u6cd5\n        privateTransactionalMethod();\n    }\n}<\/pre>\n\n\n\n<p><strong>\u573a\u666f\u4e8c\uff1a\u540c\u4e00\u4e2a\u7c7b\u4e2d\u65b9\u6cd5\u4e92\u8c03<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5f53\u4e00\u4e2a@Transactional\u6807\u6ce8\u7684\u65b9\u6cd5\u5728\u540c\u4e00\u4e2a\u7c7b\u4e2d\u88ab\u53e6\u4e00\u4e2a\u65b9\u6cd5\u76f4\u63a5\u8c03\u7528\u65f6\uff0c\u4e8b\u52a1\u4e5f\u4e0d\u4f1a\u751f\u6548\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e5f\u662f\u56e0\u4e3aSpring\u7684\u4ee3\u7406\u65b9\u5f0f\u5bfc\u81f4\u7684\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Service\npublic class TransactionService {\n\n    @Transactional\n    public void methodA() {\n        \/\/ \u6570\u636e\u5e93\u64cd\u4f5c\n    }\n\n    public void methodB() {\n        \/\/ \u76f4\u63a5\u8c03\u7528methodA\uff0c\u4e8b\u52a1\u4e0d\u4f1a\u751f\u6548\n        methodA();\n    }\n}<\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"739\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-203-1024x739.png\" alt=\"\" class=\"wp-image-15440\" style=\"width:582px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-203-1024x739.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-203-300x217.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-203-768x554.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-203-1536x1109.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-203-830x599.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-203-230x166.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-203-350x253.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-203-480x346.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-203.png 1560w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"396\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-204-1024x396.png\" alt=\"\" class=\"wp-image-15441\" style=\"width:534px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-204-1024x396.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-204-300x116.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-204-768x297.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-204-830x321.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-204-230x89.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-204-350x135.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-204-480x185.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-204.png 1486w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p><strong>\u573a\u666f\u4e09\uff1a\u4e8b\u52a1\u65b9\u6cd5\u88ab\u975e\u4e8b\u52a1\u65b9\u6cd5\u6355\u83b7\u5f02\u5e38<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5f53@Transactional\u6ce8\u89e3\u7684\u65b9\u6cd5\u629b\u51fa\u5f02\u5e38\u88ab\u540c\u7c7b\u4e2d\u975e\u4e8b\u52a1\u65b9\u6cd5\u6355\u83b7\u65f6\uff0c\u4e8b\u52a1\u53ef\u80fd\u4e0d\u4f1a\u56de\u6eda\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Service\npublic class TransactionService {\n\n    @Transactional\n    public void transactionalMethod() {\n        \/\/ \u6570\u636e\u5e93\u64cd\u4f5c\n        throw new RuntimeException(\"\u6a21\u62df\u5f02\u5e38\");\n    }\n\n    public void nonTransactionalMethod() {\n        try {\n            transactionalMethod();\n        } catch (Exception e) {\n            \/\/ \u5f02\u5e38\u88ab\u6355\u83b7\uff0c\u4e8b\u52a1\u53ef\u80fd\u4e0d\u56de\u6eda\n        }\n    }\n}<\/pre>\n\n\n\n<p><strong>\u573a\u666f\u56db\uff1a\u4e0d\u652f\u6301\u4e8b\u52a1\u7684\u6570\u636e\u5e93\u64cd\u4f5c<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u4f7f\u7528JPA\u6216Hibernate\u65f6\uff0c\u5982\u679c\u6267\u884c\u7684\u64cd\u4f5c\u4e0d\u652f\u6301\u4e8b\u52a1\uff0c\u6bd4\u5982\u53ea\u8bfb\u67e5\u8be2\uff0c\u90a3\u4e48\u5373\u4f7f\u4f60\u52a0\u4e86@Transactional\uff0c\u4e8b\u52a1\u4e5f\u4e0d\u4f1a\u751f\u6548\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Service\npublic class TransactionService {\n\n    @PersistenceContext\n    private EntityManager entityManager;\n\n    @Transactional\n    public void readOnlyOperation() {\n        \/\/ \u6267\u884c\u53ea\u8bfb\u67e5\u8be2\uff0c\u4e8b\u52a1\u4e0d\u4f1a\u751f\u6548\n        entityManager.createQuery(\"SELECT t FROM Table t\").getResultList();\n    }\n}<\/pre>\n\n\n\n<p><strong>\u573a\u666f\u4e94\uff1a\u5f02\u5e38\u7c7b\u578b\u4e0d\u89e6\u53d1\u56de\u6eda<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u53ea\u6709\u8fd0\u884c\u65f6\u5f02\u5e38\uff08RuntimeException\u53ca\u5176\u5b50\u7c7b\uff09\u548c\u9519\u8bef\uff08Error\uff09\u624d\u4f1a\u89e6\u53d1\u4e8b\u52a1\u56de\u6eda\u3002\u5982\u679c\u4f60\u629b\u51fa\u7684\u662f\u68c0\u67e5\u578b\u5f02\u5e38\uff08\u5373\u975e\u8fd0\u884c\u65f6\u5f02\u5e38\uff09\uff0c\u5219\u4e0d\u4f1a\u89e6\u53d1\u4e8b\u52a1\u56de\u6eda\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Service\npublic class TransactionService {\n\n    @Transactional\n    public void operationThrowsCheckedException() throws Exception {\n        \/\/ \u6570\u636e\u5e93\u64cd\u4f5c\n        throw new Exception(\"\u8fd9\u662f\u4e00\u4e2a\u68c0\u67e5\u578b\u5f02\u5e38\");\n    }\n}<\/pre>\n\n\n\n<p><strong>\u573a\u666f\u516d\uff1a\u4e8b\u52a1\u4f20\u64ad\u884c\u4e3a\u914d\u7f6e\u4e0d\u5f53<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>@Transactional\u6ce8\u89e3\u7684propagation\u5c5e\u6027\u5b9a\u4e49\u4e86\u4e8b\u52a1\u7684\u4f20\u64ad\u884c\u4e3a\u3002\u5982\u679c\u914d\u7f6e\u4e0d\u5f53\uff0c\u53ef\u80fd\u5bfc\u81f4\u4e8b\u52a1\u4e0d\u6309\u9884\u671f\u5de5\u4f5c\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Service\npublic class TransactionService {\n\n    @Transactional(propagation = Propagation.SUPPORTS)\n    public void supportsTransaction() {\n        \/\/ \u8fd9\u91cc\u7684\u4e8b\u52a1\u53ea\u6709\u5728\u5df2\u5b58\u5728\u7684\u4e8b\u52a1\u4e2d\u624d\u4f1a\u751f\u6548\n    }\n}<\/pre>\n\n\n\n<p><strong>\u573a\u666f\u4e03\uff1a\u66f4\u65b0\u975e\u6301\u4e45\u5316\u5bf9\u8c61<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5728JPA\u4e2d\uff0c\u5982\u679c\u4f60\u5c1d\u8bd5\u66f4\u65b0\u4e00\u4e2a\u975e\u6301\u4e45\u5316\u5bf9\u8c61\uff0c\u4e8b\u52a1\u662f\u4e0d\u4f1a\u751f\u6548\u7684\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Service\npublic class TransactionService {\n\n    @PersistenceContext\n    private EntityManager entityManager;\n\n    @Transactional\n    public void updateNonPersistentObject() {\n        Entity entity = new Entity(); \/\/ \u975e\u6301\u4e45\u5316\u5bf9\u8c61\n        entity.setId(1);\n        entity.setName(\"\u66f4\u65b0\u64cd\u4f5c\");\n        entityManager.merge(entity); \/\/ \u66f4\u65b0\u975e\u6301\u4e45\u5316\u5bf9\u8c61\n    }\n}<\/pre>\n\n\n\n<p><strong>\u5b9e\u7528\u6280\u5de7<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u9488\u5bf9\u4e0a\u8ff0\u7684\u5751\uff0c\u4e0b\u9762\u5206\u4eab\u4e00\u4e9b\u907f\u514d\u4e8b\u52a1\u5931\u6548\u7684\u5b9e\u7528\u6280\u5de7\uff1a<\/p>\n\n\n\n<p><strong>\u6280\u5de71\uff1a\u786e\u4fdd@Transactional\u4f5c\u7528\u4e8e\u516c\u5171\u65b9\u6cd5<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Service\npublic class TransactionalService {\n\n    \/\/ \u6b63\u786e\u4f7f\u7528\uff1a\u5c06@Transactional\u653e\u5728\u516c\u5171\u65b9\u6cd5\u4e0a\n    @Transactional\n    public void publicTransactionalMethod() {\n        \/\/ \u6570\u636e\u5e93\u64cd\u4f5c\n    }\n}<\/pre>\n\n\n\n<p><strong>\u6280\u5de72\uff1a\u5916\u90e8\u8c03\u7528\u4e8b\u52a1\u65b9\u6cd5<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Service\npublic class ExternalService {\n\n    @Autowired\n    private TransactionalService transactionalService;\n\n    public void method() {\n        \/\/ \u4ece\u53e6\u4e00\u4e2aBean\u8c03\u7528\u4e8b\u52a1\u65b9\u6cd5\uff0c\u786e\u4fdd\u4e8b\u52a1\u751f\u6548\n        transactionalService.publicTransactionalMethod();\n    }\n}\n\n@Service\npublic class TransactionalService {\n\n    @Transactional\n    public void publicTransactionalMethod() {\n        \/\/ \u6570\u636e\u5e93\u64cd\u4f5c\n    }\n}<\/pre>\n\n\n\n<p><strong>\u6280\u5de73\uff1a\u6b63\u786e\u5904\u7406\u5f02\u5e38<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Service\npublic class TransactionalService {\n\n    @Transactional\n    public void transactionalMethodWithException() {\n        try {\n            \/\/ \u6570\u636e\u5e93\u64cd\u4f5c\n            throw new RuntimeException(\"\u6a21\u62df\u5f02\u5e38\");\n        } catch (RuntimeException e) {\n            \/\/ \u4e0d\u8981\u5728\u8fd9\u91cc\u6355\u83b7\u5e76\u5904\u7406\u5f02\u5e38\uff0c\u8ba9\u5b83\u4f20\u64ad\u51fa\u53bb\uff0c\u4ee5\u89e6\u53d1\u4e8b\u52a1\u56de\u6eda\n            throw e;\n        }\n    }\n}<\/pre>\n\n\n\n<p><strong>\u6280\u5de74\uff1a\u4f7f\u7528\u5408\u9002\u7684\u4e8b\u52a1\u4f20\u64ad\u884c\u4e3a<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Service\npublic class TransactionalService {\n\n    \/\/ \u4f7f\u7528REQUIRED\u4f20\u64ad\u884c\u4e3a\uff0c\u8fd9\u662f\u6700\u5e38\u7528\u7684\u4e8b\u52a1\u4f20\u64ad\u884c\u4e3a\n    @Transactional(propagation = Propagation.REQUIRED)\n    public void requiredTransactionalMethod() {\n        \/\/ \u6570\u636e\u5e93\u64cd\u4f5c\n    }\n}<\/pre>\n\n\n\n<p><strong>\u6280\u5de75\uff1a\u5728\u4e8b\u52a1\u65b9\u6cd5\u4e2d\u5904\u7406\u6570\u636e\u5e93\u64cd\u4f5c<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Service\npublic class TransactionalService {\n\n    @Autowired\n    private UserRepository userRepository;\n\n    @Transactional\n    public void performDatabaseOperations() {\n        \/\/ \u786e\u4fdd\u6240\u6709\u6570\u636e\u5e93\u64cd\u4f5c\u90fd\u5728\u4e8b\u52a1\u65b9\u6cd5\u4e2d\u6267\u884c\n        userRepository.save(new User(\"\u7528\u6237\u540d\", \"email@example.com\"));\n        \/\/ \u5176\u4ed6\u6570\u636e\u5e93\u64cd\u4f5c\n    }\n}<\/pre>\n\n\n\n<p><strong>\u6280\u5de76\uff1a\u9488\u5bf9\u68c0\u67e5\u578b\u5f02\u5e38\u5b9a\u4e49\u4e8b\u52a1\u56de\u6eda\u89c4\u5219<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Service\npublic class TransactionalService {\n\n    @Transactional(rollbackFor = Exception.class)\n    public void transactionalMethodWithCheckedException() throws Exception {\n        \/\/ \u6570\u636e\u5e93\u64cd\u4f5c\n        throw new Exception(\"\u68c0\u67e5\u578b\u5f02\u5e38\");\n    }\n}<\/pre>\n\n\n\n<p><strong>\u6280\u5de77\uff1a\u907f\u514d\u5728\u53ea\u8bfb\u64cd\u4f5c\u4e0a\u4f7f\u7528\u4e8b\u52a1<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Service\npublic class TransactionalService {\n\n    @Autowired\n    private UserRepository userRepository;\n\n    \/\/ \u4f7f\u7528readOnly = true\u5bf9\u53ea\u8bfb\u64cd\u4f5c\u8fdb\u884c\u4f18\u5316\n    @Transactional(readOnly = true)\n    public User findUserById(Long id) {\n        return userRepository.findById(id).orElse(null);\n    }\n}<\/pre>\n\n\n\n<p><strong>\u6280\u5de78\uff1a\u66f4\u65b0\u6301\u4e45\u5316\u5bf9\u8c61<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Service\npublic class TransactionalService {\n\n    @PersistenceContext\n    private EntityManager entityManager;\n\n    @Transactional\n    public void updatePersistentObject(Long id) {\n        User user = entityManager.find(User.class, id);\n        if (user != null) {\n            user.setName(\"\u65b0\u540d\u79f0\");\n            entityManager.merge(user); \/\/ \u66f4\u65b0\u6301\u4e45\u5316\u5bf9\u8c61\n        }\n    }\n}<\/pre>\n\n\n\n<p><strong>\u6280\u5de79\uff1a\u907f\u514d\u590d\u6742\u7684\u4e1a\u52a1\u903b\u8f91\u5728\u4e8b\u52a1\u4e2d<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Service\npublic class ComplexBusinessService {\n\n    @Autowired\n    private TransactionalService transactionalService;\n\n    public void complexBusinessOperation() {\n        \/\/ \u5c06\u590d\u6742\u4e1a\u52a1\u903b\u8f91\u62c6\u5206\u6210\u591a\u4e2a\u7b80\u5355\u7684\u4e8b\u52a1\u65b9\u6cd5\u8c03\u7528\n        transactionalService.performPart1();\n        transactionalService.performPart2();\n        \/\/ \u66f4\u591a\u7684\u4e8b\u52a1\u65b9\u6cd5\u8c03\u7528\n    }\n}\n\n@Service\npublic class TransactionalService {\n\n    @Transactional\n    public void performPart1() {\n        \/\/ \u7b2c\u4e00\u90e8\u5206\u7684\u6570\u636e\u5e93\u64cd\u4f5c\n    }\n\n    @Transactional\n    public void performPart2() {\n        \/\/ \u7b2c\u4e8c\u90e8\u5206\u7684\u6570\u636e\u5e93\u64cd\u4f5c\n    }\n}<\/pre>\n\n\n\n<p><strong>\u6280\u5de710\uff1a\u6301\u7eed\u76d1\u63a7\u548c\u5ba1\u8ba1<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Service\npublic class AuditService {\n\n    @Autowired\n    private TransactionalService transactionalService;\n\n    public void audit() {\n        \/\/ \u5b9a\u671f\u5ba1\u8ba1\u4e8b\u52a1\u65b9\u6cd5\u7684\u8c03\u7528\uff0c\u786e\u4fdd\u4e8b\u52a1\u6309\u9884\u671f\u6267\u884c\n        transactionalService.auditedOperation();\n    }\n}\n\n@Service\npublic class TransactionalService {\n\n    @Transactional\n    public void auditedOperation() {\n        \/\/ \u88ab\u5ba1\u8ba1\u7684\u4e8b\u52a1\u64cd\u4f5c\n    }\n}<\/pre>\n\n\n\n<p><strong>\u672c\u6587\u603b\u7ed3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>SpringBoot\u7684\u4e8b\u52a1\u7ba1\u7406\u529f\u80fd\u975e\u5e38\u5f3a\u5927\uff0c\u4f46\u662f\u5b83\u4e5f\u6709\u4e00\u5b9a\u7684\u4f7f\u7528\u89c4\u5219\u3002<\/p>\n\n\n\n<p>\u4e86\u89e3\u5e76\u907f\u514d\u8fd9\u4e9b\u5e38\u89c1\u7684\u4e8b\u52a1\u5931\u6548\u573a\u666f\uff0c\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u5229\u7528SpringBoot\u6784\u5efa\u5065\u58ee\u7684\u5e94\u7528\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u4f60\u5c11\u8e29\u4e00\u4e9b\u5751\uff0c\u987a\u5229\u5b9e\u73b0\u4f60\u7684\u4e1a\u52a1\u903b\u8f91\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u54b1\u4eec\u6765\u804a\u804aSpringBoot\u4e2d\u7684\u4e00\u4e2a\u8d85\u7ea7\u91cd\u8981\u7684\u65b0\u4eba\u5bb9\u6613\u8e29\u7684\u5751\uff1a SpringBoot\u4e2d\u4e8b\u52a1\u5931\u6548\u7684\u90a3\u4e9b\u5751\uff01 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,27],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/15439"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=15439"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/15439\/revisions"}],"predecessor-version":[{"id":15442,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/15439\/revisions\/15442"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=15439"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=15439"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=15439"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}