{"id":328564,"date":"2024-05-22T17:25:20","date_gmt":"2024-05-22T09:25:20","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/328564.html"},"modified":"2024-05-22T17:25:35","modified_gmt":"2024-05-22T09:25:35","slug":"java%e9%a1%b9%e7%9b%ae%e6%80%8e%e4%b9%88%e7%ae%a1%e7%90%86%e4%ba%8b%e5%8a%a1","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/328564.html","title":{"rendered":"java\u9879\u76ee\u600e\u4e48\u7ba1\u7406\u4e8b\u52a1"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/05\/35b5f9ca-8956-449e-ab62-fdb2beb0d591.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"java\u9879\u76ee\u600e\u4e48\u7ba1\u7406\u4e8b\u52a1\" \/><\/p>\n<p><p><strong>\u5728Java\u9879\u76ee\u4e2d\uff0c\u4e8b\u52a1\u7ba1\u7406\u7684\u6838\u5fc3\u5728\u4e8e\u786e\u4fdd\u6570\u636e\u7684\u4e00\u81f4\u6027\u3001\u539f\u5b50\u6027\u3001\u9694\u79bb\u6027\u548c\u6301\u4e45\u6027\uff08ACID\u539f\u5219\uff09\u3002\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Spring\u6846\u67b6\u3001\u7f16\u7a0b\u5f0f\u4e8b\u52a1\u7ba1\u7406\u3001\u58f0\u660e\u5f0f\u4e8b\u52a1\u7ba1\u7406\u6765\u5b9e\u73b0\u3002\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0cSpring\u6846\u67b6\u662f\u6700\u4e3a\u5e7f\u6cdb\u4f7f\u7528\u7684\u3002<\/strong><\/p>\n<\/p>\n<p><h2>\u4e00\u3001SPRING\u6846\u67b6<\/h2>\n<\/p>\n<p><p>Spring\u6846\u67b6\u63d0\u4f9b\u4e86\u4e00\u5957\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u4e8b\u52a1\u7ba1\u7406\u673a\u5236\uff0c\u4f7f\u5f97\u5f00\u53d1\u8005\u80fd\u591f\u8f7b\u677e\u7ba1\u7406\u548c\u63a7\u5236\u4e8b\u52a1\u3002Spring\u6846\u67b6\u652f\u6301\u4e24\u79cd\u4e3b\u8981\u7684\u4e8b\u52a1\u7ba1\u7406\u65b9\u5f0f\uff1a\u7f16\u7a0b\u5f0f\u4e8b\u52a1\u7ba1\u7406\u548c\u58f0\u660e\u5f0f\u4e8b\u52a1\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u7f16\u7a0b\u5f0f\u4e8b\u52a1\u7ba1\u7406<\/h3>\n<\/p>\n<p><p>\u7f16\u7a0b\u5f0f\u4e8b\u52a1\u7ba1\u7406\u662f\u6307\u5728\u4ee3\u7801\u4e2d\u663e\u5f0f\u5730\u63a7\u5236\u4e8b\u52a1\u7684\u5f00\u59cb\u3001\u63d0\u4ea4\u548c\u56de\u6eda\u3002\u8fd9\u79cd\u65b9\u5f0f\u63d0\u4f9b\u4e86\u6700\u5927\u7684\u7075\u6d3b\u6027\uff0c\u4f46\u4e5f\u589e\u52a0\u4e86\u4ee3\u7801\u7684\u590d\u6742\u6027\u548c\u7ef4\u62a4\u96be\u5ea6\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528TransactionTemplate<\/h4>\n<\/p>\n<p><p>TransactionTemplate\u7c7b\u63d0\u4f9b\u4e86\u7b80\u5316\u7684\u7f16\u7a0b\u5f0f\u4e8b\u52a1\u7ba1\u7406\u65b9\u6cd5\u3002\u901a\u8fc7TransactionTemplate\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u7ba1\u7406\u4e8b\u52a1\u7684\u8fb9\u754c\u800c\u65e0\u9700\u663e\u5f0f\u5730\u7f16\u5199\u4e8b\u52a1\u63a7\u5236\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">public class MyService {<\/p>\n<p>    private final TransactionTemplate transactionTemplate;<\/p>\n<p>    public MyService(PlatformTransactionManager transactionManager) {<\/p>\n<p>        this.transactionTemplate = new TransactionTemplate(transactionManager);<\/p>\n<p>    }<\/p>\n<p>    public void doSomething() {<\/p>\n<p>        transactionTemplate.execute(status -&gt; {<\/p>\n<p>            \/\/ \u4e1a\u52a1\u903b\u8f91<\/p>\n<p>            if (someCondition) {<\/p>\n<p>                status.setRollbackOnly();<\/p>\n<p>            }<\/p>\n<p>            return null;<\/p>\n<p>        });<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u58f0\u660e\u5f0f\u4e8b\u52a1\u7ba1\u7406<\/h3>\n<\/p>\n<p><p>\u58f0\u660e\u5f0f\u4e8b\u52a1\u7ba1\u7406\u662f\u901a\u8fc7\u914d\u7f6e\u548c\u6ce8\u89e3\u7684\u65b9\u5f0f\u6765\u7ba1\u7406\u4e8b\u52a1\uff0c\u4e0d\u9700\u8981\u5728\u4ee3\u7801\u4e2d\u663e\u5f0f\u5730\u63a7\u5236\u4e8b\u52a1\u3002\u76f8\u6bd4\u7f16\u7a0b\u5f0f\u4e8b\u52a1\u7ba1\u7406\uff0c\u58f0\u660e\u5f0f\u4e8b\u52a1\u7ba1\u7406\u66f4\u52a0\u7b80\u6d01\u548c\u6613\u4e8e\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528@Transactional\u6ce8\u89e3<\/h4>\n<\/p>\n<p><p>Spring\u63d0\u4f9b\u4e86@Transactional\u6ce8\u89e3\u6765\u58f0\u660e\u4e8b\u52a1\u8fb9\u754c\uff0c\u53ef\u4ee5\u5e94\u7528\u4e8e\u65b9\u6cd5\u6216\u7c7b\u4e0a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">@Service<\/p>\n<p>public class MyService {<\/p>\n<p>    @Transactional<\/p>\n<p>    public void performTransaction() {<\/p>\n<p>        \/\/ \u4e1a\u52a1\u903b\u8f91<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001ACID\u539f\u5219<\/h2>\n<\/p>\n<p><p>\u4e8b\u52a1\u7ba1\u7406\u7684\u6838\u5fc3\u662f\u786e\u4fdd\u6570\u636e\u7684\u4e00\u81f4\u6027\uff0c\u8fd9\u5c31\u9700\u8981\u9075\u5b88ACID\u539f\u5219\u3002ACID\u662f\u6307\u539f\u5b50\u6027\uff08Atomicity\uff09\u3001\u4e00\u81f4\u6027\uff08Consistency\uff09\u3001\u9694\u79bb\u6027\uff08Isolation\uff09\u548c\u6301\u4e45\u6027\uff08Durability\uff09\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u539f\u5b50\u6027<\/h3>\n<\/p>\n<p><p>\u539f\u5b50\u6027\u4fdd\u8bc1\u4e86\u4e8b\u52a1\u4e2d\u7684\u6240\u6709\u64cd\u4f5c\u8981\u4e48\u5168\u90e8\u6267\u884c\uff0c\u8981\u4e48\u5168\u90e8\u4e0d\u6267\u884c\u3002\u5373\u4f7f\u5728\u7cfb\u7edf\u5d29\u6e83\u7684\u60c5\u51b5\u4e0b\uff0c\u4e8b\u52a1\u4e5f\u80fd\u591f\u56de\u6eda\u5230\u521d\u59cb\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u4e00\u81f4\u6027<\/h3>\n<\/p>\n<p><p>\u4e00\u81f4\u6027\u786e\u4fdd\u4e8b\u52a1\u4ece\u4e00\u4e2a\u4e00\u81f4\u72b6\u6001\u8f6c\u6362\u5230\u53e6\u4e00\u4e2a\u4e00\u81f4\u72b6\u6001\u3002\u4e8b\u52a1\u5f00\u59cb\u4e4b\u524d\u548c\u7ed3\u675f\u4e4b\u540e\uff0c\u6570\u636e\u5e93\u90fd\u5e94\u8be5\u5904\u4e8e\u4e00\u81f4\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u9694\u79bb\u6027<\/h3>\n<\/p>\n<p><p>\u9694\u79bb\u6027\u4fdd\u8bc1\u4e8b\u52a1\u5f7c\u6b64\u72ec\u7acb\u8fd0\u884c\uff0c\u4e0d\u4f1a\u4e92\u76f8\u5e72\u6270\u3002\u5e38\u89c1\u7684\u9694\u79bb\u7ea7\u522b\u5305\u62ec\u672a\u63d0\u4ea4\u8bfb\u3001\u63d0\u4ea4\u8bfb\u3001\u53ef\u91cd\u590d\u8bfb\u548c\u4e32\u884c\u5316\u3002<\/p>\n<\/p>\n<p><h3>4\u3001\u6301\u4e45\u6027<\/h3>\n<\/p>\n<p><p>\u6301\u4e45\u6027\u786e\u4fdd\u4e8b\u52a1\u63d0\u4ea4\u540e\uff0c\u5176\u7ed3\u679c\u6c38\u4e45\u4fdd\u5b58\u5230\u6570\u636e\u5e93\u4e2d\uff0c\u4e0d\u4f1a\u56e0\u4e3a\u7cfb\u7edf\u6545\u969c\u800c\u4e22\u5931\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4e8b\u52a1\u4f20\u64ad\u884c\u4e3a<\/h2>\n<\/p>\n<p><p>Spring\u63d0\u4f9b\u4e86\u591a\u79cd\u4e8b\u52a1\u4f20\u64ad\u884c\u4e3a\uff0c\u7528\u4e8e\u5b9a\u4e49\u4e8b\u52a1\u65b9\u6cd5\u4e4b\u95f4\u7684\u4f20\u64ad\u89c4\u5219\u3002<\/p>\n<\/p>\n<p><h3>1\u3001REQUIRED<\/h3>\n<\/p>\n<p><p>REQUIRED\u662f\u9ed8\u8ba4\u7684\u4f20\u64ad\u884c\u4e3a\uff0c\u5982\u679c\u5f53\u524d\u5b58\u5728\u4e8b\u52a1\uff0c\u5219\u52a0\u5165\u8be5\u4e8b\u52a1\uff1b\u5982\u679c\u5f53\u524d\u6ca1\u6709\u4e8b\u52a1\uff0c\u5219\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u4e8b\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">@Transactional(propagation = Propagation.REQUIRED)<\/p>\n<p>public void someMethod() {<\/p>\n<p>    \/\/ \u4e1a\u52a1\u903b\u8f91<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001REQUIRES_NEW<\/h3>\n<\/p>\n<p><p>REQUIRES_NEW\u8868\u793a\u6bcf\u6b21\u90fd\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u4e8b\u52a1\uff0c\u5982\u679c\u5f53\u524d\u5b58\u5728\u4e8b\u52a1\uff0c\u5219\u6302\u8d77\u5f53\u524d\u4e8b\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">@Transactional(propagation = Propagation.REQUIRES_NEW)<\/p>\n<p>public void anotherMethod() {<\/p>\n<p>    \/\/ \u4e1a\u52a1\u903b\u8f91<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001SUPPORTS<\/h3>\n<\/p>\n<p><p>SUPPORTS\u8868\u793a\u5f53\u524d\u5b58\u5728\u4e8b\u52a1\u5219\u52a0\u5165\u8be5\u4e8b\u52a1\uff0c\u5982\u679c\u5f53\u524d\u6ca1\u6709\u4e8b\u52a1\uff0c\u5219\u4ee5\u975e\u4e8b\u52a1\u65b9\u5f0f\u6267\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">@Transactional(propagation = Propagation.SUPPORTS)<\/p>\n<p>public void yetAnotherMethod() {<\/p>\n<p>    \/\/ \u4e1a\u52a1\u903b\u8f91<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u4e8b\u52a1\u9694\u79bb\u7ea7\u522b<\/h2>\n<\/p>\n<p><p>\u4e8b\u52a1\u9694\u79bb\u7ea7\u522b\u5b9a\u4e49\u4e86\u4e8b\u52a1\u4e4b\u95f4\u76f8\u4e92\u9694\u79bb\u7684\u7a0b\u5ea6\uff0cSpring\u652f\u6301\u7684\u9694\u79bb\u7ea7\u522b\u6709\u4e94\u79cd\uff1a\u9ed8\u8ba4\u3001\u672a\u63d0\u4ea4\u8bfb\u3001\u63d0\u4ea4\u8bfb\u3001\u53ef\u91cd\u590d\u8bfb\u548c\u4e32\u884c\u5316\u3002<\/p>\n<\/p>\n<p><h3>1\u3001READ_UNCOMMITTED<\/h3>\n<\/p>\n<p><p>READ_UNCOMMITTED\u5141\u8bb8\u8bfb\u53d6\u5c1a\u672a\u63d0\u4ea4\u7684\u66f4\u6539\uff0c\u53ef\u80fd\u5bfc\u81f4\u810f\u8bfb\u73b0\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">@Transactional(isolation = Isolation.READ_UNCOMMITTED)<\/p>\n<p>public void readUncommittedMethod() {<\/p>\n<p>    \/\/ \u4e1a\u52a1\u903b\u8f91<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001READ_COMMITTED<\/h3>\n<\/p>\n<p><p>READ_COMMITTED\u53ea\u5141\u8bb8\u8bfb\u53d6\u5df2\u63d0\u4ea4\u7684\u66f4\u6539\uff0c\u907f\u514d\u4e86\u810f\u8bfb\u95ee\u9898\uff0c\u4f46\u53ef\u80fd\u4f1a\u51fa\u73b0\u4e0d\u53ef\u91cd\u590d\u8bfb\u73b0\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">@Transactional(isolation = Isolation.READ_COMMITTED)<\/p>\n<p>public void readCommittedMethod() {<\/p>\n<p>    \/\/ \u4e1a\u52a1\u903b\u8f91<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001REPEATABLE_READ<\/h3>\n<\/p>\n<p><p>REPEATABLE_READ\u786e\u4fdd\u5728\u540c\u4e00\u4e2a\u4e8b\u52a1\u4e2d\u591a\u6b21\u8bfb\u53d6\u6570\u636e\u65f6\uff0c\u6570\u636e\u662f\u4e00\u81f4\u7684\uff0c\u907f\u514d\u4e86\u4e0d\u53ef\u91cd\u590d\u8bfb\u95ee\u9898\uff0c\u4f46\u53ef\u80fd\u4f1a\u51fa\u73b0\u5e7b\u8bfb\u73b0\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">@Transactional(isolation = Isolation.REPEATABLE_READ)<\/p>\n<p>public void repeatableReadMethod() {<\/p>\n<p>    \/\/ \u4e1a\u52a1\u903b\u8f91<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4\u3001SERIALIZABLE<\/h3>\n<\/p>\n<p><p>SERIALIZABLE\u662f\u6700\u9ad8\u7684\u9694\u79bb\u7ea7\u522b\uff0c\u786e\u4fdd\u4e8b\u52a1\u5b8c\u5168\u9694\u79bb\uff0c\u907f\u514d\u4e86\u810f\u8bfb\u3001\u4e0d\u53ef\u91cd\u590d\u8bfb\u548c\u5e7b\u8bfb\u95ee\u9898\uff0c\u4f46\u6027\u80fd\u8f83\u4f4e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">@Transactional(isolation = Isolation.SERIALIZABLE)<\/p>\n<p>public void serializableMethod() {<\/p>\n<p>    \/\/ \u4e1a\u52a1\u903b\u8f91<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u4e8b\u52a1\u56de\u6eda\u89c4\u5219<\/h2>\n<\/p>\n<p><p>\u5728\u4e8b\u52a1\u7ba1\u7406\u4e2d\uff0c\u5b9a\u4e49\u56de\u6eda\u89c4\u5219\u975e\u5e38\u91cd\u8981\u3002Spring\u6846\u67b6\u5141\u8bb8\u901a\u8fc7\u6ce8\u89e3\u6216\u914d\u7f6e\u6765\u6307\u5b9a\u54ea\u4e9b\u5f02\u5e38\u4f1a\u5bfc\u81f4\u4e8b\u52a1\u56de\u6eda\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u56de\u6eda\u68c0\u67e5\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cSpring\u53ea\u4f1a\u5bf9\u672a\u68c0\u67e5\u5f02\u5e38\uff08RuntimeException\u53ca\u5176\u5b50\u7c7b\uff09\u8fdb\u884c\u56de\u6eda\u3002\u5982\u679c\u9700\u8981\u5bf9\u68c0\u67e5\u5f02\u5e38\uff08Exception\u53ca\u5176\u5b50\u7c7b\uff09\u8fdb\u884c\u56de\u6eda\uff0c\u53ef\u4ee5\u901a\u8fc7\u914d\u7f6e\u5c5e\u6027\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">@Transactional(rollbackFor = Exception.class)<\/p>\n<p>public void methodWithRollback() throws Exception {<\/p>\n<p>    \/\/ \u4e1a\u52a1\u903b\u8f91<\/p>\n<p>    if (someCondition) {<\/p>\n<p>        throw new Exception(&quot;Something went wrong&quot;);<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u56de\u6eda\u672a\u68c0\u67e5\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u672a\u68c0\u67e5\u5f02\u5e38\u9ed8\u8ba4\u4f1a\u5bfc\u81f4\u4e8b\u52a1\u56de\u6eda\uff0c\u65e0\u9700\u989d\u5916\u914d\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">@Transactional<\/p>\n<p>public void anotherMethodWithRollback() {<\/p>\n<p>    \/\/ \u4e1a\u52a1\u903b\u8f91<\/p>\n<p>    if (anotherCondition) {<\/p>\n<p>        throw new RuntimeException(&quot;Something went wrong&quot;);<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516d\u3001\u4e8b\u52a1\u7ba1\u7406\u7684\u6700\u4f73\u5b9e\u8df5<\/h2>\n<\/p>\n<p><p>\u6709\u6548\u7684\u4e8b\u52a1\u7ba1\u7406\u5bf9\u4e8e\u786e\u4fdd\u6570\u636e\u7684\u4e00\u81f4\u6027\u548c\u7cfb\u7edf\u7684\u7a33\u5b9a\u6027\u81f3\u5173\u91cd\u8981\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4e8b\u52a1\u7ba1\u7406\u7684\u6700\u4f73\u5b9e\u8df5\uff1a<\/p>\n<\/p>\n<p><h3>1\u3001\u660e\u786e\u4e8b\u52a1\u8fb9\u754c<\/h3>\n<\/p>\n<p><p>\u786e\u4fdd\u5728\u9002\u5f53\u7684\u5730\u65b9\u5b9a\u4e49\u4e8b\u52a1\u8fb9\u754c\uff0c\u907f\u514d\u5c06\u4e0d\u76f8\u5173\u7684\u64cd\u4f5c\u5305\u542b\u5728\u540c\u4e00\u4e2a\u4e8b\u52a1\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u5c3d\u91cf\u7f29\u5c0f\u4e8b\u52a1\u8303\u56f4<\/h3>\n<\/p>\n<p><p>\u4e8b\u52a1\u8303\u56f4\u8d8a\u5927\uff0c\u9501\u5b9a\u7684\u8d44\u6e90\u8d8a\u591a\uff0c\u5bf9\u7cfb\u7edf\u6027\u80fd\u7684\u5f71\u54cd\u4e5f\u8d8a\u5927\u3002\u5c3d\u91cf\u7f29\u5c0f\u4e8b\u52a1\u8303\u56f4\uff0c\u4ee5\u63d0\u9ad8\u7cfb\u7edf\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u4f7f\u7528\u58f0\u660e\u5f0f\u4e8b\u52a1\u7ba1\u7406<\/h3>\n<\/p>\n<p><p>\u58f0\u660e\u5f0f\u4e8b\u52a1\u7ba1\u7406\u6bd4\u7f16\u7a0b\u5f0f\u4e8b\u52a1\u7ba1\u7406\u66f4\u7b80\u6d01\u3001\u6613\u4e8e\u7ef4\u62a4\uff0c\u5c3d\u91cf\u4f7f\u7528\u58f0\u660e\u5f0f\u4e8b\u52a1\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><h3>4\u3001\u614e\u7528\u9ad8\u9694\u79bb\u7ea7\u522b<\/h3>\n<\/p>\n<p><p>\u9ad8\u9694\u79bb\u7ea7\u522b\u4f1a\u5bf9\u7cfb\u7edf\u6027\u80fd\u4ea7\u751f\u8f83\u5927\u5f71\u54cd\uff0c\u53ea\u6709\u5728\u786e\u5b9e\u9700\u8981\u7684\u60c5\u51b5\u4e0b\u624d\u4f7f\u7528\u9ad8\u9694\u79bb\u7ea7\u522b\u3002<\/p>\n<\/p>\n<p><h3>5\u3001\u5408\u7406\u5904\u7406\u4e8b\u52a1\u56de\u6eda<\/h3>\n<\/p>\n<p><p>\u786e\u4fdd\u5728\u4e8b\u52a1\u56de\u6eda\u65f6\uff0c\u8d44\u6e90\u80fd\u591f\u6b63\u786e\u91ca\u653e\uff0c\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\u3002<\/p>\n<\/p>\n<p><h3>6\u3001\u76d1\u63a7\u548c\u8c03\u4f18<\/h3>\n<\/p>\n<p><p>\u5b9a\u671f\u76d1\u63a7\u4e8b\u52a1\u7684\u6267\u884c\u60c5\u51b5\uff0c\u53ca\u65f6\u53d1\u73b0\u548c\u89e3\u51b3\u6027\u80fd\u74f6\u9888\uff0c\u4e0d\u65ad\u4f18\u5316\u4e8b\u52a1\u7ba1\u7406\u7b56\u7565\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u5206\u5e03\u5f0f\u4e8b\u52a1\u7ba1\u7406<\/h2>\n<\/p>\n<p><p>\u5728\u5fae\u670d\u52a1\u67b6\u6784\u4e2d\uff0c\u4e8b\u52a1\u7ba1\u7406\u53d8\u5f97\u66f4\u52a0\u590d\u6742\uff0c\u56e0\u4e3a\u4e8b\u52a1\u53ef\u80fd\u6d89\u53ca\u591a\u4e2a\u72ec\u7acb\u7684\u670d\u52a1\u3002\u5206\u5e03\u5f0f\u4e8b\u52a1\u7ba1\u7406\u662f\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\u7684\u91cd\u8981\u624b\u6bb5\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u4e24\u9636\u6bb5\u63d0\u4ea4\uff082PC\uff09<\/h3>\n<\/p>\n<p><p>\u4e24\u9636\u6bb5\u63d0\u4ea4\u534f\u8bae\u662f\u4e00\u79cd\u7ecf\u5178\u7684\u5206\u5e03\u5f0f\u4e8b\u52a1\u7ba1\u7406\u65b9\u6848\uff0c\u5305\u62ec\u51c6\u5907\u9636\u6bb5\u548c\u63d0\u4ea4\u9636\u6bb5\u3002\u534f\u8c03\u8005\u8d1f\u8d23\u7ba1\u7406\u4e8b\u52a1\u7684\u63d0\u4ea4\u548c\u56de\u6eda\u3002<\/p>\n<\/p>\n<p><h3>2\u3001Saga\u6a21\u5f0f<\/h3>\n<\/p>\n<p><p>Saga\u6a21\u5f0f\u662f\u4e00\u79cd\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u4e8b\u52a1\u7ba1\u7406\u65b9\u6848\uff0c\u5c06\u4e00\u4e2a\u5927\u4e8b\u52a1\u62c6\u5206\u4e3a\u4e00\u7cfb\u5217\u5c0f\u4e8b\u52a1\uff0c\u6bcf\u4e2a\u5c0f\u4e8b\u52a1\u90fd\u6709\u5bf9\u5e94\u7684\u8865\u507f\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>3\u3001TCC\u6a21\u5f0f<\/h3>\n<\/p>\n<p><p>TCC\uff08Try-Confirm-Cancel\uff09\u6a21\u5f0f\u662f\u4e00\u79cd\u7075\u6d3b\u7684\u5206\u5e03\u5f0f\u4e8b\u52a1\u7ba1\u7406\u65b9\u6848\uff0c\u5305\u62ec\u5c1d\u8bd5\u3001\u786e\u8ba4\u548c\u53d6\u6d88\u4e09\u4e2a\u9636\u6bb5\u3002\u9002\u7528\u4e8e\u9ad8\u5e76\u53d1\u548c\u4f4e\u5ef6\u8fdf\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h2>\u516b\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u4e8b\u52a1\u7ba1\u7406\u5728Java\u9879\u76ee\u4e2d\u81f3\u5173\u91cd\u8981\uff0c\u80fd\u591f\u786e\u4fdd\u6570\u636e\u7684\u4e00\u81f4\u6027\u548c\u7cfb\u7edf\u7684\u7a33\u5b9a\u6027\u3002\u901a\u8fc7Spring\u6846\u67b6\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u7f16\u7a0b\u5f0f\u548c\u58f0\u660e\u5f0f\u4e8b\u52a1\u7ba1\u7406\u3002\u540c\u65f6\uff0c\u9075\u5faaACID\u539f\u5219\u3001\u5408\u7406\u8bbe\u7f6e\u4e8b\u52a1\u4f20\u64ad\u884c\u4e3a\u548c\u9694\u79bb\u7ea7\u522b\u3001\u660e\u786e\u56de\u6eda\u89c4\u5219\u4ee5\u53ca\u91c7\u7528\u6700\u4f73\u5b9e\u8df5\uff0c\u80fd\u591f\u6709\u6548\u63d0\u5347\u4e8b\u52a1\u7ba1\u7406\u7684\u6548\u7387\u548c\u53ef\u9760\u6027\u3002\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u9002\u5f53\u5730\u91c7\u7528\u4e24\u9636\u6bb5\u63d0\u4ea4\u3001Saga\u6a21\u5f0f\u548cTCC\u6a21\u5f0f\uff0c\u53ef\u4ee5\u5b9e\u73b0\u8de8\u670d\u52a1\u7684\u4e8b\u52a1\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0d\u65ad\u5b66\u4e60\u548c\u5b9e\u8df5\uff0c\u638c\u63e1\u4e8b\u52a1\u7ba1\u7406\u7684\u6280\u5de7\u548c\u65b9\u6cd5\uff0c\u80fd\u591f\u4e3aJava\u9879\u76ee\u7684\u6210\u529f\u5b9e\u65bd\u63d0\u4f9b\u575a\u5b9e\u7684\u4fdd\u969c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u4e3a\u4ec0\u4e48\u5728Java\u9879\u76ee\u4e2d\u7ba1\u7406\u4e8b\u52a1\u662f\u5fc5\u8981\u7684\uff1f<\/strong><\/p>\n<p>\u5728Java\u9879\u76ee\u4e2d\uff0c\u4e8b\u52a1\u7ba1\u7406\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4e8b\u52a1\u662f\u4e00\u7cfb\u5217\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u903b\u8f91\u5355\u5143\uff0c\u8981\u4e48\u5168\u90e8\u6267\u884c\u6210\u529f\uff0c\u8981\u4e48\u5168\u90e8\u56de\u6eda\u3002\u901a\u8fc7\u4e8b\u52a1\u7ba1\u7406\uff0c\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u4e00\u81f4\u6027\u548c\u5b8c\u6574\u6027\uff0c\u907f\u514d\u6570\u636e\u4e22\u5931\u548c\u51b2\u7a81\u3002\u56e0\u6b64\uff0c\u5408\u7406\u7ba1\u7406\u4e8b\u52a1\u5bf9\u4e8e\u4fdd\u8bc1\u9879\u76ee\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u9760\u6027\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<p><strong>2. Java\u9879\u76ee\u4e2d\u7684\u4e8b\u52a1\u7ba1\u7406\u65b9\u5f0f\u6709\u54ea\u4e9b\uff1f<\/strong><\/p>\n<p>Java\u9879\u76ee\u4e2d\u5e38\u7528\u7684\u4e8b\u52a1\u7ba1\u7406\u65b9\u5f0f\u6709\u4e24\u79cd\uff1a\u7f16\u7a0b\u5f0f\u4e8b\u52a1\u7ba1\u7406\u548c\u58f0\u660e\u5f0f\u4e8b\u52a1\u7ba1\u7406\u3002<\/p>\n<p>\u7f16\u7a0b\u5f0f\u4e8b\u52a1\u7ba1\u7406\u662f\u901a\u8fc7\u5728\u4ee3\u7801\u4e2d\u624b\u52a8\u7f16\u5199\u4e8b\u52a1\u7ba1\u7406\u903b\u8f91\uff0c\u5305\u62ec\u5f00\u59cb\u4e8b\u52a1\u3001\u63d0\u4ea4\u4e8b\u52a1\u548c\u56de\u6eda\u4e8b\u52a1\u7b49\u64cd\u4f5c\u3002\u8fd9\u79cd\u65b9\u5f0f\u7075\u6d3b\u6027\u8f83\u9ad8\uff0c\u9002\u5408\u5bf9\u4e8b\u52a1\u63a7\u5236\u8981\u6c42\u8f83\u4e3a\u590d\u6742\u7684\u573a\u666f\u3002<\/p>\n<p>\u58f0\u660e\u5f0f\u4e8b\u52a1\u7ba1\u7406\u662f\u901a\u8fc7\u5728\u914d\u7f6e\u6587\u4ef6\u6216\u6ce8\u89e3\u4e2d\u58f0\u660e\u4e8b\u52a1\u7684\u5c5e\u6027\u548c\u884c\u4e3a\uff0c\u7531\u6846\u67b6\u81ea\u52a8\u7ba1\u7406\u4e8b\u52a1\u3002\u8fd9\u79cd\u65b9\u5f0f\u7b80\u5316\u4e86\u4ee3\u7801\u7684\u7f16\u5199\u548c\u7ef4\u62a4\uff0c\u63d0\u9ad8\u4e86\u5f00\u53d1\u6548\u7387\u3002<\/p>\n<p><strong>3. \u5728Java\u9879\u76ee\u4e2d\u5982\u4f55\u9009\u62e9\u9002\u5408\u7684\u4e8b\u52a1\u7ba1\u7406\u65b9\u5f0f\uff1f<\/strong><\/p>\n<p>\u9009\u62e9\u9002\u5408\u7684\u4e8b\u52a1\u7ba1\u7406\u65b9\u5f0f\u9700\u8981\u8003\u8651\u9879\u76ee\u7684\u89c4\u6a21\u3001\u590d\u6742\u5ea6\u548c\u6027\u80fd\u9700\u6c42\u7b49\u56e0\u7d20\u3002<\/p>\n<p>\u5982\u679c\u9879\u76ee\u89c4\u6a21\u8f83\u5c0f\uff0c\u4e8b\u52a1\u63a7\u5236\u8f83\u7b80\u5355\uff0c\u53ef\u4ee5\u9009\u62e9\u58f0\u660e\u5f0f\u4e8b\u52a1\u7ba1\u7406\uff0c\u901a\u8fc7\u914d\u7f6e\u6587\u4ef6\u6216\u6ce8\u89e3\u6765\u7ba1\u7406\u4e8b\u52a1\uff0c\u51cf\u5c11\u4ee3\u7801\u7684\u5197\u4f59\u3002<\/p>\n<p>\u5982\u679c\u9879\u76ee\u89c4\u6a21\u8f83\u5927\uff0c\u4e8b\u52a1\u63a7\u5236\u8f83\u4e3a\u590d\u6742\uff0c\u9700\u8981\u7075\u6d3b\u63a7\u5236\u4e8b\u52a1\u7684\u5404\u4e2a\u73af\u8282\uff0c\u53ef\u4ee5\u9009\u62e9\u7f16\u7a0b\u5f0f\u4e8b\u52a1\u7ba1\u7406\uff0c\u901a\u8fc7\u4ee3\u7801\u6765\u624b\u52a8\u63a7\u5236\u4e8b\u52a1\u7684\u5f00\u59cb\u3001\u63d0\u4ea4\u548c\u56de\u6eda\u7b49\u64cd\u4f5c\u3002<\/p>\n<p>\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528\u4e24\u79cd\u65b9\u5f0f\uff0c\u6839\u636e\u5177\u4f53\u7684\u4e1a\u52a1\u573a\u666f\u548c\u9700\u6c42\u6765\u9009\u62e9\u6700\u5408\u9002\u7684\u4e8b\u52a1\u7ba1\u7406\u65b9\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Java\u9879\u76ee\u4e2d\uff0c\u4e8b\u52a1\u7ba1\u7406\u7684\u6838\u5fc3\u5728\u4e8e\u786e\u4fdd\u6570\u636e\u7684\u4e00\u81f4\u6027\u3001\u539f\u5b50\u6027\u3001\u9694\u79bb\u6027\u548c\u6301\u4e45\u6027\uff08ACID\u539f\u5219\uff09\u3002\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Sp [&hellip;]","protected":false},"author":3,"featured_media":328582,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/328564"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=328564"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/328564\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/328582"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=328564"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=328564"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=328564"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}