{"id":935,"date":"2018-08-10T14:20:07","date_gmt":"2018-08-10T06:20:07","guid":{"rendered":"https:\/\/blog.jsjs.org\/?p=935"},"modified":"2018-08-10T14:20:07","modified_gmt":"2018-08-10T06:20:07","slug":"spring%e4%ba%8b%e5%8a%a1%e7%ae%a1%e7%90%86%ef%bc%88%e8%af%a6%e8%a7%a3%e5%ae%9e%e4%be%8b%ef%bc%89","status":"publish","type":"post","link":"https:\/\/blog.jsjs.org\/?p=935","title":{"rendered":"Spring\u4e8b\u52a1\u7ba1\u7406\uff08\u8be6\u89e3+\u5b9e\u4f8b\uff09"},"content":{"rendered":"\n<p>\u5199\u8fd9\u7bc7\u535a\u5ba2\u4e4b\u524d\u6211\u9996\u5148\u8bfb\u4e86\u300aSpring in action\u300b\uff0c\u4e4b\u540e\u5728\u7f51\u4e0a\u770b\u4e86\u4e00\u4e9b\u5173\u4e8eSpring\u4e8b\u52a1\u7ba1\u7406\u7684\u6587\u7ae0\uff0c\u611f\u89c9\u90fd\u6ca1\u6709\u8bb2\u5168\uff0c\u8fd9\u91cc\u5c31\u5c06\u4e66\u4e0a\u7684\u548c\u7f51\u4e0a\u5173\u4e8e\u4e8b\u52a1\u7684\u77e5\u8bc6\u603b\u7ed3\u4e00\u4e0b\uff0c\u53c2\u8003\u7684\u6587\u7ae0\u5982\u4e0b\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><a href=\"http:\/\/www.open-open.com\/lib\/view\/open1350865116821.html\" target=\"_blank\" rel=\"noreferrer noopener\">Spring\u4e8b\u52a1\u673a\u5236\u8be6\u89e3<\/a><br\/><a href=\"http:\/\/www.blogjava.net\/robbie\/archive\/2009\/04\/05\/264003.html\" target=\"_blank\" rel=\"noreferrer noopener\">Spring\u4e8b\u52a1\u914d\u7f6e\u7684\u4e94\u79cd\u65b9\u5f0f<\/a><br\/><a href=\"http:\/\/www.jb51.net\/article\/57589.htm\" target=\"_blank\" rel=\"noreferrer noopener\">Spring\u4e2d\u7684\u4e8b\u52a1\u7ba1\u7406\u5b9e\u4f8b\u8be6\u89e3<\/a><\/p><\/blockquote>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"1-\u521d\u6b65\u7406\u89e3\">1 \u521d\u6b65\u7406\u89e3<\/h1>\n\n\n\n<p>\u7406\u89e3\u4e8b\u52a1\u4e4b\u524d\uff0c\u5148\u8bb2\u4e00\u4e2a\u4f60\u65e5\u5e38\u751f\u6d3b\u4e2d\u6700\u5e38\u5e72\u7684\u4e8b\uff1a\u53d6\u94b1\u3002\u00a0<br\/>\u6bd4\u5982\u4f60\u53bbATM\u673a\u53d61000\u5757\u94b1\uff0c\u5927\u4f53\u6709\u4e24\u4e2a\u6b65\u9aa4\uff1a\u9996\u5148\u8f93\u5165\u5bc6\u7801\u91d1\u989d\uff0c\u94f6\u884c\u5361\u6263\u63891000\u5143\u94b1\uff1b\u7136\u540eATM\u51fa1000\u5143\u94b1\u3002\u8fd9\u4e24\u4e2a\u6b65\u9aa4\u5fc5\u987b\u662f\u8981\u4e48\u90fd\u6267\u884c\u8981\u4e48\u90fd\u4e0d\u6267\u884c\u3002\u5982\u679c\u94f6\u884c\u5361\u6263\u9664\u4e861000\u5757\u4f46\u662fATM\u51fa\u94b1\u5931\u8d25\u7684\u8bdd\uff0c\u4f60\u5c06\u4f1a\u635f\u59311000\u5143\uff1b\u5982\u679c\u94f6\u884c\u5361\u6263\u94b1\u5931\u8d25\u4f46\u662fATM\u5374\u51fa\u4e861000\u5757\uff0c\u90a3\u4e48\u94f6\u884c\u5c06\u635f\u59311000\u5143\u3002\u6240\u4ee5\uff0c\u5982\u679c\u4e00\u4e2a\u6b65\u9aa4\u6210\u529f\u53e6\u4e00\u4e2a\u6b65\u9aa4\u5931\u8d25\u5bf9\u53cc\u65b9\u90fd\u4e0d\u662f\u597d\u4e8b\uff0c\u5982\u679c\u4e0d\u7ba1\u54ea\u4e00\u4e2a\u6b65\u9aa4\u5931\u8d25\u4e86\u4ee5\u540e\uff0c\u6574\u4e2a\u53d6\u94b1\u8fc7\u7a0b\u90fd\u80fd\u56de\u6eda\uff0c\u4e5f\u5c31\u662f\u5b8c\u5168\u53d6\u6d88\u6240\u6709\u64cd\u4f5c\u7684\u8bdd\uff0c\u8fd9\u5bf9\u53cc\u65b9\u90fd\u662f\u6781\u597d\u7684\u3002\u00a0<br\/>\u4e8b\u52a1\u5c31\u662f\u7528\u6765\u89e3\u51b3\u7c7b\u4f3c\u95ee\u9898\u7684\u3002\u4e8b\u52a1\u662f\u4e00\u7cfb\u5217\u7684\u52a8\u4f5c\uff0c\u5b83\u4eec\u7efc\u5408\u5728\u4e00\u8d77\u624d\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u5de5\u4f5c\u5355\u5143\uff0c\u8fd9\u4e9b\u52a8\u4f5c\u5fc5\u987b\u5168\u90e8\u5b8c\u6210\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u5931\u8d25\u7684\u8bdd\uff0c\u90a3\u4e48\u4e8b\u52a1\u5c31\u4f1a\u56de\u6eda\u5230\u6700\u5f00\u59cb\u7684\u72b6\u6001\uff0c\u4eff\u4f5b\u4ec0\u4e48\u90fd\u6ca1\u53d1\u751f\u8fc7\u4e00\u6837\u3002\u00a0<br\/>\u5728\u4f01\u4e1a\u7ea7\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u4e2d\uff0c\u4e8b\u52a1\u7ba1\u7406\u5fc5\u4e0d\u53ef\u5c11\u7684\u6280\u672f\uff0c\u7528\u6765\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\u3002\u00a0<br\/>\u4e8b\u52a1\u6709\u56db\u4e2a\u7279\u6027\uff1aACID<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u539f\u5b50\u6027\uff08Atomicity\uff09\uff1a\u4e8b\u52a1\u662f\u4e00\u4e2a\u539f\u5b50\u64cd\u4f5c\uff0c\u7531\u4e00\u7cfb\u5217\u52a8\u4f5c\u7ec4\u6210\u3002\u4e8b\u52a1\u7684\u539f\u5b50\u6027\u786e\u4fdd\u52a8\u4f5c\u8981\u4e48\u5168\u90e8\u5b8c\u6210\uff0c\u8981\u4e48\u5b8c\u5168\u4e0d\u8d77\u4f5c\u7528\u3002<br\/>\u4e00\u81f4\u6027\uff08Consistency\uff09\uff1a\u4e00\u65e6\u4e8b\u52a1\u5b8c\u6210\uff08\u4e0d\u7ba1\u6210\u529f\u8fd8\u662f\u5931\u8d25\uff09\uff0c\u7cfb\u7edf\u5fc5\u987b\u786e\u4fdd\u5b83\u6240\u5efa\u6a21\u7684\u4e1a\u52a1\u5904\u4e8e\u4e00\u81f4\u7684\u72b6\u6001\uff0c\u800c\u4e0d\u4f1a\u662f\u90e8\u5206\u5b8c\u6210\u90e8\u5206\u5931\u8d25\u3002\u5728\u73b0\u5b9e\u4e2d\u7684\u6570\u636e\u4e0d\u5e94\u8be5\u88ab\u7834\u574f\u3002<br\/>\u9694\u79bb\u6027\uff08Isolation\uff09\uff1a\u53ef\u80fd\u6709\u8bb8\u591a\u4e8b\u52a1\u4f1a\u540c\u65f6\u5904\u7406\u76f8\u540c\u7684\u6570\u636e\uff0c\u56e0\u6b64\u6bcf\u4e2a\u4e8b\u52a1\u90fd\u5e94\u8be5\u4e0e\u5176\u4ed6\u4e8b\u52a1\u9694\u79bb\u5f00\u6765\uff0c\u9632\u6b62\u6570\u636e\u635f\u574f\u3002<br\/>\u6301\u4e45\u6027\uff08Durability\uff09\uff1a\u4e00\u65e6\u4e8b\u52a1\u5b8c\u6210\uff0c\u65e0\u8bba\u53d1\u751f\u4ec0\u4e48\u7cfb\u7edf\u9519\u8bef\uff0c\u5b83\u7684\u7ed3\u679c\u90fd\u4e0d\u5e94\u8be5\u53d7\u5230\u5f71\u54cd\uff0c\u8fd9\u6837\u5c31\u80fd\u4ece\u4efb\u4f55\u7cfb\u7edf\u5d29\u6e83\u4e2d\u6062\u590d\u8fc7\u6765\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u4e8b\u52a1\u7684\u7ed3\u679c\u88ab\u5199\u5230\u6301\u4e45\u5316\u5b58\u50a8\u5668\u4e2d\u3002<\/p><\/blockquote>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"2-\u6838\u5fc3\u63a5\u53e3\">2 \u6838\u5fc3\u63a5\u53e3<\/h1>\n\n\n\n<p>Spring\u4e8b\u52a1\u7ba1\u7406\u7684\u5b9e\u73b0\u6709\u8bb8\u591a\u7ec6\u8282\uff0c\u5982\u679c\u5bf9\u6574\u4e2a\u63a5\u53e3\u6846\u67b6\u6709\u4e2a\u5927\u4f53\u4e86\u89e3\u4f1a\u975e\u5e38\u6709\u5229\u4e8e\u6211\u4eec\u7406\u89e3\u4e8b\u52a1\uff0c\u4e0b\u9762\u901a\u8fc7\u8bb2\u89e3Spring\u7684\u4e8b\u52a1\u63a5\u53e3\u6765\u4e86\u89e3Spring\u5b9e\u73b0\u4e8b\u52a1\u7684\u5177\u4f53\u7b56\u7565\u3002\u00a0<br\/>Spring\u4e8b\u52a1\u7ba1\u7406\u6d89\u53ca\u7684\u63a5\u53e3\u7684\u8054\u7cfb\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/blog.jsjs.org\/wp-content\/uploads\/2018\/08\/20160324011156424.png\" alt=\"\" class=\"wp-image-939\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"21-\u4e8b\u52a1\u7ba1\u7406\u5668\">2.1 \u4e8b\u52a1\u7ba1\u7406\u5668<\/h2>\n\n\n\n<p>Spring\u5e76\u4e0d\u76f4\u63a5\u7ba1\u7406\u4e8b\u52a1\uff0c\u800c\u662f\u63d0\u4f9b\u4e86\u591a\u79cd\u4e8b\u52a1\u7ba1\u7406\u5668\uff0c\u4ed6\u4eec\u5c06\u4e8b\u52a1\u7ba1\u7406\u7684\u804c\u8d23\u59d4\u6258\u7ed9Hibernate\u6216\u8005JTA\u7b49\u6301\u4e45\u5316\u673a\u5236\u6240\u63d0\u4f9b\u7684\u76f8\u5173\u5e73\u53f0\u6846\u67b6\u7684\u4e8b\u52a1\u6765\u5b9e\u73b0\u3002\u00a0<br\/>Spring\u4e8b\u52a1\u7ba1\u7406\u5668\u7684\u63a5\u53e3\u662forg.springframework.transaction.PlatformTransactionManager\uff0c\u901a\u8fc7\u8fd9\u4e2a\u63a5\u53e3\uff0cSpring\u4e3a\u5404\u4e2a\u5e73\u53f0\u5982JDBC\u3001Hibernate\u7b49\u90fd\u63d0\u4f9b\u4e86\u5bf9\u5e94\u7684\u4e8b\u52a1\u7ba1\u7406\u5668\uff0c\u4f46\u662f\u5177\u4f53\u7684\u5b9e\u73b0\u5c31\u662f\u5404\u4e2a\u5e73\u53f0\u81ea\u5df1\u7684\u4e8b\u60c5\u4e86\u3002\u6b64\u63a5\u53e3\u7684\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Public interface PlatformTransactionManager()...{\n    \/\/ \u7531TransactionDefinition\u5f97\u5230TransactionStatus\u5bf9\u8c61\n    TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException;\n    \/\/ \u63d0\u4ea4\n    Void commit(TransactionStatus status) throws TransactionException;\n    \/\/ \u56de\u6eda\n    Void rollback(TransactionStatus status) throws TransactionException;\n    } <\/code><\/pre>\n\n\n\n<p>\u4ece\u8fd9\u91cc\u53ef\u77e5\u5177\u4f53\u7684\u5177\u4f53\u7684\u4e8b\u52a1\u7ba1\u7406\u673a\u5236\u5bf9Spring\u6765\u8bf4\u662f\u900f\u660e\u7684\uff0c\u5b83\u5e76\u4e0d\u5173\u5fc3\u90a3\u4e9b\uff0c\u90a3\u4e9b\u662f\u5bf9\u5e94\u5404\u4e2a\u5e73\u53f0\u9700\u8981\u5173\u5fc3\u7684\uff0c\u6240\u4ee5Spring\u4e8b\u52a1\u7ba1\u7406\u7684\u4e00\u4e2a\u4f18\u70b9\u5c31\u662f\u4e3a\u4e0d\u540c\u7684\u4e8b\u52a1API\u63d0\u4f9b\u4e00\u81f4\u7684\u7f16\u7a0b\u6a21\u578b\uff0c\u5982JTA\u3001JDBC\u3001Hibernate\u3001JPA\u3002\u4e0b\u9762\u5206\u522b\u4ecb\u7ecd\u5404\u4e2a\u5e73\u53f0\u6846\u67b6\u5b9e\u73b0\u4e8b\u52a1\u7ba1\u7406\u7684\u673a\u5236\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"211-jdbc\u4e8b\u52a1\">2.1.1 JDBC\u4e8b\u52a1<\/h3>\n\n\n\n<p>\u5982\u679c\u5e94\u7528\u7a0b\u5e8f\u4e2d\u76f4\u63a5\u4f7f\u7528JDBC\u6765\u8fdb\u884c\u6301\u4e45\u5316\uff0cDataSourceTransactionManager\u4f1a\u4e3a\u4f60\u5904\u7406\u4e8b\u52a1\u8fb9\u754c\u3002\u4e3a\u4e86\u4f7f\u7528DataSourceTransactionManager\uff0c\u4f60\u9700\u8981\u4f7f\u7528\u5982\u4e0b\u7684XML\u5c06\u5176\u88c5\u914d\u5230\u5e94\u7528\u7a0b\u5e8f\u7684\u4e0a\u4e0b\u6587\u5b9a\u4e49\u4e2d\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    &lt;bean id=\"transactionManager\" class=\"org.springframework.jdbc.datasource.DataSourceTransactionManager\">\n        &lt;property name=\"dataSource\" ref=\"dataSource\" \/>\n    &lt;\/bean><\/code><\/pre>\n\n\n\n<p>\u5b9e\u9645\u4e0a\uff0cDataSourceTransactionManager\u662f\u901a\u8fc7\u8c03\u7528java.sql.Connection\u6765\u7ba1\u7406\u4e8b\u52a1\uff0c\u800c\u540e\u8005\u662f\u901a\u8fc7DataSource\u83b7\u53d6\u5230\u7684\u3002\u901a\u8fc7\u8c03\u7528\u8fde\u63a5\u7684commit()\u65b9\u6cd5\u6765\u63d0\u4ea4\u4e8b\u52a1\uff0c\u540c\u6837\uff0c\u4e8b\u52a1\u5931\u8d25\u5219\u901a\u8fc7\u8c03\u7528rollback()\u65b9\u6cd5\u8fdb\u884c\u56de\u6eda\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"212-hibernate\u4e8b\u52a1\">2.1.2 Hibernate\u4e8b\u52a1<\/h3>\n\n\n\n<p>\u5982\u679c\u5e94\u7528\u7a0b\u5e8f\u7684\u6301\u4e45\u5316\u662f\u901a\u8fc7Hibernate\u5b9e\u4e60\u7684\uff0c\u90a3\u4e48\u4f60\u9700\u8981\u4f7f\u7528HibernateTransactionManager\u3002\u5bf9\u4e8eHibernate3\uff0c\u9700\u8981\u5728Spring\u4e0a\u4e0b\u6587\u5b9a\u4e49\u4e2d\u6dfb\u52a0\u5982\u4e0b\u7684<code>&lt;bean><\/code>\u58f0\u660e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    &lt;bean id=\"transactionManager\" class=\"org.springframework.orm.hibernate3.HibernateTransactionManager\">\n        &lt;property name=\"sessionFactory\" ref=\"sessionFactory\" \/>\n    &lt;\/bean><\/code><\/pre>\n\n\n\n<p>sessionFactory\u5c5e\u6027\u9700\u8981\u88c5\u914d\u4e00\u4e2aHibernate\u7684session\u5de5\u5382\uff0cHibernateTransactionManager\u7684\u5b9e\u73b0\u7ec6\u8282\u662f\u5b83\u5c06\u4e8b\u52a1\u7ba1\u7406\u7684\u804c\u8d23\u59d4\u6258\u7ed9org.hibernate.Transaction\u5bf9\u8c61\uff0c\u800c\u540e\u8005\u662f\u4eceHibernate Session\u4e2d\u83b7\u53d6\u5230\u7684\u3002\u5f53\u4e8b\u52a1\u6210\u529f\u5b8c\u6210\u65f6\uff0cHibernateTransactionManager\u5c06\u4f1a\u8c03\u7528Transaction\u5bf9\u8c61\u7684commit()\u65b9\u6cd5\uff0c\u53cd\u4e4b\uff0c\u5c06\u4f1a\u8c03\u7528rollback()\u65b9\u6cd5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"213-java\u6301\u4e45\u5316api\u4e8b\u52a1jpa\">2.1.3 Java\u6301\u4e45\u5316API\u4e8b\u52a1\uff08JPA\uff09<\/h3>\n\n\n\n<p>Hibernate\u591a\u5e74\u6765\u4e00\u76f4\u662f\u4e8b\u5b9e\u4e0a\u7684Java\u6301\u4e45\u5316\u6807\u51c6\uff0c\u4f46\u662f\u73b0\u5728Java\u6301\u4e45\u5316API\u4f5c\u4e3a\u771f\u6b63\u7684Java\u6301\u4e45\u5316\u6807\u51c6\u8fdb\u5165\u5927\u5bb6\u7684\u89c6\u91ce\u3002\u5982\u679c\u4f60\u8ba1\u5212\u4f7f\u7528JPA\u7684\u8bdd\uff0c\u90a3\u4f60\u9700\u8981\u4f7f\u7528Spring\u7684JpaTransactionManager\u6765\u5904\u7406\u4e8b\u52a1\u3002\u4f60\u9700\u8981\u5728Spring\u4e2d\u8fd9\u6837\u914d\u7f6eJpaTransactionManager\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    &lt;bean id=\"transactionManager\" class=\"org.springframework.orm.jpa.JpaTransactionManager\">\n        &lt;property name=\"sessionFactory\" ref=\"sessionFactory\" \/>\n    &lt;\/bean><\/code><\/pre>\n\n\n\n<p>JpaTransactionManager\u53ea\u9700\u8981\u88c5\u914d\u4e00\u4e2aJPA\u5b9e\u4f53\u7ba1\u7406\u5de5\u5382\uff08javax.persistence.EntityManagerFactory\u63a5\u53e3\u7684\u4efb\u610f\u5b9e\u73b0\uff09\u3002JpaTransactionManager\u5c06\u4e0e\u7531\u5de5\u5382\u6240\u4ea7\u751f\u7684JPA EntityManager\u5408\u4f5c\u6765\u6784\u5efa\u4e8b\u52a1\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"214-java\u539f\u751fapi\u4e8b\u52a1\">2.1.4 Java\u539f\u751fAPI\u4e8b\u52a1<\/h3>\n\n\n\n<p>\u5982\u679c\u4f60\u6ca1\u6709\u4f7f\u7528\u4ee5\u4e0a\u6240\u8ff0\u7684\u4e8b\u52a1\u7ba1\u7406\uff0c\u6216\u8005\u662f\u8de8\u8d8a\u4e86\u591a\u4e2a\u4e8b\u52a1\u7ba1\u7406\u6e90\uff08\u6bd4\u5982\u4e24\u4e2a\u6216\u8005\u662f\u591a\u4e2a\u4e0d\u540c\u7684\u6570\u636e\u6e90\uff09\uff0c\u4f60\u5c31\u9700\u8981\u4f7f\u7528JtaTransactionManager\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    &lt;bean id=\"transactionManager\" class=\"org.springframework.transaction.jta.JtaTransactionManager\">\n        &lt;property name=\"transactionManagerName\" value=\"java:\/TransactionManager\" \/>\n    &lt;\/bean><\/code><\/pre>\n\n\n\n<p>JtaTransactionManager\u5c06\u4e8b\u52a1\u7ba1\u7406\u7684\u8d23\u4efb\u59d4\u6258\u7ed9javax.transaction.UserTransaction\u548cjavax.transaction.TransactionManager\u5bf9\u8c61\uff0c\u5176\u4e2d\u4e8b\u52a1\u6210\u529f\u5b8c\u6210\u901a\u8fc7UserTransaction.commit()\u65b9\u6cd5\u63d0\u4ea4\uff0c\u4e8b\u52a1\u5931\u8d25\u901a\u8fc7UserTransaction.rollback()\u65b9\u6cd5\u56de\u6eda\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"22-\u57fa\u672c\u4e8b\u52a1\u5c5e\u6027\u7684\u5b9a\u4e49\">2.2 \u57fa\u672c\u4e8b\u52a1\u5c5e\u6027\u7684\u5b9a\u4e49<\/h2>\n\n\n\n<p>\u4e0a\u9762\u8bb2\u5230\u7684\u4e8b\u52a1\u7ba1\u7406\u5668\u63a5\u53e3PlatformTransactionManager\u901a\u8fc7getTransaction(TransactionDefinition definition)\u65b9\u6cd5\u6765\u5f97\u5230\u4e8b\u52a1\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u91cc\u9762\u7684\u53c2\u6570\u662fTransactionDefinition\u7c7b\uff0c\u8fd9\u4e2a\u7c7b\u5c31\u5b9a\u4e49\u4e86\u4e00\u4e9b\u57fa\u672c\u7684\u4e8b\u52a1\u5c5e\u6027\u3002\u00a0<br\/>\u90a3\u4e48\u4ec0\u4e48\u662f\u4e8b\u52a1\u5c5e\u6027\u5462\uff1f\u4e8b\u52a1\u5c5e\u6027\u53ef\u4ee5\u7406\u89e3\u6210\u4e8b\u52a1\u7684\u4e00\u4e9b\u57fa\u672c\u914d\u7f6e\uff0c\u63cf\u8ff0\u4e86\u4e8b\u52a1\u7b56\u7565\u5982\u4f55\u5e94\u7528\u5230\u65b9\u6cd5\u4e0a\u3002\u4e8b\u52a1\u5c5e\u6027\u5305\u542b\u4e865\u4e2a\u65b9\u9762\uff0c\u5982\u56fe\u6240\u793a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/blog.jsjs.org\/wp-content\/uploads\/2018\/08\/20160325003448793.png\" alt=\"\" class=\"wp-image-940\"\/><\/figure>\n\n\n\n<p>\u800cTransactionDefinition\u63a5\u53e3\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public interface TransactionDefinition {\n    int getPropagationBehavior(); \/\/ \u8fd4\u56de\u4e8b\u52a1\u7684\u4f20\u64ad\u884c\u4e3a\n    int getIsolationLevel(); \/\/ \u8fd4\u56de\u4e8b\u52a1\u7684\u9694\u79bb\u7ea7\u522b\uff0c\u4e8b\u52a1\u7ba1\u7406\u5668\u6839\u636e\u5b83\u6765\u63a7\u5236\u53e6\u5916\u4e00\u4e2a\u4e8b\u52a1\u53ef\u4ee5\u770b\u5230\u672c\u4e8b\u52a1\u5185\u7684\u54ea\u4e9b\u6570\u636e\n    int getTimeout();  \/\/ \u8fd4\u56de\u4e8b\u52a1\u5fc5\u987b\u5728\u591a\u5c11\u79d2\u5185\u5b8c\u6210\n    boolean isReadOnly(); \/\/ \u4e8b\u52a1\u662f\u5426\u53ea\u8bfb\uff0c\u4e8b\u52a1\u7ba1\u7406\u5668\u80fd\u591f\u6839\u636e\u8fd9\u4e2a\u8fd4\u56de\u503c\u8fdb\u884c\u4f18\u5316\uff0c\u786e\u4fdd\u4e8b\u52a1\u662f\u53ea\u8bfb\u7684\n} <\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u53d1\u73b0TransactionDefinition\u6b63\u597d\u7528\u6765\u5b9a\u4e49\u4e8b\u52a1\u5c5e\u6027\uff0c\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u4e00\u4e0b\u5404\u4e2a\u4e8b\u52a1\u5c5e\u6027\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"221-\u4f20\u64ad\u884c\u4e3a\">2.2.1 \u4f20\u64ad\u884c\u4e3a<\/h3>\n\n\n\n<p>\u4e8b\u52a1\u7684\u7b2c\u4e00\u4e2a\u65b9\u9762\u662f\u4f20\u64ad\u884c\u4e3a\uff08propagation behavior\uff09\u3002\u5f53\u4e8b\u52a1\u65b9\u6cd5\u88ab\u53e6\u4e00\u4e2a\u4e8b\u52a1\u65b9\u6cd5\u8c03\u7528\u65f6\uff0c\u5fc5\u987b\u6307\u5b9a\u4e8b\u52a1\u5e94\u8be5\u5982\u4f55\u4f20\u64ad\u3002\u4f8b\u5982\uff1a\u65b9\u6cd5\u53ef\u80fd\u7ee7\u7eed\u5728\u73b0\u6709\u4e8b\u52a1\u4e2d\u8fd0\u884c\uff0c\u4e5f\u53ef\u80fd\u5f00\u542f\u4e00\u4e2a\u65b0\u4e8b\u52a1\uff0c\u5e76\u5728\u81ea\u5df1\u7684\u4e8b\u52a1\u4e2d\u8fd0\u884c\u3002Spring\u5b9a\u4e49\u4e86\u4e03\u79cd\u4f20\u64ad\u884c\u4e3a\uff1a<\/p>\n\n\n\n<table class=\"wp-block-table\"><thead><tr><th>\u4f20\u64ad\u884c\u4e3a<\/th><th>\u542b\u4e49<\/th><\/tr><\/thead><tbody><tr><td>PROPAGATION_REQUIRED<\/td><td>\u8868\u793a\u5f53\u524d\u65b9\u6cd5\u5fc5\u987b\u8fd0\u884c\u5728\u4e8b\u52a1\u4e2d\u3002\u5982\u679c\u5f53\u524d\u4e8b\u52a1\u5b58\u5728\uff0c\u65b9\u6cd5\u5c06\u4f1a\u5728\u8be5\u4e8b\u52a1\u4e2d\u8fd0\u884c\u3002\u5426\u5219\uff0c\u4f1a\u542f\u52a8\u4e00\u4e2a\u65b0\u7684\u4e8b\u52a1<\/td><\/tr><tr><td>PROPAGATION_SUPPORTS<\/td><td>\u8868\u793a\u5f53\u524d\u65b9\u6cd5\u4e0d\u9700\u8981\u4e8b\u52a1\u4e0a\u4e0b\u6587\uff0c\u4f46\u662f\u5982\u679c\u5b58\u5728\u5f53\u524d\u4e8b\u52a1\u7684\u8bdd\uff0c\u90a3\u4e48\u8be5\u65b9\u6cd5\u4f1a\u5728\u8fd9\u4e2a\u4e8b\u52a1\u4e2d\u8fd0\u884c<\/td><\/tr><tr><td>PROPAGATION_MANDATORY<\/td><td>\u8868\u793a\u8be5\u65b9\u6cd5\u5fc5\u987b\u5728\u4e8b\u52a1\u4e2d\u8fd0\u884c\uff0c\u5982\u679c\u5f53\u524d\u4e8b\u52a1\u4e0d\u5b58\u5728\uff0c\u5219\u4f1a\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38<\/td><\/tr><tr><td>PROPAGATION_REQUIRED_NEW<\/td><td>\u8868\u793a\u5f53\u524d\u65b9\u6cd5\u5fc5\u987b\u8fd0\u884c\u5728\u5b83\u81ea\u5df1\u7684\u4e8b\u52a1\u4e2d\u3002\u4e00\u4e2a\u65b0\u7684\u4e8b\u52a1\u5c06\u88ab\u542f\u52a8\u3002\u5982\u679c\u5b58\u5728\u5f53\u524d\u4e8b\u52a1\uff0c\u5728\u8be5\u65b9\u6cd5\u6267\u884c\u671f\u95f4\uff0c\u5f53\u524d\u4e8b\u52a1\u4f1a\u88ab\u6302\u8d77\u3002\u5982\u679c\u4f7f\u7528JTATransactionManager\u7684\u8bdd\uff0c\u5219\u9700\u8981\u8bbf\u95eeTransactionManager<\/td><\/tr><tr><td>PROPAGATION_NOT_SUPPORTED<\/td><td>\u8868\u793a\u8be5\u65b9\u6cd5\u4e0d\u5e94\u8be5\u8fd0\u884c\u5728\u4e8b\u52a1\u4e2d\u3002\u5982\u679c\u5b58\u5728\u5f53\u524d\u4e8b\u52a1\uff0c\u5728\u8be5\u65b9\u6cd5\u8fd0\u884c\u671f\u95f4\uff0c\u5f53\u524d\u4e8b\u52a1\u5c06\u88ab\u6302\u8d77\u3002\u5982\u679c\u4f7f\u7528JTATransactionManager\u7684\u8bdd\uff0c\u5219\u9700\u8981\u8bbf\u95eeTransactionManager<\/td><\/tr><tr><td>PROPAGATION_NEVER<\/td><td>\u8868\u793a\u5f53\u524d\u65b9\u6cd5\u4e0d\u5e94\u8be5\u8fd0\u884c\u5728\u4e8b\u52a1\u4e0a\u4e0b\u6587\u4e2d\u3002\u5982\u679c\u5f53\u524d\u6b63\u6709\u4e00\u4e2a\u4e8b\u52a1\u5728\u8fd0\u884c\uff0c\u5219\u4f1a\u629b\u51fa\u5f02\u5e38<\/td><\/tr><tr><td>PROPAGATION_NESTED<\/td><td>\u8868\u793a\u5982\u679c\u5f53\u524d\u5df2\u7ecf\u5b58\u5728\u4e00\u4e2a\u4e8b\u52a1\uff0c\u90a3\u4e48\u8be5\u65b9\u6cd5\u5c06\u4f1a\u5728\u5d4c\u5957\u4e8b\u52a1\u4e2d\u8fd0\u884c\u3002\u5d4c\u5957\u7684\u4e8b\u52a1\u53ef\u4ee5\u72ec\u7acb\u4e8e\u5f53\u524d\u4e8b\u52a1\u8fdb\u884c\u5355\u72ec\u5730\u63d0\u4ea4\u6216\u56de\u6eda\u3002\u5982\u679c\u5f53\u524d\u4e8b\u52a1\u4e0d\u5b58\u5728\uff0c\u90a3\u4e48\u5176\u884c\u4e3a\u4e0ePROPAGATION_REQUIRED\u4e00\u6837\u3002\u6ce8\u610f\u5404\u5382\u5546\u5bf9\u8fd9\u79cd\u4f20\u64ad\u884c\u4e3a\u7684\u652f\u6301\u662f\u6709\u6240\u5dee\u5f02\u7684\u3002\u53ef\u4ee5\u53c2\u8003\u8d44\u6e90\u7ba1\u7406\u5668\u7684\u6587\u6863\u6765\u786e\u8ba4\u5b83\u4eec\u662f\u5426\u652f\u6301\u5d4c\u5957\u4e8b\u52a1<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p><br\/><em>\u6ce8\uff1a\u4ee5\u4e0b\u5177\u4f53\u8bb2\u89e3\u4f20\u64ad\u884c\u4e3a\u7684\u5185\u5bb9\u53c2\u8003\u81ea<a href=\"http:\/\/www.open-open.com\/lib\/view\/open1350865116821.html\" rel=\"noreferrer noopener\" target=\"_blank\">Spring\u4e8b\u52a1\u673a\u5236\u8be6\u89e3<\/a><\/em>\u00a0<br\/>\uff081\uff09PROPAGATION_REQUIRED \u5982\u679c\u5b58\u5728\u4e00\u4e2a\u4e8b\u52a1\uff0c\u5219\u652f\u6301\u5f53\u524d\u4e8b\u52a1\u3002\u5982\u679c\u6ca1\u6709\u4e8b\u52a1\u5219\u5f00\u542f\u4e00\u4e2a\u65b0\u7684\u4e8b\u52a1\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u4e8b\u52a1\u5c5e\u6027 PROPAGATION_REQUIRED\nmethodA{\n    \u2026\u2026\n    methodB();\n    \u2026\u2026\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u4e8b\u52a1\u5c5e\u6027 PROPAGATION_REQUIRED\nmethodB{\n   \u2026\u2026\n}<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528spring\u58f0\u660e\u5f0f\u4e8b\u52a1\uff0cspring\u4f7f\u7528AOP\u6765\u652f\u6301\u58f0\u660e\u5f0f\u4e8b\u52a1\uff0c\u4f1a\u6839\u636e\u4e8b\u52a1\u5c5e\u6027\uff0c\u81ea\u52a8\u5728\u65b9\u6cd5\u8c03\u7528\u4e4b\u524d\u51b3\u5b9a\u662f\u5426\u5f00\u542f\u4e00\u4e2a\u4e8b\u52a1\uff0c\u5e76\u5728\u65b9\u6cd5\u6267\u884c\u4e4b\u540e\u51b3\u5b9a\u4e8b\u52a1\u63d0\u4ea4\u6216\u56de\u6eda\u4e8b\u52a1\u3002<\/p>\n\n\n\n<p>\u5355\u72ec\u8c03\u7528methodB\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>main{\n    metodB();\n}  <\/code><\/pre>\n\n\n\n<p>\u76f8\u5f53\u4e8e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Main{\n    Connection con=null;\n    try{\n        con = getConnection();\n        con.setAutoCommit(false);\n\n        \/\/\u65b9\u6cd5\u8c03\u7528\n        methodB();\n\n        \/\/\u63d0\u4ea4\u4e8b\u52a1\n        con.commit();\n    } Catch(RuntimeException ex) {\n        \/\/\u56de\u6eda\u4e8b\u52a1\n        con.rollback();\n    } finally {\n        \/\/\u91ca\u653e\u8d44\u6e90\n        closeCon();\n    }\n} <\/code><\/pre>\n\n\n\n<p>Spring\u4fdd\u8bc1\u5728methodB\u65b9\u6cd5\u4e2d\u6240\u6709\u7684\u8c03\u7528\u90fd\u83b7\u5f97\u5230\u4e00\u4e2a\u76f8\u540c\u7684\u8fde\u63a5\u3002\u5728\u8c03\u7528methodB\u65f6\uff0c\u6ca1\u6709\u4e00\u4e2a\u5b58\u5728\u7684\u4e8b\u52a1\uff0c\u6240\u4ee5\u83b7\u5f97\u4e00\u4e2a\u65b0\u7684\u8fde\u63a5\uff0c\u5f00\u542f\u4e86\u4e00\u4e2a\u65b0\u7684\u4e8b\u52a1\u3002\u00a0<br\/>\u5355\u72ec\u8c03\u7528MethodA\u65f6\uff0c\u5728MethodA\u5185\u53c8\u4f1a\u8c03\u7528MethodB.<\/p>\n\n\n\n<p>\u6267\u884c\u6548\u679c\u76f8\u5f53\u4e8e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>main{\n    Connection con = null;\n    try{\n        con = getConnection();\n        methodA();\n        con.commit();\n    } catch(RuntimeException ex) {\n        con.rollback();\n    } finally {\n        closeCon();\n    }\n} <\/code><\/pre>\n\n\n\n<p>\u8c03\u7528MethodA\u65f6\uff0c\u73af\u5883\u4e2d\u6ca1\u6709\u4e8b\u52a1\uff0c\u6240\u4ee5\u5f00\u542f\u4e00\u4e2a\u65b0\u7684\u4e8b\u52a1.\u5f53\u5728MethodA\u4e2d\u8c03\u7528MethodB\u65f6\uff0c\u73af\u5883\u4e2d\u5df2\u7ecf\u6709\u4e86\u4e00\u4e2a\u4e8b\u52a1\uff0c\u6240\u4ee5methodB\u5c31\u52a0\u5165\u5f53\u524d\u4e8b\u52a1\u3002<\/p>\n\n\n\n<p>\uff082\uff09PROPAGATION_SUPPORTS \u5982\u679c\u5b58\u5728\u4e00\u4e2a\u4e8b\u52a1\uff0c\u652f\u6301\u5f53\u524d\u4e8b\u52a1\u3002\u5982\u679c\u6ca1\u6709\u4e8b\u52a1\uff0c\u5219\u975e\u4e8b\u52a1\u7684\u6267\u884c\u3002\u4f46\u662f\u5bf9\u4e8e\u4e8b\u52a1\u540c\u6b65\u7684\u4e8b\u52a1\u7ba1\u7406\u5668\uff0cPROPAGATION_SUPPORTS\u4e0e\u4e0d\u4f7f\u7528\u4e8b\u52a1\u6709\u5c11\u8bb8\u4e0d\u540c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u4e8b\u52a1\u5c5e\u6027 PROPAGATION_REQUIRED\nmethodA(){\n  methodB();\n}\n\n\/\/\u4e8b\u52a1\u5c5e\u6027 PROPAGATION_SUPPORTS\nmethodB(){\n  \u2026\u2026\n}<\/code><\/pre>\n\n\n\n<p>\u5355\u7eaf\u7684\u8c03\u7528methodB\u65f6\uff0cmethodB\u65b9\u6cd5\u662f\u975e\u4e8b\u52a1\u7684\u6267\u884c\u7684\u3002\u5f53\u8c03\u7528methdA\u65f6,methodB\u5219\u52a0\u5165\u4e86methodA\u7684\u4e8b\u52a1\u4e2d,\u4e8b\u52a1\u5730\u6267\u884c\u3002<\/p>\n\n\n\n<p>\uff083\uff09PROPAGATION_MANDATORY \u5982\u679c\u5df2\u7ecf\u5b58\u5728\u4e00\u4e2a\u4e8b\u52a1\uff0c\u652f\u6301\u5f53\u524d\u4e8b\u52a1\u3002\u5982\u679c\u6ca1\u6709\u4e00\u4e2a\u6d3b\u52a8\u7684\u4e8b\u52a1\uff0c\u5219\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u4e8b\u52a1\u5c5e\u6027 PROPAGATION_REQUIRED\nmethodA(){\n    methodB();\n}\n\n\/\/\u4e8b\u52a1\u5c5e\u6027 PROPAGATION_MANDATORY\n    methodB(){\n    \u2026\u2026\n}<\/code><\/pre>\n\n\n\n<p>\u5f53\u5355\u72ec\u8c03\u7528methodB\u65f6\uff0c\u56e0\u4e3a\u5f53\u524d\u6ca1\u6709\u4e00\u4e2a\u6d3b\u52a8\u7684\u4e8b\u52a1\uff0c\u5219\u4f1a\u629b\u51fa\u5f02\u5e38throw new IllegalTransactionStateException(\u201cTransaction propagation \u2018mandatory\u2019 but no existing transaction found\u201d);\u5f53\u8c03\u7528methodA\u65f6\uff0cmethodB\u5219\u52a0\u5165\u5230methodA\u7684\u4e8b\u52a1\u4e2d\uff0c\u4e8b\u52a1\u5730\u6267\u884c\u3002<\/p>\n\n\n\n<p>\uff084\uff09PROPAGATION_REQUIRES_NEW \u603b\u662f\u5f00\u542f\u4e00\u4e2a\u65b0\u7684\u4e8b\u52a1\u3002\u5982\u679c\u4e00\u4e2a\u4e8b\u52a1\u5df2\u7ecf\u5b58\u5728\uff0c\u5219\u5c06\u8fd9\u4e2a\u5b58\u5728\u7684\u4e8b\u52a1\u6302\u8d77\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u4e8b\u52a1\u5c5e\u6027 PROPAGATION_REQUIRED\nmethodA(){\n    doSomeThingA();\n    methodB();\n    doSomeThingB();\n}\n\n\/\/\u4e8b\u52a1\u5c5e\u6027 PROPAGATION_REQUIRES_NEW\nmethodB(){\n    \u2026\u2026\n}<\/code><\/pre>\n\n\n\n<p>\u8c03\u7528A\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>main(){\n    methodA();\n}<\/code><\/pre>\n\n\n\n<p>\u76f8\u5f53\u4e8e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>main(){\n    TransactionManager tm = null;\n    try{\n        \/\/\u83b7\u5f97\u4e00\u4e2aJTA\u4e8b\u52a1\u7ba1\u7406\u5668\n        tm = getTransactionManager();\n        tm.begin();\/\/\u5f00\u542f\u4e00\u4e2a\u65b0\u7684\u4e8b\u52a1\n        Transaction ts1 = tm.getTransaction();\n        doSomeThing();\n        tm.suspend();\/\/\u6302\u8d77\u5f53\u524d\u4e8b\u52a1\n        try{\n            tm.begin();\/\/\u91cd\u65b0\u5f00\u542f\u7b2c\u4e8c\u4e2a\u4e8b\u52a1\n            Transaction ts2 = tm.getTransaction();\n            methodB();\n            ts2.commit();\/\/\u63d0\u4ea4\u7b2c\u4e8c\u4e2a\u4e8b\u52a1\n        } Catch(RunTimeException ex) {\n            ts2.rollback();\/\/\u56de\u6eda\u7b2c\u4e8c\u4e2a\u4e8b\u52a1\n        } finally {\n            \/\/\u91ca\u653e\u8d44\u6e90\n        }\n        \/\/methodB\u6267\u884c\u5b8c\u540e\uff0c\u6062\u590d\u7b2c\u4e00\u4e2a\u4e8b\u52a1\n        tm.resume(ts1);\n        doSomeThingB();\n        ts1.commit();\/\/\u63d0\u4ea4\u7b2c\u4e00\u4e2a\u4e8b\u52a1\n    } catch(RunTimeException ex) {\n        ts1.rollback();\/\/\u56de\u6eda\u7b2c\u4e00\u4e2a\u4e8b\u52a1\n    } finally {\n        \/\/\u91ca\u653e\u8d44\u6e90\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u91cc\uff0c\u6211\u628ats1\u79f0\u4e3a\u5916\u5c42\u4e8b\u52a1\uff0cts2\u79f0\u4e3a\u5185\u5c42\u4e8b\u52a1\u3002\u4ece\u4e0a\u9762\u7684\u4ee3\u7801\u53ef\u4ee5\u770b\u51fa\uff0cts2\u4e0ets1\u662f\u4e24\u4e2a\u72ec\u7acb\u7684\u4e8b\u52a1\uff0c\u4e92\u4e0d\u76f8\u5e72\u3002Ts2\u662f\u5426\u6210\u529f\u5e76\u4e0d\u4f9d\u8d56\u4e8e ts1\u3002\u5982\u679cmethodA\u65b9\u6cd5\u5728\u8c03\u7528methodB\u65b9\u6cd5\u540e\u7684doSomeThingB\u65b9\u6cd5\u5931\u8d25\u4e86\uff0c\u800cmethodB\u65b9\u6cd5\u6240\u505a\u7684\u7ed3\u679c\u4f9d\u7136\u88ab\u63d0\u4ea4\u3002\u800c\u9664\u4e86 methodB\u4e4b\u5916\u7684\u5176\u5b83\u4ee3\u7801\u5bfc\u81f4\u7684\u7ed3\u679c\u5374\u88ab\u56de\u6eda\u4e86\u3002\u4f7f\u7528PROPAGATION_REQUIRES_NEW,\u9700\u8981\u4f7f\u7528 JtaTransactionManager\u4f5c\u4e3a\u4e8b\u52a1\u7ba1\u7406\u5668\u3002<\/p>\n\n\n\n<p>\uff085\uff09PROPAGATION_NOT_SUPPORTED \u603b\u662f\u975e\u4e8b\u52a1\u5730\u6267\u884c\uff0c\u5e76\u6302\u8d77\u4efb\u4f55\u5b58\u5728\u7684\u4e8b\u52a1\u3002\u4f7f\u7528PROPAGATION_NOT_SUPPORTED,\u4e5f\u9700\u8981\u4f7f\u7528JtaTransactionManager\u4f5c\u4e3a\u4e8b\u52a1\u7ba1\u7406\u5668\u3002\uff08\u4ee3\u7801\u793a\u4f8b\u540c\u4e0a\uff0c\u53ef\u540c\u7406\u63a8\u51fa\uff09<\/p>\n\n\n\n<p>\uff086\uff09PROPAGATION_NEVER \u603b\u662f\u975e\u4e8b\u52a1\u5730\u6267\u884c\uff0c\u5982\u679c\u5b58\u5728\u4e00\u4e2a\u6d3b\u52a8\u4e8b\u52a1\uff0c\u5219\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n\n\n\n<p>\uff087\uff09PROPAGATION_NESTED\u5982\u679c\u4e00\u4e2a\u6d3b\u52a8\u7684\u4e8b\u52a1\u5b58\u5728\uff0c\u5219\u8fd0\u884c\u5728\u4e00\u4e2a\u5d4c\u5957\u7684\u4e8b\u52a1\u4e2d. \u5982\u679c\u6ca1\u6709\u6d3b\u52a8\u4e8b\u52a1, \u5219\u6309TransactionDefinition.PROPAGATION_REQUIRED \u5c5e\u6027\u6267\u884c\u3002\u8fd9\u662f\u4e00\u4e2a\u5d4c\u5957\u4e8b\u52a1,\u4f7f\u7528JDBC 3.0\u9a71\u52a8\u65f6,\u4ec5\u4ec5\u652f\u6301DataSourceTransactionManager\u4f5c\u4e3a\u4e8b\u52a1\u7ba1\u7406\u5668\u3002\u9700\u8981JDBC \u9a71\u52a8\u7684java.sql.Savepoint\u7c7b\u3002\u6709\u4e00\u4e9bJTA\u7684\u4e8b\u52a1\u7ba1\u7406\u5668\u5b9e\u73b0\u53ef\u80fd\u4e5f\u63d0\u4f9b\u4e86\u540c\u6837\u7684\u529f\u80fd\u3002\u4f7f\u7528PROPAGATION_NESTED\uff0c\u8fd8\u9700\u8981\u628aPlatformTransactionManager\u7684nestedTransactionAllowed\u5c5e\u6027\u8bbe\u4e3atrue;\u800c nestedTransactionAllowed\u5c5e\u6027\u503c\u9ed8\u8ba4\u4e3afalse\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u4e8b\u52a1\u5c5e\u6027 PROPAGATION_REQUIRED\nmethodA(){\n    doSomeThingA();\n    methodB();\n    doSomeThingB();\n}\n\n\/\/\u4e8b\u52a1\u5c5e\u6027 PROPAGATION_NESTED\nmethodB(){\n    \u2026\u2026\n}<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u5355\u72ec\u8c03\u7528methodB\u65b9\u6cd5\uff0c\u5219\u6309REQUIRED\u5c5e\u6027\u6267\u884c\u3002\u5982\u679c\u8c03\u7528methodA\u65b9\u6cd5\uff0c\u76f8\u5f53\u4e8e\u4e0b\u9762\u7684\u6548\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>main(){\n    Connection con = null;\n    Savepoint savepoint = null;\n    try{\n        con = getConnection();\n        con.setAutoCommit(false);\n        doSomeThingA();\n        savepoint = con2.setSavepoint();\n        try{\n            methodB();\n        } catch(RuntimeException ex) {\n            con.rollback(savepoint);\n        } finally {\n            \/\/\u91ca\u653e\u8d44\u6e90\n        }\n        doSomeThingB();\n        con.commit();\n    } catch(RuntimeException ex) {\n        con.rollback();\n    } finally {\n        \/\/\u91ca\u653e\u8d44\u6e90\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u5f53methodB\u65b9\u6cd5\u8c03\u7528\u4e4b\u524d\uff0c\u8c03\u7528setSavepoint\u65b9\u6cd5\uff0c\u4fdd\u5b58\u5f53\u524d\u7684\u72b6\u6001\u5230savepoint\u3002\u5982\u679cmethodB\u65b9\u6cd5\u8c03\u7528\u5931\u8d25\uff0c\u5219\u6062\u590d\u5230\u4e4b\u524d\u4fdd\u5b58\u7684\u72b6\u6001\u3002\u4f46\u662f\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u65f6\u7684\u4e8b\u52a1\u5e76\u6ca1\u6709\u8fdb\u884c\u63d0\u4ea4\uff0c\u5982\u679c\u540e\u7eed\u7684\u4ee3\u7801(doSomeThingB()\u65b9\u6cd5)\u8c03\u7528\u5931\u8d25\uff0c\u5219\u56de\u6eda\u5305\u62ecmethodB\u65b9\u6cd5\u7684\u6240\u6709\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p>\u5d4c\u5957\u4e8b\u52a1\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u6982\u5ff5\u5c31\u662f\u5185\u5c42\u4e8b\u52a1\u4f9d\u8d56\u4e8e\u5916\u5c42\u4e8b\u52a1\u3002\u5916\u5c42\u4e8b\u52a1\u5931\u8d25\u65f6\uff0c\u4f1a\u56de\u6eda\u5185\u5c42\u4e8b\u52a1\u6240\u505a\u7684\u52a8\u4f5c\u3002\u800c\u5185\u5c42\u4e8b\u52a1\u64cd\u4f5c\u5931\u8d25\u5e76\u4e0d\u4f1a\u5f15\u8d77\u5916\u5c42\u4e8b\u52a1\u7684\u56de\u6eda\u3002<\/p>\n\n\n\n<p>PROPAGATION_NESTED \u4e0ePROPAGATION_REQUIRES_NEW\u7684\u533a\u522b:\u5b83\u4eec\u975e\u5e38\u7c7b\u4f3c,\u90fd\u50cf\u4e00\u4e2a\u5d4c\u5957\u4e8b\u52a1\uff0c\u5982\u679c\u4e0d\u5b58\u5728\u4e00\u4e2a\u6d3b\u52a8\u7684\u4e8b\u52a1\uff0c\u90fd\u4f1a\u5f00\u542f\u4e00\u4e2a\u65b0\u7684\u4e8b\u52a1\u3002\u4f7f\u7528 PROPAGATION_REQUIRES_NEW\u65f6\uff0c\u5185\u5c42\u4e8b\u52a1\u4e0e\u5916\u5c42\u4e8b\u52a1\u5c31\u50cf\u4e24\u4e2a\u72ec\u7acb\u7684\u4e8b\u52a1\u4e00\u6837\uff0c\u4e00\u65e6\u5185\u5c42\u4e8b\u52a1\u8fdb\u884c\u4e86\u63d0\u4ea4\u540e\uff0c\u5916\u5c42\u4e8b\u52a1\u4e0d\u80fd\u5bf9\u5176\u8fdb\u884c\u56de\u6eda\u3002\u4e24\u4e2a\u4e8b\u52a1\u4e92\u4e0d\u5f71\u54cd\u3002\u4e24\u4e2a\u4e8b\u52a1\u4e0d\u662f\u4e00\u4e2a\u771f\u6b63\u7684\u5d4c\u5957\u4e8b\u52a1\u3002\u540c\u65f6\u5b83\u9700\u8981JTA\u4e8b\u52a1\u7ba1\u7406\u5668\u7684\u652f\u6301\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528PROPAGATION_NESTED\u65f6\uff0c\u5916\u5c42\u4e8b\u52a1\u7684\u56de\u6eda\u53ef\u4ee5\u5f15\u8d77\u5185\u5c42\u4e8b\u52a1\u7684\u56de\u6eda\u3002\u800c\u5185\u5c42\u4e8b\u52a1\u7684\u5f02\u5e38\u5e76\u4e0d\u4f1a\u5bfc\u81f4\u5916\u5c42\u4e8b\u52a1\u7684\u56de\u6eda\uff0c\u5b83\u662f\u4e00\u4e2a\u771f\u6b63\u7684\u5d4c\u5957\u4e8b\u52a1\u3002DataSourceTransactionManager\u4f7f\u7528savepoint\u652f\u6301PROPAGATION_NESTED\u65f6\uff0c\u9700\u8981JDBC 3.0\u4ee5\u4e0a\u9a71\u52a8\u53ca1.4\u4ee5\u4e0a\u7684JDK\u7248\u672c\u652f\u6301\u3002\u5176\u5b83\u7684JTA TrasactionManager\u5b9e\u73b0\u53ef\u80fd\u6709\u4e0d\u540c\u7684\u652f\u6301\u65b9\u5f0f\u3002<\/p>\n\n\n\n<p>PROPAGATION_REQUIRES_NEW \u542f\u52a8\u4e00\u4e2a\u65b0\u7684, \u4e0d\u4f9d\u8d56\u4e8e\u73af\u5883\u7684 \u201c\u5185\u90e8\u201d \u4e8b\u52a1. \u8fd9\u4e2a\u4e8b\u52a1\u5c06\u88ab\u5b8c\u5168 commited \u6216 rolled back \u800c\u4e0d\u4f9d\u8d56\u4e8e\u5916\u90e8\u4e8b\u52a1, \u5b83\u62e5\u6709\u81ea\u5df1\u7684\u9694\u79bb\u8303\u56f4, \u81ea\u5df1\u7684\u9501, \u7b49\u7b49. \u5f53\u5185\u90e8\u4e8b\u52a1\u5f00\u59cb\u6267\u884c\u65f6, \u5916\u90e8\u4e8b\u52a1\u5c06\u88ab\u6302\u8d77, \u5185\u52a1\u4e8b\u52a1\u7ed3\u675f\u65f6, \u5916\u90e8\u4e8b\u52a1\u5c06\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n\n\n\n<p>\u53e6\u4e00\u65b9\u9762, PROPAGATION_NESTED \u5f00\u59cb\u4e00\u4e2a \u201c\u5d4c\u5957\u7684\u201d \u4e8b\u52a1, \u5b83\u662f\u5df2\u7ecf\u5b58\u5728\u4e8b\u52a1\u7684\u4e00\u4e2a\u771f\u6b63\u7684\u5b50\u4e8b\u52a1. \u6f5c\u5957\u4e8b\u52a1\u5f00\u59cb\u6267\u884c\u65f6, \u5b83\u5c06\u53d6\u5f97\u4e00\u4e2a savepoint. \u5982\u679c\u8fd9\u4e2a\u5d4c\u5957\u4e8b\u52a1\u5931\u8d25, \u6211\u4eec\u5c06\u56de\u6eda\u5230\u6b64 savepoint. \u6f5c\u5957\u4e8b\u52a1\u662f\u5916\u90e8\u4e8b\u52a1\u7684\u4e00\u90e8\u5206, \u53ea\u6709\u5916\u90e8\u4e8b\u52a1\u7ed3\u675f\u540e\u5b83\u624d\u4f1a\u88ab\u63d0\u4ea4\u3002<\/p>\n\n\n\n<p>\u7531\u6b64\u53ef\u89c1, PROPAGATION_REQUIRES_NEW \u548c PROPAGATION_NESTED \u7684\u6700\u5927\u533a\u522b\u5728\u4e8e, PROPAGATION_REQUIRES_NEW \u5b8c\u5168\u662f\u4e00\u4e2a\u65b0\u7684\u4e8b\u52a1, \u800c PROPAGATION_NESTED \u5219\u662f\u5916\u90e8\u4e8b\u52a1\u7684\u5b50\u4e8b\u52a1, \u5982\u679c\u5916\u90e8\u4e8b\u52a1 commit, \u5d4c\u5957\u4e8b\u52a1\u4e5f\u4f1a\u88ab commit, \u8fd9\u4e2a\u89c4\u5219\u540c\u6837\u9002\u7528\u4e8e roll back.<\/p>\n\n\n\n<p>PROPAGATION_REQUIRED\u5e94\u8be5\u662f\u6211\u4eec\u9996\u5148\u7684\u4e8b\u52a1\u4f20\u64ad\u884c\u4e3a\u3002\u5b83\u80fd\u591f\u6ee1\u8db3\u6211\u4eec\u5927\u591a\u6570\u7684\u4e8b\u52a1\u9700\u6c42\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"222-\u9694\u79bb\u7ea7\u522b\">2.2.2 \u9694\u79bb\u7ea7\u522b<\/h3>\n\n\n\n<p>\u4e8b\u52a1\u7684\u7b2c\u4e8c\u4e2a\u7ef4\u5ea6\u5c31\u662f\u9694\u79bb\u7ea7\u522b\uff08isolation level\uff09\u3002\u9694\u79bb\u7ea7\u522b\u5b9a\u4e49\u4e86\u4e00\u4e2a\u4e8b\u52a1\u53ef\u80fd\u53d7\u5176\u4ed6\u5e76\u53d1\u4e8b\u52a1\u5f71\u54cd\u7684\u7a0b\u5ea6\u3002\u00a0<br\/>\uff081\uff09\u5e76\u53d1\u4e8b\u52a1\u5f15\u8d77\u7684\u95ee\u9898\u00a0<br\/>\u5728\u5178\u578b\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u591a\u4e2a\u4e8b\u52a1\u5e76\u53d1\u8fd0\u884c\uff0c\u7ecf\u5e38\u4f1a\u64cd\u4f5c\u76f8\u540c\u7684\u6570\u636e\u6765\u5b8c\u6210\u5404\u81ea\u7684\u4efb\u52a1\u3002\u5e76\u53d1\u867d\u7136\u662f\u5fc5\u987b\u7684\uff0c\u4f46\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e00\u4e0b\u7684\u95ee\u9898\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u810f\u8bfb\uff08Dirty reads\uff09\u2014\u2014\u810f\u8bfb\u53d1\u751f\u5728\u4e00\u4e2a\u4e8b\u52a1\u8bfb\u53d6\u4e86\u53e6\u4e00\u4e2a\u4e8b\u52a1\u6539\u5199\u4f46\u5c1a\u672a\u63d0\u4ea4\u7684\u6570\u636e\u65f6\u3002\u5982\u679c\u6539\u5199\u5728\u7a0d\u540e\u88ab\u56de\u6eda\u4e86\uff0c\u90a3\u4e48\u7b2c\u4e00\u4e2a\u4e8b\u52a1\u83b7\u53d6\u7684\u6570\u636e\u5c31\u662f\u65e0\u6548\u7684\u3002<br\/>\u4e0d\u53ef\u91cd\u590d\u8bfb\uff08Nonrepeatable read\uff09\u2014\u2014\u4e0d\u53ef\u91cd\u590d\u8bfb\u53d1\u751f\u5728\u4e00\u4e2a\u4e8b\u52a1\u6267\u884c\u76f8\u540c\u7684\u67e5\u8be2\u4e24\u6b21\u6216\u4e24\u6b21\u4ee5\u4e0a\uff0c\u4f46\u662f\u6bcf\u6b21\u90fd\u5f97\u5230\u4e0d\u540c\u7684\u6570\u636e\u65f6\u3002\u8fd9\u901a\u5e38\u662f\u56e0\u4e3a\u53e6\u4e00\u4e2a\u5e76\u53d1\u4e8b\u52a1\u5728\u4e24\u6b21\u67e5\u8be2\u671f\u95f4\u8fdb\u884c\u4e86\u66f4\u65b0\u3002<br\/>\u5e7b\u8bfb\uff08Phantom read\uff09\u2014\u2014\u5e7b\u8bfb\u4e0e\u4e0d\u53ef\u91cd\u590d\u8bfb\u7c7b\u4f3c\u3002\u5b83\u53d1\u751f\u5728\u4e00\u4e2a\u4e8b\u52a1\uff08T1\uff09\u8bfb\u53d6\u4e86\u51e0\u884c\u6570\u636e\uff0c\u63a5\u7740\u53e6\u4e00\u4e2a\u5e76\u53d1\u4e8b\u52a1\uff08T2\uff09\u63d2\u5165\u4e86\u4e00\u4e9b\u6570\u636e\u65f6\u3002\u5728\u968f\u540e\u7684\u67e5\u8be2\u4e2d\uff0c\u7b2c\u4e00\u4e2a\u4e8b\u52a1\uff08T1\uff09\u5c31\u4f1a\u53d1\u73b0\u591a\u4e86\u4e00\u4e9b\u539f\u672c\u4e0d\u5b58\u5728\u7684\u8bb0\u5f55\u3002<\/p><\/blockquote>\n\n\n\n<p><strong>\u4e0d\u53ef\u91cd\u590d\u8bfb\u4e0e\u5e7b\u8bfb\u7684\u533a\u522b<\/strong><\/p>\n\n\n\n<p>\u4e0d\u53ef\u91cd\u590d\u8bfb\u7684\u91cd\u70b9\u662f\u4fee\u6539:\u00a0<br\/>\u540c\u6837\u7684\u6761\u4ef6, \u4f60\u8bfb\u53d6\u8fc7\u7684\u6570\u636e, \u518d\u6b21\u8bfb\u53d6\u51fa\u6765\u53d1\u73b0\u503c\u4e0d\u4e00\u6837\u4e86\u00a0<br\/>\u4f8b\u5982\uff1a\u5728\u4e8b\u52a11\u4e2d\uff0cMary \u8bfb\u53d6\u4e86\u81ea\u5df1\u7684\u5de5\u8d44\u4e3a1000,\u64cd\u4f5c\u5e76\u6ca1\u6709\u5b8c\u6210<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    con1 = getConnection();\n    select salary from employee empId =\"Mary\";  <\/code><\/pre>\n\n\n\n<p>\u5728\u4e8b\u52a12\u4e2d\uff0c\u8fd9\u65f6\u8d22\u52a1\u4eba\u5458\u4fee\u6539\u4e86Mary\u7684\u5de5\u8d44\u4e3a2000,\u5e76\u63d0\u4ea4\u4e86\u4e8b\u52a1.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    con2 = getConnection();\n    update employee set salary = 2000;\n    con2.commit();  <\/code><\/pre>\n\n\n\n<p>\u5728\u4e8b\u52a11\u4e2d\uff0cMary \u518d\u6b21\u8bfb\u53d6\u81ea\u5df1\u7684\u5de5\u8d44\u65f6\uff0c\u5de5\u8d44\u53d8\u4e3a\u4e862000<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    \/\/con1\n    select salary from employee empId =\"Mary\"; <\/code><\/pre>\n\n\n\n<p>\u5728\u4e00\u4e2a\u4e8b\u52a1\u4e2d\u524d\u540e\u4e24\u6b21\u8bfb\u53d6\u7684\u7ed3\u679c\u5e76\u4e0d\u4e00\u81f4\uff0c\u5bfc\u81f4\u4e86\u4e0d\u53ef\u91cd\u590d\u8bfb\u3002<\/p>\n\n\n\n<p>\u5e7b\u8bfb\u7684\u91cd\u70b9\u5728\u4e8e\u65b0\u589e\u6216\u8005\u5220\u9664\uff1a\u00a0<br\/>\u540c\u6837\u7684\u6761\u4ef6, \u7b2c1\u6b21\u548c\u7b2c2\u6b21\u8bfb\u51fa\u6765\u7684\u8bb0\u5f55\u6570\u4e0d\u4e00\u6837\u00a0<br\/>\u4f8b\u5982\uff1a\u76ee\u524d\u5de5\u8d44\u4e3a1000\u7684\u5458\u5de5\u670910\u4eba\u3002\u4e8b\u52a11,\u8bfb\u53d6\u6240\u6709\u5de5\u8d44\u4e3a1000\u7684\u5458\u5de5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    con1 = getConnection();\n    Select * from employee where salary =1000; <\/code><\/pre>\n\n\n\n<p>\u5171\u8bfb\u53d610\u6761\u8bb0\u5f55<\/p>\n\n\n\n<p>\u8fd9\u65f6\u53e6\u4e00\u4e2a\u4e8b\u52a1\u5411employee\u8868\u63d2\u5165\u4e86\u4e00\u6761\u5458\u5de5\u8bb0\u5f55\uff0c\u5de5\u8d44\u4e5f\u4e3a1000<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    con2 = getConnection();\n    Insert into employee(empId,salary) values(\"Lili\",1000);\n    con2.commit();  <\/code><\/pre>\n\n\n\n<p>\u4e8b\u52a11\u518d\u6b21\u8bfb\u53d6\u6240\u6709\u5de5\u8d44\u4e3a1000\u7684\u5458\u5de5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    \/\/con1\n    select * from employee where salary =1000;  <\/code><\/pre>\n\n\n\n<p>\u5171\u8bfb\u53d6\u5230\u4e8611\u6761\u8bb0\u5f55\uff0c\u8fd9\u5c31\u4ea7\u751f\u4e86\u5e7b\u50cf\u8bfb\u3002<\/p>\n\n\n\n<p>\u4ece\u603b\u7684\u7ed3\u679c\u6765\u770b, \u4f3c\u4e4e\u4e0d\u53ef\u91cd\u590d\u8bfb\u548c\u5e7b\u8bfb\u90fd\u8868\u73b0\u4e3a\u4e24\u6b21\u8bfb\u53d6\u7684\u7ed3\u679c\u4e0d\u4e00\u81f4\u3002\u4f46\u5982\u679c\u4f60\u4ece\u63a7\u5236\u7684\u89d2\u5ea6\u6765\u770b, \u4e24\u8005\u7684\u533a\u522b\u5c31\u6bd4\u8f83\u5927\u3002\u00a0<br\/>\u5bf9\u4e8e\u524d\u8005, \u53ea\u9700\u8981\u9501\u4f4f\u6ee1\u8db3\u6761\u4ef6\u7684\u8bb0\u5f55\u3002\u00a0<br\/>\u5bf9\u4e8e\u540e\u8005, \u8981\u9501\u4f4f\u6ee1\u8db3\u6761\u4ef6\u53ca\u5176\u76f8\u8fd1\u7684\u8bb0\u5f55\u3002<\/p>\n\n\n\n<p>\uff082\uff09\u9694\u79bb\u7ea7\u522b<\/p>\n\n\n\n<table class=\"wp-block-table\"><thead><tr><th>\u9694\u79bb\u7ea7\u522b<\/th><th>\u542b\u4e49<\/th><\/tr><\/thead><tbody><tr><td>ISOLATION_DEFAULT<\/td><td>\u4f7f\u7528\u540e\u7aef\u6570\u636e\u5e93\u9ed8\u8ba4\u7684\u9694\u79bb\u7ea7\u522b<\/td><\/tr><tr><td>ISOLATION_READ_UNCOMMITTED<\/td><td>\u6700\u4f4e\u7684\u9694\u79bb\u7ea7\u522b\uff0c\u5141\u8bb8\u8bfb\u53d6\u5c1a\u672a\u63d0\u4ea4\u7684\u6570\u636e\u53d8\u66f4\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u810f\u8bfb\u3001\u5e7b\u8bfb\u6216\u4e0d\u53ef\u91cd\u590d\u8bfb<\/td><\/tr><tr><td>ISOLATION_READ_COMMITTED<\/td><td>\u5141\u8bb8\u8bfb\u53d6\u5e76\u53d1\u4e8b\u52a1\u5df2\u7ecf\u63d0\u4ea4\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u963b\u6b62\u810f\u8bfb\uff0c\u4f46\u662f\u5e7b\u8bfb\u6216\u4e0d\u53ef\u91cd\u590d\u8bfb\u4ecd\u6709\u53ef\u80fd\u53d1\u751f<\/td><\/tr><tr><td>ISOLATION_REPEATABLE_READ<\/td><td>\u5bf9\u540c\u4e00\u5b57\u6bb5\u7684\u591a\u6b21\u8bfb\u53d6\u7ed3\u679c\u90fd\u662f\u4e00\u81f4\u7684\uff0c\u9664\u975e\u6570\u636e\u662f\u88ab\u672c\u8eab\u4e8b\u52a1\u81ea\u5df1\u6240\u4fee\u6539\uff0c\u53ef\u4ee5\u963b\u6b62\u810f\u8bfb\u548c\u4e0d\u53ef\u91cd\u590d\u8bfb\uff0c\u4f46\u5e7b\u8bfb\u4ecd\u6709\u53ef\u80fd\u53d1\u751f<\/td><\/tr><tr><td>ISOLATION_SERIALIZABLE<\/td><td>\u6700\u9ad8\u7684\u9694\u79bb\u7ea7\u522b\uff0c\u5b8c\u5168\u670d\u4eceACID\u7684\u9694\u79bb\u7ea7\u522b\uff0c\u786e\u4fdd\u963b\u6b62\u810f\u8bfb\u3001\u4e0d\u53ef\u91cd\u590d\u8bfb\u4ee5\u53ca\u5e7b\u8bfb\uff0c\u4e5f\u662f\u6700\u6162\u7684\u4e8b\u52a1\u9694\u79bb\u7ea7\u522b\uff0c\u56e0\u4e3a\u5b83\u901a\u5e38\u662f\u901a\u8fc7\u5b8c\u5168\u9501\u5b9a\u4e8b\u52a1\u76f8\u5173\u7684\u6570\u636e\u5e93\u8868\u6765\u5b9e\u73b0\u7684<\/td><\/tr><\/tbody><\/table>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"223-\u53ea\u8bfb\">2.2.3 \u53ea\u8bfb<\/h3>\n\n\n\n<p>\u4e8b\u52a1\u7684\u7b2c\u4e09\u4e2a\u7279\u6027\u662f\u5b83\u662f\u5426\u4e3a\u53ea\u8bfb\u4e8b\u52a1\u3002\u5982\u679c\u4e8b\u52a1\u53ea\u5bf9\u540e\u7aef\u7684\u6570\u636e\u5e93\u8fdb\u884c\u8be5\u64cd\u4f5c\uff0c\u6570\u636e\u5e93\u53ef\u4ee5\u5229\u7528\u4e8b\u52a1\u7684\u53ea\u8bfb\u7279\u6027\u6765\u8fdb\u884c\u4e00\u4e9b\u7279\u5b9a\u7684\u4f18\u5316\u3002\u901a\u8fc7\u5c06\u4e8b\u52a1\u8bbe\u7f6e\u4e3a\u53ea\u8bfb\uff0c\u4f60\u5c31\u53ef\u4ee5\u7ed9\u6570\u636e\u5e93\u4e00\u4e2a\u673a\u4f1a\uff0c\u8ba9\u5b83\u5e94\u7528\u5b83\u8ba4\u4e3a\u5408\u9002\u7684\u4f18\u5316\u63aa\u65bd\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"224-\u4e8b\u52a1\u8d85\u65f6\">2.2.4 \u4e8b\u52a1\u8d85\u65f6<\/h3>\n\n\n\n<p>\u4e3a\u4e86\u4f7f\u5e94\u7528\u7a0b\u5e8f\u5f88\u597d\u5730\u8fd0\u884c\uff0c\u4e8b\u52a1\u4e0d\u80fd\u8fd0\u884c\u592a\u957f\u7684\u65f6\u95f4\u3002\u56e0\u4e3a\u4e8b\u52a1\u53ef\u80fd\u6d89\u53ca\u5bf9\u540e\u7aef\u6570\u636e\u5e93\u7684\u9501\u5b9a\uff0c\u6240\u4ee5\u957f\u65f6\u95f4\u7684\u4e8b\u52a1\u4f1a\u4e0d\u5fc5\u8981\u7684\u5360\u7528\u6570\u636e\u5e93\u8d44\u6e90\u3002\u4e8b\u52a1\u8d85\u65f6\u5c31\u662f\u4e8b\u52a1\u7684\u4e00\u4e2a\u5b9a\u65f6\u5668\uff0c\u5728\u7279\u5b9a\u65f6\u95f4\u5185\u4e8b\u52a1\u5982\u679c\u6ca1\u6709\u6267\u884c\u5b8c\u6bd5\uff0c\u90a3\u4e48\u5c31\u4f1a\u81ea\u52a8\u56de\u6eda\uff0c\u800c\u4e0d\u662f\u4e00\u76f4\u7b49\u5f85\u5176\u7ed3\u675f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"225-\u56de\u6eda\u89c4\u5219\">2.2.5 \u56de\u6eda\u89c4\u5219<\/h3>\n\n\n\n<p>\u4e8b\u52a1\u4e94\u8fb9\u5f62\u7684\u6700\u540e\u4e00\u4e2a\u65b9\u9762\u662f\u4e00\u7ec4\u89c4\u5219\uff0c\u8fd9\u4e9b\u89c4\u5219\u5b9a\u4e49\u4e86\u54ea\u4e9b\u5f02\u5e38\u4f1a\u5bfc\u81f4\u4e8b\u52a1\u56de\u6eda\u800c\u54ea\u4e9b\u4e0d\u4f1a\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u4e8b\u52a1\u53ea\u6709\u9047\u5230\u8fd0\u884c\u671f\u5f02\u5e38\u65f6\u624d\u4f1a\u56de\u6eda\uff0c\u800c\u5728\u9047\u5230\u68c0\u67e5\u578b\u5f02\u5e38\u65f6\u4e0d\u4f1a\u56de\u6eda\uff08\u8fd9\u4e00\u884c\u4e3a\u4e0eEJB\u7684\u56de\u6eda\u884c\u4e3a\u662f\u4e00\u81f4\u7684\uff09\u00a0<br\/>\u4f46\u662f\u4f60\u53ef\u4ee5\u58f0\u660e\u4e8b\u52a1\u5728\u9047\u5230\u7279\u5b9a\u7684\u68c0\u67e5\u578b\u5f02\u5e38\u65f6\u50cf\u9047\u5230\u8fd0\u884c\u671f\u5f02\u5e38\u90a3\u6837\u56de\u6eda\u3002\u540c\u6837\uff0c\u4f60\u8fd8\u53ef\u4ee5\u58f0\u660e\u4e8b\u52a1\u9047\u5230\u7279\u5b9a\u7684\u5f02\u5e38\u4e0d\u56de\u6eda\uff0c\u5373\u4f7f\u8fd9\u4e9b\u5f02\u5e38\u662f\u8fd0\u884c\u671f\u5f02\u5e38\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"23-\u4e8b\u52a1\u72b6\u6001\">2.3 \u4e8b\u52a1\u72b6\u6001<\/h2>\n\n\n\n<p>\u4e0a\u9762\u8bb2\u5230\u7684\u8c03\u7528PlatformTransactionManager\u63a5\u53e3\u7684getTransaction()\u7684\u65b9\u6cd5\u5f97\u5230\u7684\u662fTransactionStatus\u63a5\u53e3\u7684\u4e00\u4e2a\u5b9e\u73b0\uff0c\u8fd9\u4e2a\u63a5\u53e3\u7684\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public interface TransactionStatus{\n    boolean isNewTransaction(); \/\/ \u662f\u5426\u662f\u65b0\u7684\u4e8b\u7269\n    boolean hasSavepoint(); \/\/ \u662f\u5426\u6709\u6062\u590d\u70b9\n    void setRollbackOnly();  \/\/ \u8bbe\u7f6e\u4e3a\u53ea\u56de\u6eda\n    boolean isRollbackOnly(); \/\/ \u662f\u5426\u4e3a\u53ea\u56de\u6eda\n    boolean isCompleted; \/\/ \u662f\u5426\u5df2\u5b8c\u6210\n} <\/code><\/pre>\n\n\n\n<p>\u53ef\u4ee5\u53d1\u73b0\u8fd9\u4e2a\u63a5\u53e3\u63cf\u8ff0\u7684\u662f\u4e00\u4e9b\u5904\u7406\u4e8b\u52a1\u63d0\u4f9b\u7b80\u5355\u7684\u63a7\u5236\u4e8b\u52a1\u6267\u884c\u548c\u67e5\u8be2\u4e8b\u52a1\u72b6\u6001\u7684\u65b9\u6cd5\uff0c\u5728\u56de\u6eda\u6216\u63d0\u4ea4\u7684\u65f6\u5019\u9700\u8981\u5e94\u7528\u5bf9\u5e94\u7684\u4e8b\u52a1\u72b6\u6001\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"3-\u7f16\u7a0b\u5f0f\u4e8b\u52a1\">3 \u7f16\u7a0b\u5f0f\u4e8b\u52a1<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"31-\u7f16\u7a0b\u5f0f\u548c\u58f0\u660e\u5f0f\u4e8b\u52a1\u7684\u533a\u522b\">3.1 \u7f16\u7a0b\u5f0f\u548c\u58f0\u660e\u5f0f\u4e8b\u52a1\u7684\u533a\u522b<\/h2>\n\n\n\n<p>Spring\u63d0\u4f9b\u4e86\u5bf9\u7f16\u7a0b\u5f0f\u4e8b\u52a1\u548c\u58f0\u660e\u5f0f\u4e8b\u52a1\u7684\u652f\u6301\uff0c\u7f16\u7a0b\u5f0f\u4e8b\u52a1\u5141\u8bb8\u7528\u6237\u5728\u4ee3\u7801\u4e2d\u7cbe\u786e\u5b9a\u4e49\u4e8b\u52a1\u7684\u8fb9\u754c\uff0c\u800c\u58f0\u660e\u5f0f\u4e8b\u52a1\uff08\u57fa\u4e8eAOP\uff09\u6709\u52a9\u4e8e\u7528\u6237\u5c06\u64cd\u4f5c\u4e0e\u4e8b\u52a1\u89c4\u5219\u8fdb\u884c\u89e3\u8026\u3002\u00a0<br\/>\u7b80\u5355\u5730\u8bf4\uff0c\u7f16\u7a0b\u5f0f\u4e8b\u52a1\u4fb5\u5165\u5230\u4e86\u4e1a\u52a1\u4ee3\u7801\u91cc\u9762\uff0c\u4f46\u662f\u63d0\u4f9b\u4e86\u66f4\u52a0\u8be6\u7ec6\u7684\u4e8b\u52a1\u7ba1\u7406\uff1b\u800c\u58f0\u660e\u5f0f\u4e8b\u52a1\u7531\u4e8e\u57fa\u4e8eAOP\uff0c\u6240\u4ee5\u65e2\u80fd\u8d77\u5230\u4e8b\u52a1\u7ba1\u7406\u7684\u4f5c\u7528\uff0c\u53c8\u53ef\u4ee5\u4e0d\u5f71\u54cd\u4e1a\u52a1\u4ee3\u7801\u7684\u5177\u4f53\u5b9e\u73b0\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"32-\u5982\u4f55\u5b9e\u73b0\u7f16\u7a0b\u5f0f\u4e8b\u52a1\">3.2 \u5982\u4f55\u5b9e\u73b0\u7f16\u7a0b\u5f0f\u4e8b\u52a1\uff1f<\/h2>\n\n\n\n<p>Spring\u63d0\u4f9b\u4e24\u79cd\u65b9\u5f0f\u7684\u7f16\u7a0b\u5f0f\u4e8b\u52a1\u7ba1\u7406\uff0c\u5206\u522b\u662f\uff1a\u4f7f\u7528TransactionTemplate\u548c\u76f4\u63a5\u4f7f\u7528PlatformTransactionManager\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"321-\u4f7f\u7528transactiontemplate\">3.2.1 \u4f7f\u7528TransactionTemplate<\/h3>\n\n\n\n<p>\u91c7\u7528TransactionTemplate\u548c\u91c7\u7528\u5176\u4ed6Spring\u6a21\u677f\uff0c\u5982JdbcTempalte\u548cHibernateTemplate\u662f\u4e00\u6837\u7684\u65b9\u6cd5\u3002\u5b83\u4f7f\u7528\u56de\u8c03\u65b9\u6cd5\uff0c\u628a\u5e94\u7528\u7a0b\u5e8f\u4ece\u5904\u7406\u53d6\u5f97\u548c\u91ca\u653e\u8d44\u6e90\u4e2d\u89e3\u8131\u51fa\u6765\u3002\u5982\u540c\u5176\u4ed6\u6a21\u677f\uff0cTransactionTemplate\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002\u4ee3\u7801\u7247\u6bb5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    TransactionTemplate tt = new TransactionTemplate(); \/\/ \u65b0\u5efa\u4e00\u4e2aTransactionTemplate\n    Object result = tt.execute(\n        new TransactionCallback(){\n            public Object doTransaction(TransactionStatus status){\n                updateOperation();\n                return resultOfUpdateOperation();\n            }\n    }); \/\/ \u6267\u884cexecute\u65b9\u6cd5\u8fdb\u884c\u4e8b\u52a1\u7ba1\u7406<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528TransactionCallback()\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u503c\u3002\u5982\u679c\u4f7f\u7528TransactionCallbackWithoutResult\u5219\u6ca1\u6709\u8fd4\u56de\u503c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"322-\u4f7f\u7528platformtransactionmanager\">3.2.2 \u4f7f\u7528PlatformTransactionManager<\/h3>\n\n\n\n<p>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n    DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(); \/\/\u5b9a\u4e49\u4e00\u4e2a\u67d0\u4e2a\u6846\u67b6\u5e73\u53f0\u7684TransactionManager\uff0c\u5982JDBC\u3001Hibernate\n    dataSourceTransactionManager.setDataSource(this.getJdbcTemplate().getDataSource()); \/\/ \u8bbe\u7f6e\u6570\u636e\u6e90\n    DefaultTransactionDefinition transDef = new DefaultTransactionDefinition(); \/\/ \u5b9a\u4e49\u4e8b\u52a1\u5c5e\u6027\n    transDef.setPropagationBehavior(DefaultTransactionDefinition.PROPAGATION_REQUIRED); \/\/ \u8bbe\u7f6e\u4f20\u64ad\u884c\u4e3a\u5c5e\u6027\n    TransactionStatus status = dataSourceTransactionManager.getTransaction(transDef); \/\/ \u83b7\u5f97\u4e8b\u52a1\u72b6\u6001\n    try {\n        \/\/ \u6570\u636e\u5e93\u64cd\u4f5c\n        dataSourceTransactionManager.commit(status);\/\/ \u63d0\u4ea4\n    } catch (Exception e) {\n        dataSourceTransactionManager.rollback(status);\/\/ \u56de\u6eda\n    }<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"4-\u58f0\u660e\u5f0f\u4e8b\u52a1\">4 \u58f0\u660e\u5f0f\u4e8b\u52a1<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"41-\u914d\u7f6e\u65b9\u5f0f\">4.1 \u914d\u7f6e\u65b9\u5f0f<\/h2>\n\n\n\n<p><em>\u6ce8\uff1a\u4ee5\u4e0b\u914d\u7f6e\u4ee3\u7801\u53c2\u8003\u81ea<a href=\"http:\/\/www.blogjava.net\/robbie\/archive\/2009\/04\/05\/264003.html\" rel=\"noreferrer noopener\" target=\"_blank\">Spring\u4e8b\u52a1\u914d\u7f6e\u7684\u4e94\u79cd\u65b9\u5f0f<\/a><\/em><\/p>\n\n\n\n<p>\u6839\u636e\u4ee3\u7406\u673a\u5236\u7684\u4e0d\u540c\uff0c\u603b\u7ed3\u4e86\u4e94\u79cdSpring\u4e8b\u52a1\u7684\u914d\u7f6e\u65b9\u5f0f\uff0c\u914d\u7f6e\u6587\u4ef6\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>\uff081\uff09\u6bcf\u4e2aBean\u90fd\u6709\u4e00\u4e2a\u4ee3\u7406<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?>\n&lt;beans xmlns=\"http:\/\/www.springframework.org\/schema\/beans\"\n    xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n    xmlns:context=\"http:\/\/www.springframework.org\/schema\/context\"\n    xmlns:aop=\"http:\/\/www.springframework.org\/schema\/aop\"\n    xsi:schemaLocation=\"http:\/\/www.springframework.org\/schema\/beans\n           http:\/\/www.springframework.org\/schema\/beans\/spring-beans-2.5.xsd\n           http:\/\/www.springframework.org\/schema\/context\n           http:\/\/www.springframework.org\/schema\/context\/spring-context-2.5.xsd\n           http:\/\/www.springframework.org\/schema\/aop http:\/\/www.springframework.org\/schema\/aop\/spring-aop-2.5.xsd\">\n\n    &lt;bean id=\"sessionFactory\"\n            class=\"org.springframework.orm.hibernate3.LocalSessionFactoryBean\">\n        &lt;property name=\"configLocation\" value=\"classpath:hibernate.cfg.xml\" \/>\n        &lt;property name=\"configurationClass\" value=\"org.hibernate.cfg.AnnotationConfiguration\" \/>\n    &lt;\/bean>\n\n    &lt;!-- \u5b9a\u4e49\u4e8b\u52a1\u7ba1\u7406\u5668\uff08\u58f0\u660e\u5f0f\u7684\u4e8b\u52a1\uff09 -->\n    &lt;bean id=\"transactionManager\"\n        class=\"org.springframework.orm.hibernate3.HibernateTransactionManager\">\n        &lt;property name=\"sessionFactory\" ref=\"sessionFactory\" \/>\n    &lt;\/bean>\n\n    &lt;!-- \u914d\u7f6eDAO -->\n    &lt;bean id=\"userDaoTarget\" class=\"com.bluesky.spring.dao.UserDaoImpl\">\n        &lt;property name=\"sessionFactory\" ref=\"sessionFactory\" \/>\n    &lt;\/bean>\n\n    &lt;bean id=\"userDao\"\n        class=\"org.springframework.transaction.interceptor.TransactionProxyFactoryBean\">\n           &lt;!-- \u914d\u7f6e\u4e8b\u52a1\u7ba1\u7406\u5668 -->\n           &lt;property name=\"transactionManager\" ref=\"transactionManager\" \/>\n        &lt;property name=\"target\" ref=\"userDaoTarget\" \/>\n         &lt;property name=\"proxyInterfaces\" value=\"com.bluesky.spring.dao.GeneratorDao\" \/>\n        &lt;!-- \u914d\u7f6e\u4e8b\u52a1\u5c5e\u6027 -->\n        &lt;property name=\"transactionAttributes\">\n            &lt;props>\n                &lt;prop key=\"*\">PROPAGATION_REQUIRED&lt;\/prop>\n            &lt;\/props>\n        &lt;\/property>\n    &lt;\/bean>\n&lt;\/beans><\/code><\/pre>\n\n\n\n<p>\uff082\uff09\u6240\u6709Bean\u5171\u4eab\u4e00\u4e2a\u4ee3\u7406\u57fa\u7c7b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?>\n&lt;beans xmlns=\"http:\/\/www.springframework.org\/schema\/beans\"\n    xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n    xmlns:context=\"http:\/\/www.springframework.org\/schema\/context\"\n    xmlns:aop=\"http:\/\/www.springframework.org\/schema\/aop\"\n    xsi:schemaLocation=\"http:\/\/www.springframework.org\/schema\/beans\n           http:\/\/www.springframework.org\/schema\/beans\/spring-beans-2.5.xsd\n           http:\/\/www.springframework.org\/schema\/context\n           http:\/\/www.springframework.org\/schema\/context\/spring-context-2.5.xsd\n           http:\/\/www.springframework.org\/schema\/aop http:\/\/www.springframework.org\/schema\/aop\/spring-aop-2.5.xsd\">\n\n    &lt;bean id=\"sessionFactory\"\n            class=\"org.springframework.orm.hibernate3.LocalSessionFactoryBean\">\n        &lt;property name=\"configLocation\" value=\"classpath:hibernate.cfg.xml\" \/>\n        &lt;property name=\"configurationClass\" value=\"org.hibernate.cfg.AnnotationConfiguration\" \/>\n    &lt;\/bean>\n\n    &lt;!-- \u5b9a\u4e49\u4e8b\u52a1\u7ba1\u7406\u5668\uff08\u58f0\u660e\u5f0f\u7684\u4e8b\u52a1\uff09 -->\n    &lt;bean id=\"transactionManager\"\n        class=\"org.springframework.orm.hibernate3.HibernateTransactionManager\">\n        &lt;property name=\"sessionFactory\" ref=\"sessionFactory\" \/>\n    &lt;\/bean>\n\n    &lt;bean id=\"transactionBase\"\n            class=\"org.springframework.transaction.interceptor.TransactionProxyFactoryBean\"\n            lazy-init=\"true\" abstract=\"true\">\n        &lt;!-- \u914d\u7f6e\u4e8b\u52a1\u7ba1\u7406\u5668 -->\n        &lt;property name=\"transactionManager\" ref=\"transactionManager\" \/>\n        &lt;!-- \u914d\u7f6e\u4e8b\u52a1\u5c5e\u6027 -->\n        &lt;property name=\"transactionAttributes\">\n            &lt;props>\n                &lt;prop key=\"*\">PROPAGATION_REQUIRED&lt;\/prop>\n            &lt;\/props>\n        &lt;\/property>\n    &lt;\/bean>\n\n    &lt;!-- \u914d\u7f6eDAO -->\n    &lt;bean id=\"userDaoTarget\" class=\"com.bluesky.spring.dao.UserDaoImpl\">\n        &lt;property name=\"sessionFactory\" ref=\"sessionFactory\" \/>\n    &lt;\/bean>\n\n    &lt;bean id=\"userDao\" parent=\"transactionBase\" >\n        &lt;property name=\"target\" ref=\"userDaoTarget\" \/>\n    &lt;\/bean>\n&lt;\/beans><\/code><\/pre>\n\n\n\n<p>\uff083\uff09\u4f7f\u7528\u62e6\u622a\u5668<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?>\n&lt;beans xmlns=\"http:\/\/www.springframework.org\/schema\/beans\"\n    xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n    xmlns:context=\"http:\/\/www.springframework.org\/schema\/context\"\n    xmlns:aop=\"http:\/\/www.springframework.org\/schema\/aop\"\n    xsi:schemaLocation=\"http:\/\/www.springframework.org\/schema\/beans\n           http:\/\/www.springframework.org\/schema\/beans\/spring-beans-2.5.xsd\n           http:\/\/www.springframework.org\/schema\/context\n           http:\/\/www.springframework.org\/schema\/context\/spring-context-2.5.xsd\n           http:\/\/www.springframework.org\/schema\/aop http:\/\/www.springframework.org\/schema\/aop\/spring-aop-2.5.xsd\">\n\n    &lt;bean id=\"sessionFactory\"\n            class=\"org.springframework.orm.hibernate3.LocalSessionFactoryBean\">\n        &lt;property name=\"configLocation\" value=\"classpath:hibernate.cfg.xml\" \/>\n        &lt;property name=\"configurationClass\" value=\"org.hibernate.cfg.AnnotationConfiguration\" \/>\n    &lt;\/bean>\n\n    &lt;!-- \u5b9a\u4e49\u4e8b\u52a1\u7ba1\u7406\u5668\uff08\u58f0\u660e\u5f0f\u7684\u4e8b\u52a1\uff09 -->\n    &lt;bean id=\"transactionManager\"\n        class=\"org.springframework.orm.hibernate3.HibernateTransactionManager\">\n        &lt;property name=\"sessionFactory\" ref=\"sessionFactory\" \/>\n    &lt;\/bean>\n\n    &lt;bean id=\"transactionInterceptor\"\n        class=\"org.springframework.transaction.interceptor.TransactionInterceptor\">\n        &lt;property name=\"transactionManager\" ref=\"transactionManager\" \/>\n        &lt;!-- \u914d\u7f6e\u4e8b\u52a1\u5c5e\u6027 -->\n        &lt;property name=\"transactionAttributes\">\n            &lt;props>\n                &lt;prop key=\"*\">PROPAGATION_REQUIRED&lt;\/prop>\n            &lt;\/props>\n        &lt;\/property>\n    &lt;\/bean>\n\n    &lt;bean class=\"org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator\">\n        &lt;property name=\"beanNames\">\n            &lt;list>\n                &lt;value>*Dao&lt;\/value>\n            &lt;\/list>\n        &lt;\/property>\n        &lt;property name=\"interceptorNames\">\n            &lt;list>\n                &lt;value>transactionInterceptor&lt;\/value>\n            &lt;\/list>\n        &lt;\/property>\n    &lt;\/bean>\n\n    &lt;!-- \u914d\u7f6eDAO -->\n    &lt;bean id=\"userDao\" class=\"com.bluesky.spring.dao.UserDaoImpl\">\n        &lt;property name=\"sessionFactory\" ref=\"sessionFactory\" \/>\n    &lt;\/bean>\n&lt;\/beans><\/code><\/pre>\n\n\n\n<p>\uff084\uff09\u4f7f\u7528tx\u6807\u7b7e\u914d\u7f6e\u7684\u62e6\u622a\u5668<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?>\n&lt;beans xmlns=\"http:\/\/www.springframework.org\/schema\/beans\"\n    xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n    xmlns:context=\"http:\/\/www.springframework.org\/schema\/context\"\n    xmlns:aop=\"http:\/\/www.springframework.org\/schema\/aop\"\n    xmlns:tx=\"http:\/\/www.springframework.org\/schema\/tx\"\n    xsi:schemaLocation=\"http:\/\/www.springframework.org\/schema\/beans\n           http:\/\/www.springframework.org\/schema\/beans\/spring-beans-2.5.xsd\n           http:\/\/www.springframework.org\/schema\/context\n           http:\/\/www.springframework.org\/schema\/context\/spring-context-2.5.xsd\n           http:\/\/www.springframework.org\/schema\/aop http:\/\/www.springframework.org\/schema\/aop\/spring-aop-2.5.xsd\n           http:\/\/www.springframework.org\/schema\/tx http:\/\/www.springframework.org\/schema\/tx\/spring-tx-2.5.xsd\">\n\n    &lt;context:annotation-config \/>\n    &lt;context:component-scan base-package=\"com.bluesky\" \/>\n\n    &lt;bean id=\"sessionFactory\"\n            class=\"org.springframework.orm.hibernate3.LocalSessionFactoryBean\">\n        &lt;property name=\"configLocation\" value=\"classpath:hibernate.cfg.xml\" \/>\n        &lt;property name=\"configurationClass\" value=\"org.hibernate.cfg.AnnotationConfiguration\" \/>\n    &lt;\/bean>\n\n    &lt;!-- \u5b9a\u4e49\u4e8b\u52a1\u7ba1\u7406\u5668\uff08\u58f0\u660e\u5f0f\u7684\u4e8b\u52a1\uff09 -->\n    &lt;bean id=\"transactionManager\"\n        class=\"org.springframework.orm.hibernate3.HibernateTransactionManager\">\n        &lt;property name=\"sessionFactory\" ref=\"sessionFactory\" \/>\n    &lt;\/bean>\n\n    &lt;tx:advice id=\"txAdvice\" transaction-manager=\"transactionManager\">\n        &lt;tx:attributes>\n            &lt;tx:method name=\"*\" propagation=\"REQUIRED\" \/>\n        &lt;\/tx:attributes>\n    &lt;\/tx:advice>\n\n    &lt;aop:config>\n        &lt;aop:pointcut id=\"interceptorPointCuts\"\n            expression=\"execution(* com.bluesky.spring.dao.*.*(..))\" \/>\n        &lt;aop:advisor advice-ref=\"txAdvice\"\n            pointcut-ref=\"interceptorPointCuts\" \/>\n    &lt;\/aop:config>\n&lt;\/beans><\/code><\/pre>\n\n\n\n<p>\uff085\uff09\u5168\u6ce8\u89e3<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?>\n&lt;beans xmlns=\"http:\/\/www.springframework.org\/schema\/beans\"\n    xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n    xmlns:context=\"http:\/\/www.springframework.org\/schema\/context\"\n    xmlns:aop=\"http:\/\/www.springframework.org\/schema\/aop\"\n    xmlns:tx=\"http:\/\/www.springframework.org\/schema\/tx\"\n    xsi:schemaLocation=\"http:\/\/www.springframework.org\/schema\/beans\n           http:\/\/www.springframework.org\/schema\/beans\/spring-beans-2.5.xsd\n           http:\/\/www.springframework.org\/schema\/context\n           http:\/\/www.springframework.org\/schema\/context\/spring-context-2.5.xsd\n           http:\/\/www.springframework.org\/schema\/aop http:\/\/www.springframework.org\/schema\/aop\/spring-aop-2.5.xsd\n           http:\/\/www.springframework.org\/schema\/tx http:\/\/www.springframework.org\/schema\/tx\/spring-tx-2.5.xsd\">\n\n    &lt;context:annotation-config \/>\n    &lt;context:component-scan base-package=\"com.bluesky\" \/>\n\n    &lt;tx:annotation-driven transaction-manager=\"transactionManager\"\/>\n\n    &lt;bean id=\"sessionFactory\"\n            class=\"org.springframework.orm.hibernate3.LocalSessionFactoryBean\">\n        &lt;property name=\"configLocation\" value=\"classpath:hibernate.cfg.xml\" \/>\n        &lt;property name=\"configurationClass\" value=\"org.hibernate.cfg.AnnotationConfiguration\" \/>\n    &lt;\/bean>\n\n    &lt;!-- \u5b9a\u4e49\u4e8b\u52a1\u7ba1\u7406\u5668\uff08\u58f0\u660e\u5f0f\u7684\u4e8b\u52a1\uff09 -->\n    &lt;bean id=\"transactionManager\"\n        class=\"org.springframework.orm.hibernate3.HibernateTransactionManager\">\n        &lt;property name=\"sessionFactory\" ref=\"sessionFactory\" \/>\n    &lt;\/bean>\n\n&lt;\/beans><\/code><\/pre>\n\n\n\n<p>\u6b64\u65f6\u5728DAO\u4e0a\u9700\u52a0\u4e0a@Transactional\u6ce8\u89e3\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.bluesky.spring.dao;\n\nimport java.util.List;\n\nimport org.hibernate.SessionFactory;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.orm.hibernate3.support.HibernateDaoSupport;\nimport org.springframework.stereotype.Component;\n\nimport com.bluesky.spring.domain.User;\n\n@Transactional\n@Component(\"userDao\")\npublic class UserDaoImpl extends HibernateDaoSupport implements UserDao {\n\n    public List&lt;User> listUsers() {\n        return this.getSession().createQuery(\"from User\").list();\n    }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"42-\u4e00\u4e2a\u58f0\u660e\u5f0f\u4e8b\u52a1\u7684\u5b9e\u4f8b\">4.2 \u4e00\u4e2a\u58f0\u660e\u5f0f\u4e8b\u52a1\u7684\u5b9e\u4f8b<\/h2>\n\n\n\n<p><em>\u6ce8\uff1a\u8be5\u5b9e\u4f8b\u53c2\u8003\u81ea<a href=\"http:\/\/www.jb51.net\/article\/57589.htm\" rel=\"noreferrer noopener\" target=\"_blank\">Spring\u4e2d\u7684\u4e8b\u52a1\u7ba1\u7406\u5b9e\u4f8b\u8be6\u89e3<\/a><\/em><\/p>\n\n\n\n<p><strong>\u9996\u5148\u662f\u6570\u636e\u5e93\u8868<\/strong>\u00a0<br\/>book(isbn, book_name, price)\u00a0<br\/>account(username, balance)\u00a0<br\/>book_stock(isbn, stock)<\/p>\n\n\n\n<p><strong>\u7136\u540e\u662fXML\u914d\u7f6e<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;beans xmlns=\"http:\/\/www.springframework.org\/schema\/beans\"\nxmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\nxmlns:context=\"http:\/\/www.springframework.org\/schema\/context\"\nxmlns:aop=\"http:\/\/www.springframework.org\/schema\/aop\"\nxmlns:tx=\"http:\/\/www.springframework.org\/schema\/tx\"\nxsi:schemaLocation=\"http:\/\/www.springframework.org\/schema\/beans\nhttp:\/\/www.springframework.org\/schema\/beans\/spring-beans-3.0.xsd\nhttp:\/\/www.springframework.org\/schema\/context\nhttp:\/\/www.springframework.org\/schema\/context\/spring-context-3.0.xsd\nhttp:\/\/www.springframework.org\/schema\/aop http:\/\/www.springframework.org\/schema\/aop\/spring-aop-2.5.xsd\nhttp:\/\/www.springframework.org\/schema\/tx http:\/\/www.springframework.org\/schema\/tx\/spring-tx-2.5.xsd\">\n\n    &lt;import resource=\"applicationContext-db.xml\" \/>\n\n    &lt;context:component-scan\n        base-package=\"com.springinaction.transaction\">\n    &lt;\/context:component-scan>\n\n    &lt;tx:annotation-driven transaction-manager=\"txManager\"\/>\n\n    &lt;bean id=\"transactionManager\" class=\"org.springframework.jdbc.datasource.DataSourceTransactionManager\">\n        &lt;property name=\"dataSource\" ref=\"dataSource\" \/>\n    &lt;\/bean>\n\n&lt;\/beans><\/code><\/pre>\n\n\n\n<p><strong>\u4f7f\u7528\u7684\u7c7b<\/strong>\u00a0<br\/>BookShopDao<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.springinaction.transaction;\n\npublic interface BookShopDao {\n    \/\/ \u6839\u636e\u4e66\u53f7\u83b7\u53d6\u4e66\u7684\u5355\u4ef7\n    public int findBookPriceByIsbn(String isbn);\n    \/\/ \u66f4\u65b0\u4e66\u7684\u5e93\u5b58\uff0c\u4f7f\u4e66\u53f7\u5bf9\u5e94\u7684\u5e93\u5b58-1\n    public void updateBookStock(String isbn);\n    \/\/ \u66f4\u65b0\u7528\u6237\u7684\u8d26\u6237\u4f59\u989d\uff1aaccount\u7684balance-price\n    public void updateUserAccount(String username, int price);\n}<\/code><\/pre>\n\n\n\n<p>BookShopDaoImpl<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.springinaction.transaction;\n\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.jdbc.core.JdbcTemplate;\nimport org.springframework.stereotype.Repository;\n\n@Repository(\"bookShopDao\")\npublic class BookShopDaoImpl implements BookShopDao {\n\n    @Autowired\n    private JdbcTemplate JdbcTemplate;\n\n    @Override\n    public int findBookPriceByIsbn(String isbn) {\n        String sql = \"SELECT price FROM book WHERE isbn = ?\";\n\n        return JdbcTemplate.queryForObject(sql, Integer.class, isbn);\n    }\n\n    @Override\n    public void updateBookStock(String isbn) {\n        \/\/\u68c0\u67e5\u4e66\u7684\u5e93\u5b58\u662f\u5426\u8db3\u591f\uff0c\u82e5\u4e0d\u591f\uff0c\u5219\u629b\u51fa\u5f02\u5e38\n        String sql2 = \"SELECT stock FROM book_stock WHERE isbn = ?\";\n        int stock = JdbcTemplate.queryForObject(sql2, Integer.class, isbn);\n        if (stock == 0) {\n            throw new BookStockException(\"\u5e93\u5b58\u4e0d\u8db3\uff01\");\n        }\n        String sql = \"UPDATE book_stock SET stock = stock - 1 WHERE isbn = ?\";\n        JdbcTemplate.update(sql, isbn);\n    }\n\n    @Override\n    public void updateUserAccount(String username, int price) {\n        \/\/\u68c0\u67e5\u4f59\u989d\u662f\u5426\u4e0d\u8db3\uff0c\u82e5\u4e0d\u8db3\uff0c\u5219\u629b\u51fa\u5f02\u5e38\n        String sql2 = \"SELECT balance FROM account WHERE username = ?\";\n        int balance = JdbcTemplate.queryForObject(sql2, Integer.class, username);\n        if (balance &lt; price) {\n            throw new UserAccountException(\"\u4f59\u989d\u4e0d\u8db3\uff01\");\n        }\n        String sql = \"UPDATE account SET balance = balance - ? WHERE username = ?\";\n        JdbcTemplate.update(sql, price, username);\n    }\n\n}<\/code><\/pre>\n\n\n\n<p>BookShopService<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.springinaction.transaction;\npublic interface BookShopService {\n     public void purchase(String username, String isbn);\n}<\/code><\/pre>\n\n\n\n<p>BookShopServiceImpl<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.springinaction.transaction;\n\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.stereotype.Service;\nimport org.springframework.transaction.annotation.Isolation;\nimport org.springframework.transaction.annotation.Propagation;\nimport org.springframework.transaction.annotation.Transactional;\n\n@Service(\"bookShopService\")\npublic class BookShopServiceImpl implements BookShopService {\n\n    @Autowired\n    private BookShopDao bookShopDao;\n\n    \/**\n     * 1.\u6dfb\u52a0\u4e8b\u52a1\u6ce8\u89e3\n     * \u4f7f\u7528propagation \u6307\u5b9a\u4e8b\u52a1\u7684\u4f20\u64ad\u884c\u4e3a\uff0c\u5373\u5f53\u524d\u7684\u4e8b\u52a1\u65b9\u6cd5\u88ab\u53e6\u5916\u4e00\u4e2a\u4e8b\u52a1\u65b9\u6cd5\u8c03\u7528\u65f6\u5982\u4f55\u4f7f\u7528\u4e8b\u52a1\u3002\n     * \u9ed8\u8ba4\u53d6\u503c\u4e3aREQUIRED\uff0c\u5373\u4f7f\u7528\u8c03\u7528\u65b9\u6cd5\u7684\u4e8b\u52a1\n     * REQUIRES_NEW\uff1a\u4f7f\u7528\u81ea\u5df1\u7684\u4e8b\u52a1\uff0c\u8c03\u7528\u7684\u4e8b\u52a1\u65b9\u6cd5\u7684\u4e8b\u52a1\u88ab\u6302\u8d77\u3002\n     *\n     * 2.\u4f7f\u7528isolation \u6307\u5b9a\u4e8b\u52a1\u7684\u9694\u79bb\u7ea7\u522b\uff0c\u6700\u5e38\u7528\u7684\u53d6\u503c\u4e3aREAD_COMMITTED\n     * 3.\u9ed8\u8ba4\u60c5\u51b5\u4e0b Spring \u7684\u58f0\u660e\u5f0f\u4e8b\u52a1\u5bf9\u6240\u6709\u7684\u8fd0\u884c\u65f6\u5f02\u5e38\u8fdb\u884c\u56de\u6eda\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5bf9\u5e94\u7684\u5c5e\u6027\u8fdb\u884c\u8bbe\u7f6e\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u9ed8\u8ba4\u503c\u5373\u53ef\u3002\n     * 4.\u4f7f\u7528readOnly \u6307\u5b9a\u4e8b\u52a1\u662f\u5426\u4e3a\u53ea\u8bfb\u3002 \u8868\u793a\u8fd9\u4e2a\u4e8b\u52a1\u53ea\u8bfb\u53d6\u6570\u636e\u4f46\u4e0d\u66f4\u65b0\u6570\u636e\uff0c\u8fd9\u6837\u53ef\u4ee5\u5e2e\u52a9\u6570\u636e\u5e93\u5f15\u64ce\u4f18\u5316\u4e8b\u52a1\u3002\u82e5\u771f\u7684\u662f\u4e00\u4e2a\u53ea\u8bfb\u53d6\u6570\u636e\u5e93\u503c\u5f97\u65b9\u6cd5\uff0c\u5e94\u8bbe\u7f6ereadOnly=true\n     * 5.\u4f7f\u7528timeOut \u6307\u5b9a\u5f3a\u5236\u56de\u6eda\u4e4b\u524d\u4e8b\u52a1\u53ef\u4ee5\u5360\u7528\u7684\u65f6\u95f4\u3002\n     *\/\n    @Transactional(propagation=Propagation.REQUIRES_NEW,\n            isolation=Isolation.READ_COMMITTED,\n            noRollbackFor={UserAccountException.class},\n            readOnly=true, timeout=3)\n    @Override\n    public void purchase(String username, String isbn) {\n        \/\/1.\u83b7\u53d6\u4e66\u7684\u5355\u4ef7\n        int price = bookShopDao.findBookPriceByIsbn(isbn);\n        \/\/2.\u66f4\u65b0\u4e66\u7684\u5e93\u5b58\n        bookShopDao.updateBookStock(isbn);\n        \/\/3.\u66f4\u65b0\u7528\u6237\u4f59\u989d\n        bookShopDao.updateUserAccount(username, price);\n    }\n}<\/code><\/pre>\n\n\n\n<p>Cashier<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.springinaction.transaction;\nimport java.util.List;\npublic interface Cashier {\n    public void checkout(String username, List&lt;String>isbns);\n}<\/code><\/pre>\n\n\n\n<p>CashierImpl\uff1aCashierImpl.checkout\u548cbookShopService.purchase\u8054\u5408\u6d4b\u8bd5\u4e86\u4e8b\u52a1\u7684\u4f20\u64ad\u884c\u4e3a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.springinaction.transaction;\n\nimport java.util.List;\n\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.stereotype.Service;\nimport org.springframework.transaction.annotation.Transactional;\n\n@Service(\"cashier\")\npublic class CashierImpl implements Cashier {\n    @Autowired\n    private BookShopService bookShopService;\n\n    @Transactional\n    @Override\n    public void checkout(String username, List&lt;String> isbns) {\n        for(String isbn : isbns) {\n            bookShopService.purchase(username, isbn);\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>BookStockException<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\npackage com.springinaction.transaction;\npublic class BookStockException extends RuntimeException {\n\n    private static final long serialVersionUID = 1L;\n\n    public BookStockException() {\n        super();\n        \/\/ TODO Auto-generated constructor stub\n    }\n\n    public BookStockException(String arg0, Throwable arg1, boolean arg2,\n            boolean arg3) {\n        super(arg0, arg1, arg2, arg3);\n        \/\/ TODO Auto-generated constructor stub\n    }\n\n    public BookStockException(String arg0, Throwable arg1) {\n        super(arg0, arg1);\n        \/\/ TODO Auto-generated constructor stub\n    }\n\n    public BookStockException(String arg0) {\n        super(arg0);\n        \/\/ TODO Auto-generated constructor stub\n    }\n\n    public BookStockException(Throwable arg0) {\n        super(arg0);\n        \/\/ TODO Auto-generated constructor stub\n    }\n}<\/code><\/pre>\n\n\n\n<p>UserAccountException<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.springinaction.transaction;\npublic class UserAccountException extends RuntimeException {\n\n    private static final long serialVersionUID = 1L;\n\n    public UserAccountException() {\n        super();\n        \/\/ TODO Auto-generated constructor stub\n    }\n\n    public UserAccountException(String arg0, Throwable arg1, boolean arg2,\n            boolean arg3) {\n        super(arg0, arg1, arg2, arg3);\n        \/\/ TODO Auto-generated constructor stub\n    }\n\n    public UserAccountException(String arg0, Throwable arg1) {\n        super(arg0, arg1);\n        \/\/ TODO Auto-generated constructor stub\n    }\n\n    public UserAccountException(String arg0) {\n        super(arg0);\n        \/\/ TODO Auto-generated constructor stub\n    }\n\n    public UserAccountException(Throwable arg0) {\n        super(arg0);\n        \/\/ TODO Auto-generated constructor stub\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u6d4b\u8bd5\u7c7b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.springinaction.transaction;\n\nimport java.util.Arrays;\n\nimport org.junit.Test;\nimport org.springframework.context.ApplicationContext;\nimport org.springframework.context.support.ClassPathXmlApplicationContext;\n\npublic class SpringTransitionTest {\n\n    private ApplicationContext ctx = null;\n    private BookShopDao bookShopDao = null;\n    private BookShopService bookShopService = null;\n    private Cashier cashier = null;\n    {\n        ctx = new ClassPathXmlApplicationContext(\"config\/transaction.xml\");\n        bookShopDao = ctx.getBean(BookShopDao.class);\n        bookShopService = ctx.getBean(BookShopService.class);\n        cashier = ctx.getBean(Cashier.class);\n    }\n\n    @Test\n    public void testBookShopDaoFindPriceByIsbn() {\n        System.out.println(bookShopDao.findBookPriceByIsbn(\"1001\"));\n    }\n\n    @Test\n    public void testBookShopDaoUpdateBookStock(){\n        bookShopDao.updateBookStock(\"1001\");\n    }\n\n    @Test\n    public void testBookShopDaoUpdateUserAccount(){\n        bookShopDao.updateUserAccount(\"AA\", 100);\n    }\n    @Test\n    public void testBookShopService(){\n        bookShopService.purchase(\"AA\", \"1001\");\n    }\n\n    @Test\n    public void testTransactionPropagation(){\n        cashier.checkout(\"AA\", Arrays.asList(\"1001\", \"1002\"));\n    }\n}<\/code><\/pre>\n\n\n\n<p><em>OVER<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5199\u8fd9\u7bc7\u535a\u5ba2\u4e4b\u524d\u6211\u9996\u5148\u8bfb\u4e86\u300aSpring in action\u300b\uff0c\u4e4b\u540e\u5728\u7f51\u4e0a\u770b\u4e86\u4e00\u4e9b\u5173\u4e8eSpring\u4e8b\u52a1\u7ba1\u7406\u7684\u6587\u7ae0\uff0c\u611f\u89c9\u90fd\u6ca1\u6709\u8bb2\u5168\uff0c\u8fd9\u91cc\u5c31\u5c06\u4e66\u4e0a\u7684\u548c\u7f51\u4e0a\u5173\u4e8e\u4e8b\u52a1\u7684\u77e5\u8bc6\u603b\u7ed3\u4e00\u4e0b\uff0c\u53c2\u8003\u7684\u6587\u7ae0\u5982\u4e0b\uff1a Spring\u4e8b\u52a1\u673a\u5236\u8be6\u89e3Spring\u4e8b\u52a1\u914d\u7f6e\u7684\u4e94\u79cd\u65b9\u5f0fSpring\u4e2d\u7684\u4e8b\u52a1\u7ba1\u7406\u5b9e\u4f8b\u8be6\u89e3 1 \u521d\u6b65\u7406\u89e3 \u7406\u89e3\u4e8b\u52a1\u4e4b\u524d\uff0c\u5148\u8bb2\u4e00\u4e2a\u4f60\u65e5\u5e38\u751f\u6d3b\u4e2d\u6700\u5e38\u5e72\u7684\u4e8b\uff1a\u53d6\u94b1\u3002\u00a0\u6bd4\u5982\u4f60\u53bbATM\u673a\u53d61000\u5757\u94b1\uff0c\u5927\u4f53\u6709\u4e24\u4e2a\u6b65\u9aa4\uff1a\u9996\u5148\u8f93\u5165\u5bc6\u7801\u91d1\u989d\uff0c\u94f6\u884c\u5361\u6263\u63891000\u5143\u94b1\uff1b\u7136\u540eATM\u51fa1000\u5143\u94b1\u3002\u8fd9\u4e24\u4e2a\u6b65\u9aa4\u5fc5\u987b\u662f\u8981\u4e48\u90fd\u6267\u884c\u8981\u4e48\u90fd\u4e0d\u6267\u884c\u3002\u5982\u679c\u94f6\u884c\u5361\u6263\u9664\u4e861000\u5757\u4f46\u662fATM\u51fa\u94b1\u5931\u8d25\u7684\u8bdd\uff0c\u4f60\u5c06\u4f1a\u635f\u59311000\u5143\uff1b\u5982\u679c\u94f6\u884c\u5361\u6263\u94b1\u5931\u8d25\u4f46\u662fATM\u5374\u51fa\u4e861000\u5757\uff0c\u90a3\u4e48\u94f6\u884c\u5c06\u635f\u59311000\u5143\u3002\u6240\u4ee5\uff0c\u5982\u679c\u4e00\u4e2a\u6b65\u9aa4\u6210\u529f\u53e6\u4e00\u4e2a\u6b65\u9aa4\u5931\u8d25\u5bf9\u53cc\u65b9\u90fd\u4e0d\u662f\u597d\u4e8b\uff0c\u5982\u679c\u4e0d\u7ba1\u54ea\u4e00\u4e2a\u6b65\u9aa4\u5931\u8d25\u4e86\u4ee5\u540e\uff0c\u6574\u4e2a\u53d6\u94b1\u8fc7\u7a0b\u90fd\u80fd\u56de\u6eda\uff0c\u4e5f\u5c31\u662f\u5b8c\u5168\u53d6\u6d88\u6240\u6709\u64cd\u4f5c\u7684\u8bdd\uff0c\u8fd9\u5bf9\u53cc\u65b9\u90fd\u662f\u6781\u597d\u7684\u3002\u00a0\u4e8b\u52a1\u5c31\u662f\u7528\u6765\u89e3\u51b3\u7c7b\u4f3c\u95ee\u9898\u7684\u3002\u4e8b\u52a1\u662f\u4e00\u7cfb\u5217\u7684\u52a8\u4f5c\uff0c\u5b83\u4eec\u7efc\u5408\u5728\u4e00\u8d77\u624d\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u5de5\u4f5c\u5355\u5143\uff0c\u8fd9\u4e9b\u52a8\u4f5c\u5fc5\u987b\u5168\u90e8\u5b8c\u6210\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u5931\u8d25\u7684\u8bdd\uff0c\u90a3\u4e48\u4e8b\u52a1\u5c31\u4f1a\u56de\u6eda\u5230\u6700\u5f00\u59cb\u7684\u72b6\u6001\uff0c\u4eff\u4f5b\u4ec0\u4e48\u90fd\u6ca1\u53d1\u751f\u8fc7\u4e00\u6837\u3002\u00a0\u5728\u4f01\u4e1a\u7ea7\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u4e2d\uff0c\u4e8b\u52a1\u7ba1\u7406\u5fc5\u4e0d\u53ef\u5c11\u7684\u6280\u672f\uff0c\u7528\u6765\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\u3002\u00a0\u4e8b\u52a1\u6709\u56db\u4e2a\u7279\u6027\uff1aACID \u539f\u5b50\u6027\uff08Atomicity\uff09\uff1a\u4e8b\u52a1\u662f\u4e00\u4e2a\u539f\u5b50\u64cd\u4f5c\uff0c\u7531\u4e00\u7cfb\u5217\u52a8\u4f5c\u7ec4\u6210\u3002\u4e8b\u52a1\u7684\u539f\u5b50\u6027\u786e\u4fdd\u52a8\u4f5c\u8981\u4e48\u5168\u90e8\u5b8c\u6210\uff0c\u8981\u4e48\u5b8c\u5168\u4e0d\u8d77\u4f5c\u7528\u3002\u4e00\u81f4\u6027\uff08Consistency\uff09\uff1a\u4e00\u65e6\u4e8b\u52a1\u5b8c\u6210\uff08\u4e0d\u7ba1\u6210\u529f\u8fd8\u662f\u5931\u8d25\uff09\uff0c\u7cfb\u7edf\u5fc5\u987b\u786e\u4fdd\u5b83\u6240\u5efa\u6a21\u7684\u4e1a\u52a1\u5904\u4e8e\u4e00\u81f4\u7684\u72b6\u6001\uff0c\u800c\u4e0d\u4f1a\u662f\u90e8\u5206\u5b8c\u6210\u90e8\u5206\u5931\u8d25\u3002\u5728\u73b0\u5b9e\u4e2d\u7684\u6570\u636e\u4e0d\u5e94\u8be5\u88ab\u7834\u574f\u3002\u9694\u79bb\u6027\uff08Isolation\uff09\uff1a\u53ef\u80fd\u6709\u8bb8\u591a\u4e8b\u52a1\u4f1a\u540c\u65f6\u5904\u7406\u76f8\u540c\u7684\u6570\u636e\uff0c\u56e0\u6b64\u6bcf\u4e2a\u4e8b\u52a1\u90fd\u5e94\u8be5\u4e0e\u5176\u4ed6\u4e8b\u52a1\u9694\u79bb\u5f00\u6765\uff0c\u9632\u6b62\u6570\u636e\u635f\u574f\u3002\u6301\u4e45\u6027\uff08Durability\uff09\uff1a\u4e00\u65e6\u4e8b\u52a1\u5b8c\u6210\uff0c\u65e0\u8bba\u53d1\u751f\u4ec0\u4e48\u7cfb\u7edf\u9519\u8bef\uff0c\u5b83\u7684\u7ed3\u679c\u90fd\u4e0d\u5e94\u8be5\u53d7\u5230\u5f71\u54cd\uff0c\u8fd9\u6837\u5c31\u80fd\u4ece\u4efb\u4f55\u7cfb\u7edf\u5d29\u6e83\u4e2d\u6062\u590d\u8fc7\u6765\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u4e8b\u52a1\u7684\u7ed3\u679c\u88ab\u5199\u5230\u6301\u4e45\u5316\u5b58\u50a8\u5668\u4e2d\u3002 2 \u6838\u5fc3\u63a5\u53e3 Spring\u4e8b\u52a1\u7ba1\u7406\u7684\u5b9e\u73b0\u6709\u8bb8\u591a\u7ec6\u8282\uff0c\u5982\u679c\u5bf9\u6574\u4e2a\u63a5\u53e3\u6846\u67b6\u6709\u4e2a\u5927\u4f53\u4e86\u89e3\u4f1a\u975e\u5e38\u6709\u5229\u4e8e\u6211\u4eec\u7406\u89e3\u4e8b\u52a1\uff0c\u4e0b\u9762\u901a\u8fc7\u8bb2\u89e3Spring\u7684\u4e8b\u52a1\u63a5\u53e3\u6765\u4e86\u89e3Spring\u5b9e\u73b0\u4e8b\u52a1\u7684\u5177\u4f53\u7b56\u7565\u3002\u00a0Spring\u4e8b\u52a1\u7ba1\u7406\u6d89\u53ca\u7684\u63a5\u53e3\u7684\u8054\u7cfb\u5982\u4e0b\uff1a 2.1 \u4e8b\u52a1\u7ba1\u7406\u5668 Spring\u5e76\u4e0d\u76f4\u63a5\u7ba1\u7406\u4e8b\u52a1\uff0c\u800c\u662f\u63d0\u4f9b\u4e86\u591a\u79cd\u4e8b\u52a1\u7ba1\u7406\u5668\uff0c\u4ed6\u4eec\u5c06\u4e8b\u52a1\u7ba1\u7406\u7684\u804c\u8d23\u59d4\u6258\u7ed9Hibernate\u6216\u8005JTA\u7b49\u6301\u4e45\u5316\u673a\u5236\u6240\u63d0\u4f9b\u7684\u76f8\u5173\u5e73\u53f0\u6846\u67b6\u7684\u4e8b\u52a1\u6765\u5b9e\u73b0\u3002\u00a0Spring\u4e8b\u52a1\u7ba1\u7406\u5668\u7684\u63a5\u53e3\u662forg.springframework.transaction.PlatformTransactionManager\uff0c\u901a\u8fc7\u8fd9\u4e2a\u63a5\u53e3\uff0cSpring\u4e3a\u5404\u4e2a\u5e73\u53f0\u5982JDBC\u3001Hibernate\u7b49\u90fd\u63d0\u4f9b\u4e86\u5bf9\u5e94\u7684\u4e8b\u52a1\u7ba1\u7406\u5668\uff0c\u4f46\u662f\u5177\u4f53\u7684\u5b9e\u73b0\u5c31\u662f\u5404\u4e2a\u5e73\u53f0\u81ea\u5df1\u7684\u4e8b\u60c5\u4e86\u3002\u6b64\u63a5\u53e3\u7684\u5185\u5bb9\u5982\u4e0b\uff1a \u4ece\u8fd9\u91cc\u53ef\u77e5\u5177\u4f53\u7684\u5177\u4f53\u7684\u4e8b\u52a1\u7ba1\u7406\u673a\u5236\u5bf9Spring\u6765\u8bf4\u662f\u900f\u660e\u7684\uff0c\u5b83\u5e76\u4e0d\u5173\u5fc3\u90a3\u4e9b\uff0c\u90a3\u4e9b\u662f\u5bf9\u5e94\u5404\u4e2a\u5e73\u53f0\u9700\u8981\u5173\u5fc3\u7684\uff0c\u6240\u4ee5Spring\u4e8b\u52a1\u7ba1\u7406\u7684\u4e00\u4e2a\u4f18\u70b9\u5c31\u662f\u4e3a\u4e0d\u540c\u7684\u4e8b\u52a1API\u63d0\u4f9b\u4e00\u81f4\u7684\u7f16\u7a0b\u6a21\u578b\uff0c\u5982JTA\u3001JDBC\u3001Hibernate\u3001JPA\u3002\u4e0b\u9762\u5206\u522b\u4ecb\u7ecd\u5404\u4e2a\u5e73\u53f0\u6846\u67b6\u5b9e\u73b0\u4e8b\u52a1\u7ba1\u7406\u7684\u673a\u5236\u3002 2.1.1 JDBC\u4e8b\u52a1 \u5982\u679c\u5e94\u7528\u7a0b\u5e8f\u4e2d\u76f4\u63a5\u4f7f\u7528JDBC\u6765\u8fdb\u884c\u6301\u4e45\u5316\uff0cDataSourceTransactionManager\u4f1a\u4e3a\u4f60\u5904\u7406\u4e8b\u52a1\u8fb9\u754c\u3002\u4e3a\u4e86\u4f7f\u7528DataSourceTransactionManager\uff0c\u4f60\u9700\u8981\u4f7f\u7528\u5982\u4e0b\u7684XML\u5c06\u5176\u88c5\u914d\u5230\u5e94\u7528\u7a0b\u5e8f\u7684\u4e0a\u4e0b\u6587\u5b9a\u4e49\u4e2d\uff1a \u5b9e\u9645\u4e0a\uff0cDataSourceTransactionManager\u662f\u901a\u8fc7\u8c03\u7528java.sql.Connection\u6765\u7ba1\u7406\u4e8b\u52a1\uff0c\u800c\u540e\u8005\u662f\u901a\u8fc7DataSource\u83b7\u53d6\u5230\u7684\u3002\u901a\u8fc7\u8c03\u7528\u8fde\u63a5\u7684commit()\u65b9\u6cd5\u6765\u63d0\u4ea4\u4e8b\u52a1\uff0c\u540c\u6837\uff0c\u4e8b\u52a1\u5931\u8d25\u5219\u901a\u8fc7\u8c03\u7528rollback()\u65b9\u6cd5\u8fdb\u884c\u56de\u6eda\u3002 2.1.2 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[1],"tags":[],"class_list":["post-935","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/posts\/935","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=935"}],"version-history":[{"count":0,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/posts\/935\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=935"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=935"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=935"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}