{"id":1436,"date":"2023-03-20T22:02:50","date_gmt":"2023-03-20T14:02:50","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1436"},"modified":"2023-04-28T21:00:13","modified_gmt":"2023-04-28T13:00:13","slug":"spring-boot-specify-transaction-manager","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/20\/spring-boot-specify-transaction-manager\/","title":{"rendered":"Spring Boot\u6307\u5b9a\u4e8b\u52a1\u7ba1\u7406\u5668"},"content":{"rendered":"<p>Spring Boot \u4f7f\u7528\u4e8b\u52a1\u975e\u5e38\u7b80\u5355\uff0c\u9996\u5148\u4f7f\u7528\u6ce8\u89e3<code>@EnableTransactionManagement<\/code>\u5f00\u542f\u4e8b\u52a1\u652f\u6301\u540e\uff0c\u7136\u540e\u5728\u8bbf\u95ee\u6570\u636e\u5e93\u7684<code>Service<\/code>\u65b9\u6cd5\u4e0a\u6dfb\u52a0\u6ce8\u89e3<code>@Transactional<\/code>\u4fbf\u53ef\u3002<\/p>\n<p>\u5173\u4e8e\u4e8b\u52a1\u7ba1\u7406\u5668\uff0c\u4e0d\u7ba1\u662f<code>JPA<\/code>\u8fd8\u662f<code>JDBC<\/code>\u7b49\u90fd\u5b9e\u73b0\u81ea\u63a5\u53e3<code>PlatformTransactionManager<\/code>\u3002\u5982\u679c\u6dfb\u52a0\u7684\u662f<code>spring-boot-starter-jdbc<\/code>\u4f9d\u8d56\uff0c\u6846\u67b6\u4f1a\u9ed8\u8ba4\u6ce8\u5165<code>DataSourceTransactionManager<\/code>\u5b9e\u4f8b\u3002\u5982\u679c\u6dfb\u52a0\u7684\u662f<code>spring-boot-starter-data-jpa<\/code>\u4f9d\u8d56\uff0c\u6846\u67b6\u4f1a\u9ed8\u8ba4\u6ce8\u5165<code>JpaTransactionManager<\/code>\u5b9e\u4f8b\u3002<\/p>\n<p><!-- more --><\/p>\n<p>\u53ef\u4ee5\u5728\u542f\u52a8\u7c7b\u4e2d\u6dfb\u52a0\u5982\u4e0b\u65b9\u6cd5\uff0cDebug\u6d4b\u8bd5\uff0c\u5c31\u80fd\u77e5\u9053\u81ea\u52a8\u6ce8\u5165\u7684\u662f<code>PlatformTransactionManager<\/code>\u63a5\u53e3\u7684\u54ea\u4e2a\u5b9e\u73b0\u7c7b\u3002<\/p>\n<h3>\u6253\u5370\u9879\u76ee\u4e8b\u52a1\u7ba1\u7406\u5668<\/h3>\n<pre><code class=\"language-java\">@EnableTransactionManagement \/\/ \u542f\u6ce8\u89e3\u4e8b\u52a1\u7ba1\u7406\uff0c\u7b49\u540c\u4e8exml\u914d\u7f6e\u65b9\u5f0f\u7684 &lt;tx:annotation-driven \/&gt;\n@SpringBootApplication\npublic class TransactionDemoApplication {\n\n    @Bean\n    public Object testBean(PlatformTransactionManager platformTransactionManager){\n        System.out.println(&quot;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&quot; + platformTransactionManager.getClass().getName());\n        return new Object();\n    }\n\n    public static void main(String[] args) {\n        SpringApplication.run(TransactionDemoApplication.class, args);\n    }\n}<\/code><\/pre>\n<p>\u8fd9\u4e9bSpring Boot\u4e3a\u6211\u4eec\u81ea\u52a8\u505a\u4e86\uff0c\u5bf9\u6211\u4eec\u5e76\u4e0d\u900f\u660e\uff0c\u5982\u679c\u9879\u76ee\u505a\u7684\u6bd4\u8f83\u5927\uff0c\u6dfb\u52a0\u7684\u6301\u4e45\u5316\u4f9d\u8d56\u6bd4\u8f83\u591a\uff0c\u6211\u4eec\u8fd8\u662f\u4f1a\u9009\u62e9\u4eba\u4e3a\u7684\u6307\u5b9a\u4f7f\u7528\u54ea\u4e2a\u4e8b\u52a1\u7ba1\u7406\u5668\u3002<\/p>\n<h3>\u6307\u5b9a\u4e8b\u52a1\u7ba1\u7406\u5668<\/h3>\n<pre><code class=\"language-java\">@EnableTransactionManagement\n@SpringBootApplication\npublic class TransactionDemoApplication {\n\n    \/\/ \u5176\u4e2d dataSource \u6846\u67b6\u4f1a\u81ea\u52a8\u4e3a\u6211\u4eec\u6ce8\u5165\n    @Bean\n    public PlatformTransactionManager txManager(DataSource dataSource) {\n        return new DataSourceTransactionManager(dataSource);\n    }\n\n    @Bean\n    public Object testBean(PlatformTransactionManager platformTransactionManager) {\n        System.out.println(&quot;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&quot; + platformTransactionManager.getClass().getName());\n        return new Object();\n    }\n\n    public static void main(String[] args) {\n        SpringApplication.run(TransactionDemoApplication.class, args);\n    }\n}<\/code><\/pre>\n<p>\u5728Spring\u5bb9\u5668\u4e2d\uff0c\u6211\u4eec\u624b\u5de5\u6ce8\u89e3<code>@Bean<\/code>\u5c06\u88ab\u4f18\u5148\u52a0\u8f7d\uff0c\u6846\u67b6\u4e0d\u4f1a\u91cd\u65b0\u5b9e\u4f8b\u5316\u5176\u4ed6\u7684<code>PlatformTransactionManager<\/code>\u5b9e\u73b0\u7c7b\u3002<\/p>\n<p>\u7136\u540e\u5728Service\u4e2d\uff0c\u88ab<code>@Transactional<\/code>\u6ce8\u89e3\u7684\u65b9\u6cd5\uff0c\u5c06\u652f\u6301\u4e8b\u52a1\u3002\u5982\u679c\u6ce8\u89e3\u5728\u7c7b\u4e0a\uff0c\u5219\u6574\u4e2a\u7c7b\u7684\u6240\u6709\u65b9\u6cd5\u90fd\u9ed8\u8ba4\u652f\u6301\u4e8b\u52a1\u3002<\/p>\n<p>\u5bf9\u4e8e\u540c\u4e00\u4e2a\u5de5\u7a0b\u4e2d\u5b58\u5728\u591a\u4e2a\u4e8b\u52a1\u7ba1\u7406\u5668\u8981\u600e\u4e48\u5904\u7406\uff0c\u8bf7\u770b\u4e0b\u9762\u7684\u5b9e\u4f8b\uff0c\u5177\u4f53\u8bf4\u660e\u8bf7\u770b\u4ee3\u7801\u4e2d\u7684\u6ce8\u91ca\u3002<\/p>\n<h3>\u4f7f\u7528\u6307\u5b9a\u7684\u4e8b\u52a1\u7ba1\u7406\u5668<\/h3>\n<pre><code class=\"language-java\">@EnableTransactionManagement \/\/ \u5f00\u542f\u6ce8\u89e3\u4e8b\u52a1\u7ba1\u7406\uff0c\u7b49\u540c\u4e8exml\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684 &lt;tx:annotation-driven \/&gt;\n@SpringBootApplication\npublic class TransactionDemoApplication implements TransactionManagementConfigurer {\n\n    @Resource(name=&quot;txManager2&quot;)\n    private PlatformTransactionManager txManager2;\n\n    \/\/ \u521b\u5efa\u4e8b\u52a1\u7ba1\u7406\u56681\n    @Bean(name = &quot;txManager1&quot;)\n    public PlatformTransactionManager txManager(DataSource dataSource) {\n        return new DataSourceTransactionManager(dataSource);\n    }\n\n    \/\/ \u521b\u5efa\u4e8b\u52a1\u7ba1\u7406\u56682\n    @Bean(name = &quot;txManager2&quot;)\n    public PlatformTransactionManager txManager2(EntityManagerFactory factory) {\n        return new JpaTransactionManager(factory);\n    }\n\n    \/\/ \u5b9e\u73b0\u63a5\u53e3 TransactionManagementConfigurer \u65b9\u6cd5\uff0c\u5176\u8fd4\u56de\u503c\u4ee3\u8868\u5728\u62e5\u6709\u591a\u4e2a\u4e8b\u52a1\u7ba1\u7406\u5668\u7684\u60c5\u51b5\u4e0b\u9ed8\u8ba4\u4f7f\u7528\u7684\u4e8b\u52a1\u7ba1\u7406\u5668\n    @Override\n    public PlatformTransactionManager annotationDrivenTransactionManager() {\n        return txManager2;\n    }\n\n    public static void main(String[] args) {\n        SpringApplication.run(TransactionDemoApplication.class, args);\n    }\n}\n\n@Component\npublic class DevSendMessage implements SendMessage {\n\n    \/\/ \u4f7f\u7528value\u5177\u4f53\u6307\u5b9a\u4f7f\u7528\u54ea\u4e2a\u4e8b\u52a1\u7ba1\u7406\u5668\n    @Transactional(value=&quot;txManager1&quot;)\n    @Override\n    public void send() {\n        System.out.println(&quot;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;Dev Send()&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&quot;);\n        send2();\n    }\n\n    \/\/ \u5728\u5b58\u5728\u591a\u4e2a\u4e8b\u52a1\u7ba1\u7406\u5668\u7684\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u4f7f\u7528value\u5177\u4f53\u6307\u5b9a\n    \/\/ \u5219\u9ed8\u8ba4\u4f7f\u7528\u65b9\u6cd5 annotationDrivenTransactionManager() \u8fd4\u56de\u7684\u4e8b\u52a1\u7ba1\u7406\u5668\n    @Transactional\n    public void send2() {\n        System.out.println(&quot;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;Dev Send2()&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&quot;);\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Spring Boot \u4f7f\u7528\u4e8b\u52a1\u975e\u5e38\u7b80\u5355\uff0c\u9996\u5148\u4f7f\u7528\u6ce8\u89e3@EnableTransactionManagement [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[87],"class_list":["post-1436","post","type-post","status-publish","format-standard","hentry","category-spring-boot","tag-87"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1436","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1436"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1436\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1436"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1436"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1436"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}