{"id":7171,"date":"2024-04-13T09:01:02","date_gmt":"2024-04-13T01:01:02","guid":{"rendered":""},"modified":"2024-04-13T09:01:02","modified_gmt":"2024-04-13T01:01:02","slug":"Java-\u4e8b\u52a1JTA","status":"publish","type":"post","link":"https:\/\/mushiming.com\/7171.html","title":{"rendered":"Java-\u4e8b\u52a1JTA"},"content":{"rendered":"

\n <\/path> \n<\/svg> <\/p>\n

\u7b80\u4ecb<\/h4>\n

\u2003\u2003JTA\u662f\u4e00\u4e2a\u5206\u5e03\u5f0f\u6570\u636e\u5e93\u4e8b\u52a1\u7684Java API\u89c4\u8303\uff0c\u5b9e\u73b0\u4e86\u8be5API\u7684Trans cationManager\u652f\u6301\u5206\u5e03\u5f0f\u4e8b\u52a1 -\u5168\u5c40\u4e8b\u52a1\u3002<\/strong>
\u2003\u2003\u89e3\u51b3\u4e86\u591a\u6570\u636e\u5e93\u5171\u540c\u5b8c\u6210\u4e00\u4e2a\u5de5\u4f5c\u65f6\uff0c\u56e0\u4e0d\u77e5\u9053\u5176\u5b83\u6570\u636e\u5e93\u6267\u884c\u72b6\u51b5\uff0c\u800c\u65e0\u6cd5\u9009\u62e9\u63d0\u4ea4\u6216\u56de\u6eda\u7684\u95ee\u9898\u3002<\/strong><\/p>\n

JAT\u7684\u5b9e\u73b0\uff08\u7531j2ee\u670d\u52a1\u5668\u63d0\u4f9b\u5546\u6839\u636eJTS\u89c4\u8303\u63d0\u4f9b\uff09<\/h4>\n
    \n
  • J2EE\u5bb9\u5668\u6240\u63d0\u4f9b\u7684JTA\u5b9e\u73b0\uff08JBoss\uff09\u3002<\/li>\n
  • \u72ec\u7acb\u7684JTA\u5b9e\u73b0:\u5982JOTM\uff0cAtomikos.\u8fd9\u4e9b\u5b9e\u73b0\u53ef\u4ee5\u5e94\u7528\u5728\u90a3\u4e9b\u4e0d\u4f7f\u7528J2EE\u5e94\u7528\u670d\u52a1\u5668\u7684\u73af\u5883\u91cc\u7528\u4ee5\u63d0\u4f9b\u5206\u5e03\u4e8b\u4e8b\u52a1\u4fdd\u8bc1\u3002\u5982Tomcat,Jetty\u4ee5\u53ca\u666e\u901a\u7684java\u5e94\u7528\u3002<\/li>\n<\/ul>\n

    XA\u89c4\u8303<\/h4>\n

    \u2003\u2003\u5e95\u5c42\u6570\u636e\u5e93\u652f\u6301\u5206\u5e03\u5f0f\u4e8b\u52a1\u7ba1\u7406\uff0c\u8be5\u6570\u636e\u5e93\u7684\u9a71\u52a8\u7a0b\u5e8f\u5fc5\u987b\u5b9e\u73b03\u4e2a\u89c4\u8303\uff1a<\/strong><\/p>\n

      \n
    • javax.sql.XADataSource \u63a5\u53e3\uff1a<\/strong> XAConnection\u5bf9\u8c61\u7684\u5de5\u5382\u7c7b<\/li>\n
    • javax.sql.XAConnection \u63a5\u53e3\uff1a<\/strong> \u6570\u636e\u8d44\u6e90\u7684\u8fde\u63a5\u529f\u80fd<\/li>\n
    • javax.sql.XAResource \u63a5\u53e3\uff1a<\/strong> \u6570\u636e\u8d44\u6e90\uff1a\u6570\u636e\u5e93\u6216\u5176\u4ed6\u8d44\u6e90<\/li>\n<\/ul>\n

      DTP\u6a21\u578b<\/h4>\n

      \u2003\u2003\u5373\u5206\u5e03\u5f0f\u4e8b\u52a1\u5904\u7406\u7684\u6a21\u578b\u89c4\u8303\uff0c\u6a21\u578b\u5206\u4e3a\u56db\u90e8\u5206\uff1a<\/strong><\/p>\n

        \n
      • \u5e94\u7528\u7a0b\u5e8fAP\uff1a<\/strong> \u4f7f\u7528\u4e8b\u52a1\u7684\u5e94\u7528<\/li>\n
      • \u4e8b\u52a1\u7ba1\u7406\u5668TM\uff1a<\/strong> \u7531\u6570\u636e\u5e93\u8fde\u63a5\u6846\u67b6(Mybatis\u3001Hibernate\u2026)\u6216JDBC\u63d0\u4f9b\u5b9e\u73b0JTA\u7684\u7ba1\u7406\u5668<\/li>\n
      • \u8d44\u6e90\u7ba1\u7406\u5668RM\uff1a<\/strong> \u6570\u636e\u5e93\uff0c\u901a\u5e38\u4e3a\u6570\u636e\u6e90\u3002JDBC\u6216\u7b2c\u4e09\u65b9\u63d0\u4f9b\u7684datasource<\/li>\n
      • \u901a\u4fe1\u8d44\u6e90\u7ba1\u7406\u5668CRM\uff1a<\/strong> \u6d88\u606f\u4e2d\u95f4\u4ef6<\/li>\n<\/ul>\n

        JTA\u7684\u4f7f\u7528<\/h4>\n

        JTA\u91cc\u9762\u63d0\u4f9b\u4e86 java.transaction.UserTransaction ,\u91cc\u9762\u5b9a\u4e49\u4e86\u4e0b\u9762\u51e0\u4e2a\u65b9\u6cd5
        begin\uff1a\u5f00\u542f\u4e00\u4e2a\u4e8b\u52a1
        commit\uff1a\u63d0\u4ea4\u5f53\u524d\u4e8b\u52a1
        rollback\uff1a\u56de\u6eda\u5f53\u524d\u4e8b\u52a1
        setRollbackOnly\uff1a\u628a\u5f53\u524d\u4e8b\u52a1\u6807\u8bb0\u4e3a\u56de\u6eda
        setTransactionTimeout\uff1a\u8bbe\u7f6e\u4e8b\u52a1\u7684\u4e8b\u4ef6\uff0c\u8d85\u8fc7\u8fd9\u4e2a\u4e8b\u4ef6\uff0c\u5c31\u629b\u51fa\u5f02\u5e38\uff0c\u56de\u6eda\u4e8b\u52a1<\/p>\n

        public<\/span> void<\/span> JtaTransfer<\/span>(<\/span>)<\/span> { \n   <\/span> \n        javax.<\/span>transaction.<\/span>UserTransaction tx =<\/span> null;<\/span>\n        java.<\/span>sql.<\/span>Connection conn =<\/span> null;<\/span>\n         try<\/span>{ \n   <\/span> \n             tx =<\/span> (<\/span>javax.<\/span>transaction.<\/span>UserTransaction)<\/span> context.<\/span>lookup<\/span>(<\/span>\"java:comp\/UserTransaction\"<\/span>)<\/span>;<\/span>  \/\/\u53d6\u5f97JTA\u4e8b\u52a1\uff0c\u672c\u4f8b\u4e2d\u662f\u7531Jboss\u5bb9\u5668\u7ba1\u7406<\/span>\n             javax.<\/span>sql.<\/span>DataSource ds =<\/span> (<\/span>javax.<\/span>sql.<\/span>DataSource)<\/span> context.<\/span>lookup<\/span>(<\/span>\"java:\/XAOracleDS\"<\/span>)<\/span>;<\/span>  \/\/\u53d6\u5f97\u6570\u636e\u5e93\u8fde\u63a5\u6c60\uff0c\u5fc5\u987b\u6709\u652f\u6301XA\u7684\u6570\u636e\u5e93\u3001\u9a71\u52a8\u7a0b\u5e8f <\/span>\n             tx.<\/span>begin<\/span>(<\/span>)<\/span>;<\/span>\n            conn =<\/span> ds.<\/span>getConnection<\/span>(<\/span>)<\/span>;<\/span>\n\n             \/\/ \u5c06\u81ea\u52a8\u63d0\u4ea4\u8bbe\u7f6e\u4e3a false\uff0c<\/span>\n             \/\/\u82e5\u8bbe\u7f6e\u4e3a true \u5219\u6570\u636e\u5e93\u5c06\u4f1a\u628a\u6bcf\u4e00\u6b21\u6570\u636e\u66f4\u65b0\u8ba4\u5b9a\u4e3a\u4e00\u4e2a\u4e8b\u52a1\u5e76\u81ea\u52a8\u63d0\u4ea4<\/span>\n             conn.<\/span>setAutoCommit<\/span>(<\/span>false<\/span>)<\/span>;<\/span>\n\n             stmt =<\/span> conn.<\/span>createStatement<\/span>(<\/span>)<\/span>;<\/span> \n             \/\/ \u5c06 A \u8d26\u6237\u4e2d\u7684\u91d1\u989d\u51cf\u5c11 500 <\/span>\n             stmt.<\/span>execute<\/span>(<\/span>\"\\ update t_account set amount = amount - 500 where account_id = 'A'\"<\/span>)<\/span>;<\/span>\n             \/\/ \u5c06 B \u8d26\u6237\u4e2d\u7684\u91d1\u989d\u589e\u52a0 500 <\/span>\n             stmt.<\/span>execute<\/span>(<\/span>\"\\ update t_account set amount = amount + 500 where account_id = 'B'\"<\/span>)<\/span>;<\/span>\n\n             \/\/ \u63d0\u4ea4\u4e8b\u52a1<\/span>\n             tx.<\/span>commit<\/span>(<\/span>)<\/span>;<\/span>\n             \/\/ \u4e8b\u52a1\u63d0\u4ea4\uff1a\u8f6c\u8d26\u7684\u4e24\u6b65\u64cd\u4f5c\u540c\u65f6\u6210\u529f<\/span>\n         }<\/span> catch<\/span>(<\/span>SQLException sqle)<\/span>{ \n   <\/span>            \n             try<\/span>{ \n   <\/span> \n                 \/\/ \u53d1\u751f\u5f02\u5e38\uff0c\u56de\u6eda\u5728\u672c\u4e8b\u52a1\u4e2d\u7684\u64cd\u505a<\/span>\n              tx.<\/span>rollback<\/span>(<\/span>)<\/span>;<\/span>\n                 \/\/ \u4e8b\u52a1\u56de\u6eda\uff1a\u8f6c\u8d26\u7684\u4e24\u6b65\u64cd\u4f5c\u5b8c\u5168\u64a4\u9500<\/span>\n                 stmt.<\/span>close<\/span>(<\/span>)<\/span>;<\/span> \n                 conn.<\/span>close<\/span>(<\/span>)<\/span>;<\/span> \n             }<\/span>catch<\/span>(<\/span>Exception ignore)<\/span>{ \n   <\/span> \n\n             }<\/span> \n             sqle.<\/span>printStackTrace<\/span>(<\/span>)<\/span>;<\/span> \n         }<\/span> \n     }<\/span>\n<\/code><\/pre>\n
        \n

        \u2003\u4e0a\u9762\u7684\u4f8b\u5b50\u5c31\u662f\u4e00\u4e2a\u4f7f\u7528JTA\u4e8b\u52a1\u7684\u8f6c\u8d26\u64cd\u4f5c\uff0c\u8be5\u64cd\u4f5c\u76f8\u5bf9\u4f9d\u8d56\u4e8eJ2EE\u5bb9\u5668\uff0c\u5e76\u4e14\u9700\u8981\u901a\u8fc7JNDI\u7684\u65b9\u5f0f\u83b7\u53d6UserTransaction\u548cConnection\u3002
        \u2003\u4e0a\u9762\u7684\u4f8b\u5b50\u5c31\u662f\u4e00\u4e2a\u4f7f\u7528JTA\u4e8b\u52a1\u7684\u8f6c\u8d26\u64cd\u4f5c\uff0c\u8be5\u64cd\u4f5c\u76f8\u5bf9\u4f9d\u8d56\u4e8eJ2EE\u5bb9\u5668\uff0c\u5e76\u4e14\u9700\u8981\u901a\u8fc7JNDI\u7684\u65b9\u5f0f\u83b7\u53d6UserTransaction\u548cConnection\u3002<\/p>\n<\/blockquote>\n

        \u5b66\u4e60\u94fe\u63a5<\/p>\n","protected":false},"excerpt":{"rendered":"Java-\u4e8b\u52a1JTA\u7b80\u4ecb\u2003\u2003JTA\u662f\u4e00\u4e2a\u5206\u5e03\u5f0f\u6570\u636e\u5e93\u4e8b\u52a1\u7684JavaAPI\u89c4\u8303\uff0c\u5b9e\u73b0\u4e86\u8be5API\u7684TranscationManager\u652f\u6301\u5206\u5e03\u5f0f\u4e8b\u52a1-\u5168\u5c40\u4e8b\u52a1","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/7171"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=7171"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/7171\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=7171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=7171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=7171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}