{"id":11570,"date":"2023-11-28T22:25:24","date_gmt":"2023-11-28T14:25:24","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=11570"},"modified":"2023-12-11T11:21:32","modified_gmt":"2023-12-11T03:21:32","slug":"spring-zhongtransactionalrollbackfor-exceptioncla","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/spring-zhongtransactionalrollbackfor-exceptioncla\/","title":{"rendered":"Spring\u4e2d@Transactional(rollbackFor = Exception.class)\u6ce8\u89e3\u4e86\u89e3\u5417\uff1f"},"content":{"rendered":"<p>\u662f\u7684\uff0c<code>@Transactional<\/code>\u6ce8\u89e3\u662fSpring\u6846\u67b6\u4e2d\u7528\u4e8e\u58f0\u660e\u4e8b\u52a1\u884c\u4e3a\u7684\u6ce8\u89e3\u4e4b\u4e00\u3002\u901a\u8fc7\u5728\u65b9\u6cd5\u6216\u7c7b\u4e0a\u4f7f\u7528<code>@Transactional<\/code>\u6ce8\u89e3\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u544a\u8bc9Spring\u6846\u67b6\u5c06\u76f8\u5e94\u7684\u65b9\u6cd5\u6216\u7c7b\u5305\u88c5\u5728\u4e8b\u52a1\u4e2d\u3002<\/p>\n<pre><code class=\"language-java\">import org.springframework.transaction.annotation.Transactional;\n\n@Service\npublic class MyService {\n\n    @Transactional(rollbackFor = Exception.class)\n    public void myTransactionalMethod() {\n        \/\/ \u4e1a\u52a1\u903b\u8f91\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\uff0c<code>@Transactional(rollbackFor = Exception.class)<\/code>\u6ce8\u89e3\u5e94\u7528\u5728<code>myTransactionalMethod<\/code>\u65b9\u6cd5\u4e0a\uff0c\u8868\u793a\u8be5\u65b9\u6cd5\u5e94\u8be5\u5728\u4e00\u4e2a\u4e8b\u52a1\u4e2d\u8fd0\u884c\uff0c\u5e76\u4e14\u5728\u9047\u5230<code>Exception<\/code>\u6216\u5176\u5b50\u7c7b\u629b\u51fa\u65f6\u8fdb\u884c\u56de\u6eda\u3002<\/p>\n<p>\u5173\u4e8e<code>rollbackFor<\/code>\u5c5e\u6027\u7684\u89e3\u91ca\uff1a<\/p>\n<ul>\n<li><strong>rollbackFor\uff1a<\/strong> \u7528\u4e8e\u6307\u5b9a\u5728\u54ea\u4e9b\u5f02\u5e38\u53d1\u751f\u65f6\u8fdb\u884c\u4e8b\u52a1\u56de\u6eda\u3002\u53ef\u4ee5\u6307\u5b9a\u4e00\u4e2a\u6216\u591a\u4e2a\u5f02\u5e38\u7c7b\uff0c\u5982\u679c\u65b9\u6cd5\u4e2d\u629b\u51fa\u7684\u5f02\u5e38\u662f\u6307\u5b9a\u5f02\u5e38\u7c7b\u6216\u5176\u5b50\u7c7b\uff0c\u5219\u4f1a\u89e6\u53d1\u4e8b\u52a1\u56de\u6eda\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>@Transactional<\/code>\u6ce8\u89e3\u4f1a\u5728\u9047\u5230<code>RuntimeException<\/code>\u53ca\u5176\u5b50\u7c7b\u548c<code>Error<\/code>\u65f6\u56de\u6eda\u4e8b\u52a1\uff0c\u800c\u5176\u4ed6\u5f02\u5e38\u4e0d\u4f1a\u89e6\u53d1\u56de\u6eda\u3002\u4f7f\u7528<code>rollbackFor<\/code>\u5c5e\u6027\u53ef\u4ee5\u81ea\u5b9a\u4e49\u56de\u6eda\u7684\u5f02\u5e38\u7c7b\u578b\u3002<\/li>\n<\/ul>\n<p>\u4f7f\u7528<code>rollbackFor<\/code>\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-java\">import org.springframework.transaction.annotation.Transactional;\n\n@Service\npublic class MyService {\n\n    @Transactional(rollbackFor = { CustomException1.class, CustomException2.class })\n    public void myTransactionalMethod() throws CustomException1, CustomException2 {\n        \/\/ \u4e1a\u52a1\u903b\u8f91\uff0c\u53ef\u80fd\u629b\u51fa CustomException1 \u6216 CustomException2\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5982\u679c<code>myTransactionalMethod<\/code>\u65b9\u6cd5\u4e2d\u629b\u51fa\u4e86<code>CustomException1<\/code>\u6216<code>CustomException2<\/code>\uff0c\u4e8b\u52a1\u5c06\u4f1a\u56de\u6eda\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u662f\u7684\uff0c@Transactional\u6ce8\u89e3\u662fSpring\u6846\u67b6\u4e2d\u7528\u4e8e\u58f0\u660e\u4e8b\u52a1\u884c\u4e3a\u7684\u6ce8\u89e3\u4e4b\u4e00\u3002\u901a\u8fc7\u5728\u65b9\u6cd5\u6216\u7c7b\u4e0a\u4f7f\u7528@T [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[121],"tags":[],"class_list":["post-11570","post","type-post","status-publish","format-standard","hentry","category-spring"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11570","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=11570"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11570\/revisions"}],"predecessor-version":[{"id":17080,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11570\/revisions\/17080"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=11570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=11570"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=11570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}