{"id":1568,"date":"2017-09-13T15:16:57","date_gmt":"2017-09-13T07:16:57","guid":{"rendered":"http:\/\/coderbee.net\/?p=1568"},"modified":"2017-09-13T15:16:57","modified_gmt":"2017-09-13T07:16:57","slug":"spring-%e4%ba%8b%e5%8a%a1%e7%ae%a1%e7%90%86%e7%9a%84%e4%b8%80%e4%b8%aa-trick","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/framework\/20170913\/1568","title":{"rendered":"Spring \u4e8b\u52a1\u7ba1\u7406\u7684\u4e00\u4e2a trick"},"content":{"rendered":"<h2>\u95ee\u9898<\/h2>\n<p>\u6700\u8fd1\u6709\u540c\u4e8b\u78b0\u5230\u8fd9\u4e2a\u5f02\u5e38\u4fe1\u606f\uff1a <code>Transaction rolled back because it has been marked as rollback-only<\/code> \uff0c\u5f02\u5e38\u6808\u88ab\u5403\u4e86\uff0c\u6ca1\u6253\u5370\u51fa\u6765\u3002<\/p>\n<p>\u8c03\u7528\u4ee3\u7801\u5927\u6982\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">@Component\npublic class InnerService {\n    @Autowired\n    private JdbcTemplate jdbcTemplate;\n\n    @Transactional(rollbackFor = Throwable.class)\n    public void innerTx(boolean ex) {\n        jdbcTemplate.execute(\"insert into t_user(uname, age) values('liuwhb', 31)\");\n        if (ex) {\n            throw new NullPointerException();\n        }\n    }\n\n}\n\n@Component\npublic class OutterService {\n    @Autowired\n    private InnerService innerService;\n\n    @Transactional(rollbackFor = Throwable.class)\n    public void outTx(boolean ex) {\n        try {\n            innerService.innerTx(ex);\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n}\n\noutterService.outTx(true);\n<\/code><\/pre>\n<p>\u4ed6\u671f\u671b\u7684\u662f <code>innerService.innerTx(ex);<\/code> \u8c03\u7528\u5373\u4f7f\u5931\u8d25\u4e86\u4e5f\u4e0d\u4f1a\u5f71\u54cd <code>OutterService.outTx<\/code> \u65b9\u6cd5\u4e0a\u7684\u4e8b\u52a1\uff0c\u53ea\u56de\u6eda\u4e86 <code>innerTx<\/code> \u7684\u64cd\u4f5c\u3002<\/p>\n<p>\u7ed3\u679c\u6ca1\u6709\u5f97\u5230\u4ed6\u60f3\u8981\u7684\uff0c\u8c03\u7528 <code>OutterService.outTx<\/code> \u7684\u5916\u56f4\u65b9\u6cd5\u6355\u83b7\u5230\u4e86\u5f02\u5e38\uff0c\u5f02\u5e38\u4fe1\u606f\u662f <code>Transaction rolled back because it has been marked as rollback-only<\/code>\uff0c<code>outTx<\/code> \u7684\u5176\u4ed6\u64cd\u4f5c\u4e5f\u6ca1\u6709\u63d0\u4ea4\u4e8b\u52a1\u3002<\/p>\n<h2>\u5206\u6790<\/h2>\n<p>\u4e0a\u8ff0\u65b9\u6cd5\u7684\u4e8b\u52a1\u4f20\u64ad\u673a\u5236\u7684\u9ed8\u8ba4\u7684\uff0c\u4e5f\u5c31\u662f <code>Propagation.REQUIRED<\/code>\uff0c\u5982\u679c\u5f53\u524d\u5df2\u6709\u4e8b\u52a1\u5c31\u52a0\u5165\u5f53\u524d\u4e8b\u52a1\uff0c\u6ca1\u6709\u5c31\u65b0\u5efa\u4e00\u4e2a\u4e8b\u52a1\u3002<\/p>\n<p>\u4e8b\u52a1\u6027\u7684\u65b9\u6cd5 <code>outTx<\/code> \u8c03\u7528\u4e86\u53e6\u4e00\u4e2a\u4e8b\u52a1\u6027\u7684\u65b9\u6cd5 <code>innerTx<\/code> \u3002\u8c03\u7528\u65b9\u5bf9\u88ab\u8c03\u7528\u7684\u4e8b\u52a1\u65b9\u6cd5\u8fdb\u884c\u5f02\u5e38\u6355\u83b7\uff0c\u76ee\u7684\u662f\u5e0c\u671b\u88ab\u8c03\u7528\u65b9\u7684\u5f02\u5e38\u4e0d\u4f1a\u5f71\u54cd\u8c03\u7528\u65b9\u7684\u4e8b\u52a1\u3002<\/p>\n<p>\u4f46\u8fd8\u662f\u4f1a\u5f71\u54cd\u8c03\u7528\u65b9\u7684\u884c\u4e3a\u7684\u3002Spring \u6355\u83b7\u5230\u88ab\u8c03\u7528\u4e8b\u52a1\u65b9\u6cd5\u7684\u5f02\u5e38\u540e\uff0c\u4f1a\u628a\u4e8b\u52a1\u6807\u8bb0\u4e3a <code>read-only<\/code>\uff0c\u7136\u540e\u8c03\u7528\u65b9\u63d0\u4ea4\u4e8b\u52a1\u7684\u65f6\u5019\u53d1\u73b0\u4e8b\u52a1\u662f\u53ea\u8bfb\u7684\uff0c\u5c31\u4f1a\u629b\u51fa\u4e0a\u9762\u7684\u5f02\u5e38\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u89e3\u51b3\u65b9\u6cd5<\/h2>\n<p>\u7f51\u4e0a\u6709\u4eba\u628a <code>AbstractPlatformTransactionManager.globalRollbackOnParticipationFailure<\/code> \u5c5e\u6027\u8bbe\u7f6e\u4e3a <code>false<\/code>\uff0c\u8bf4\u4e5f\u628a\u95ee\u9898\u89e3\u51b3\u4e86\u3002<\/p>\n<p>\u4ed4\u7ec6\u770b\u4e86\u66f4\u65b0\u8fd9\u4e2a\u5b57\u6bb5\u7684\u65b9\u6cd5 <code>AbstractPlatformTransactionManager.setGlobalRollbackOnParticipationFailure<\/code> \u4e0a\u7684\u6ce8\u91ca\u53d1\u73b0\u4fee\u6539\u8fd9\u4e2a\u5b57\u6bb5\u5e76\u4e0d\u662f\u89e3\u51b3\u4e0a\u9762\u7684\u573a\u666f\u7684\u6700\u4f73\u505a\u6cd5\uff0c\u53cd\u800c\u53ef\u80fd\u5f15\u5165\u5751\u3002<\/p>\n<p>\u6ce8\u91ca\u5927\u610f\u5982\u4e0b\uff1a<\/p>\n<blockquote>\n<ul>\n<li>\u9ed8\u8ba4\u662f &#8220;true&#8221;\uff1a\u5982\u679c\u4e00\u4e2a\u53c2\u4e0e\u4e8b\u52a1\uff08\u4f8b\u5982\uff0c\u6807\u8bb0\u4e3a\u7684 <code>PROPAGATION_REQUIRES<\/code> \u6216 <code>PROPAGATION_SUPPORTS<\/code> \u78b0\u5230\u4e00\u4e2a\u5df2\u6709\u4e8b\u52a1\u65f6\u5c31\u662f\u53c2\u4e0e\u4e8b\u52a1\uff09\u5931\u8d25\uff0c\u4e8b\u52a1\u5c06\u88ab\u5168\u5c40\u5730\u6807\u8bb0\u4e3a rollback-only \u3002\u8fd9\u4e2a\u4e8b\u52a1\u7684\u552f\u4e00\u7ed3\u679c\u5c31\u662f\u56de\u6eda\uff1a\u4e8b\u52a1\u53d1\u8d77\u8005\u518d\u4e5f\u4e0d\u80fd\u63d0\u4ea4\u4e8b\u52a1\u3002<\/li>\n<li>\u5207\u6362\u4e3a &#8220;false&#8221; \u53ef\u4ee5\u8ba9\u4e8b\u52a1\u53d1\u8d77\u8005\u51b3\u5b9a\u662f\u5426\u56de\u6eda\u3002\u5982\u679c\u53c2\u4e0e\u4e8b\u52a1\u56e0\u4e3a\u5f02\u5e38\u5931\u8d25\uff0c\u8c03\u7528\u8005\u4ecd\u7136\u53ef\u4ee5\u51b3\u5b9a\u7ee7\u7eed\u8d70\u4e8b\u52a1\u5185\u7684\u4e0d\u540c\u8def\u5f84\u3002\u7136\u800c\uff0c\u8fd9\u53ea\u6709\u5728\u6240\u6709\u53c2\u4e0e\u8d44\u6e90\u90fd\u5141\u8bb8\u7ee7\u7eed\u76f4\u5230\u4e8b\u52a1\u63d0\u4ea4\uff0c\u5373\u4f7f\u6570\u636e\u8bbf\u95ee\u5931\u8d25\u3002<\/li>\n<li>Note:This flag only applies to an explicit rollback attempt for a subtransaction, typically caused by an exception thrown by a data access operation (where TransactionInterceptor will trigger a PlatformTransactionManager.rollback() call according to a rollback rule). If the flag is off, the caller can handle the exception and decide on a rollback, independent of the rollback rules of the subtransaction. This flag does, however, not apply to explicit setRollbackOnly calls on a TransactionStatus, which will always cause an eventual global rollback (as it might not throw an exception after the rollback-only call). <\/li>\n<li>\u5904\u7406\u5b50\u4e8b\u52a1\u5931\u8d25\u7684\u63a8\u8350\u65b9\u6848\u662f \u5d4c\u5957\u4e8b\u52a1\uff1a\u5168\u5c40\u4e8b\u52a1\u53ef\u4ee5\u56de\u6eda\u5230\u5b50\u4e8b\u52a1\u5f00\u59cb\u7684\u5b89\u5168\u70b9\u3002 <code>PROPAGATION_NESTED<\/code> \u63d0\u4f9b\u4e86\u8fd9\u79cd\u8bed\u4e49\u3002\u5f53\u7136\uff0c\u8fd9\u4e2a\u53ea\u6709\u5728\u4f7f\u7528\u652f\u6301\u5d4c\u5957\u4e8b\u52a1\u7684 <code>DataSourceTransactionManager<\/code> \u65f6\u751f\u6548\uff0c <code>JtaTransactionManager<\/code> \u4e0d\u652f\u6301\u5d4c\u5957\u4e8b\u52a1\u3002<\/li>\n<\/ul>\n<\/blockquote>\n<p>\u4e4b\u6240\u4ee5\u8bf4\u4fee\u6539\u8fd9\u4e2a\u5b57\u6bb5\u53ef\u80fd\u5f15\u5165\u5751\u662f\u56e0\u4e3a\uff1a\u5bb9\u6613\u8ba9\u4eba\u8bef\u4ee5\u4e3a <code>innerTx<\/code> \u629b\u51fa\u5f02\u5e38\u540e\uff0c\u5b83\u505a\u7684\u64cd\u4f5c\u5c31\u88ab\u56de\u6eda\u4e86\uff0c\u5176\u5b9e\u662f\u6ca1\u6709\u7684\uff0c\u8fd9\u4e9b\u64cd\u4f5c\u4f1a\u8ddf\u968f <code>outTx<\/code> \u4e0a\u7684\u4e8b\u52a1\u4e00\u8d77\u63d0\u4ea4\u3002\u4e5f\u5c31\u662f\u8bf4  <code>innerTx<\/code> \u91cc\u7684\u64cd\u4f5c\u53ef\u80fd\u53ea\u5b8c\u6210\u4e86\u4e00\u90e8\u5206\uff0c\u8fd9\u5c31\u7834\u5916\u4e86\u4e8b\u52a1\u7684\u5b8c\u6574\u6027\u3002<\/p>\n<p>\u628a <code>innerTx<\/code> \u6807\u8bb0\u4e3a <code>@Transactional(propagation = Propagation.NESTED)<\/code> \u53ef\u4ee5\u4fdd\u8bc1 <code>innerTx<\/code> \u91cc\u64cd\u4f5c\u7684\u4e8b\u52a1\u5b8c\u6574\u6027\u3002<\/p>\n<p>\u8fd9\u5176\u5b9e\u662f\u4e2a\u5d4c\u5957\u4e8b\u52a1\u7684\u5904\u7406\u573a\u666f\u3002<\/p>\n<p>\u5176\u4ed6\u7684\u6d4b\u8bd5\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"sql\">DROP TABLE IF EXISTS t_user;\ncreate table t_user(uid int auto_increment , uname VARCHAR(100), age int, PRIMARY KEY(uid) ) ENGINE = INNODB default CHARSET UTF8;\n<\/code><\/pre>\n<pre><code class=\"java\">@SpringBootApplication\npublic class TxApp {\n    private static Logger logger = LoggerFactory.getLogger(TxApp.class);\n\n    @Bean\n    public PlatformTransactionManager initTransactionManager(DataSource ds) {\n        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(ds);\n        transactionManager.setGlobalRollbackOnParticipationFailure(false);\n        return transactionManager;\n    }\n\n    public static void main(String[] args) {\n        try (ConfigurableApplicationContext ctx = SpringApplication.run(TxApp.class, args)) {\n            OutterService outterService = ctx.getBean(OutterService.class);\n            \/\/ outterService.outTx(false);\n            outterService.outTx(true);\n            logger.info(\"outTx commit success .\");\n        }\n    }\n\n}\n<\/code><\/pre>\n<hr\/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u95ee\u9898 \u6700\u8fd1\u6709\u540c\u4e8b\u78b0\u5230\u8fd9\u4e2a\u5f02\u5e38\u4fe1\u606f\uff1a Transaction rolled bac &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/framework\/20170913\/1568\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[255],"tags":[172,208],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1568"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=1568"}],"version-history":[{"count":1,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1568\/revisions"}],"predecessor-version":[{"id":1569,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1568\/revisions\/1569"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=1568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=1568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=1568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}