{"id":123,"date":"2009-07-29T10:34:47","date_gmt":"2009-07-29T02:34:47","guid":{"rendered":"http:\/\/www.genepeng.com\/?p=123"},"modified":"2009-08-13T23:02:13","modified_gmt":"2009-08-13T15:02:13","slug":"perl-dbi-%e4%ba%8b%e5%8a%a1%e6%94%af%e6%8c%81","status":"publish","type":"post","link":"https:\/\/www.genepeng.com\/index.php\/123","title":{"rendered":"perl DBI \u4e8b\u52a1\u652f\u6301"},"content":{"rendered":"<p>\u548c\u5176\u5b83\u7684\u8bed\u8a00\u4e00\u6837, perl DBI\u5bf9\u6570\u636e\u5e93\u7684\u64cd\u4f5c\u4e5f\u652f\u6301\u4e8b\u52a1\u5904\u7406\uff0c \u5b83\u7684\u5b9e\u73b0\u65b9\u5f0f\u6709\u4e24\u4e2a\uff1a<\/p>\n<p>\u4e00\uff1a \u5728\u8fde\u63a5\u6570\u636e\u5e93\u7684\u65f6\u5019\u5c31\u5f00\u59cb\u4e00\u4e2a\u4e8b\u52a1<\/p>\n<p>$dbh = DBI-&gt;connect($dataSource, $user, $passwd, {AutoCommit =&gt; 0});<\/p>\n<p>\u53ef\u4ee5\u770b\u5230\u5728\u8fde\u63a5\u7684\u65f6\u5019\u8bbe\u7f6e\u4e86AutoCommit\u4e3afalse\uff0c \u4e5f\u5c31\u662f\u8bf4\u5f53\u4f60\u5bf9\u6570\u636e\u5e93\u8fdb\u884c\u66f4\u65b0\u64cd\u4f5c\u7684\u65f6\u5019\uff0c \u5b83\u4e0d\u4f1a\u81ea\u52a8\u5730\u628a\u90a3\u4e9b\u66f4\u65b0\u76f4\u63a5\u5199\u5230\u6570\u636e\u5e93\u91cc\uff0c \u800c\u662f\u8981\u7a0b\u5e8f\u901a\u8fc7  $dbh-&gt;commit\u6765\u4f7f\u6570\u636e\u771f\u6b63\u5730\u5199\u5230\u6570\u636e\u5e93\u91cc\uff0c \u6216  $dbh-&gt;rollback \u6765\u56de\u6eda\u521a\u624d\u7684\u64cd\u4f5c<\/p>\n<p>\u4e8c\uff1a\u901a\u8fc7$dbh-&gt;begin_work()\u8bed\u53e5\u6765\u5f00\u59cb\u4e00\u4e2a\u4e8b\u52a1<\/p>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u5c31\u4e0d\u9700\u8981\u5728\u8fde\u63a5\u6570\u636e\u5e93\u7684\u65f6\u5019\u8bbe\u7f6eAutoCommit  \uff1d  0\u4e86\uff0c \u672c\u4eba\u8ba4\u4e3a\u8fd9\u79cd\u65b9\u5f0f\u66f4\u597d\uff0c \u53ef\u4ee5\u4e00\u6b21\u6570\u636e\u5e93\u8fde\u63a5\u8fdb\u884c\u591a\u6b21\u4e8b\u52a1\u64cd\u4f5c\uff0c \u4e0d\u7528\u6bcf\u4e00\u6b21\u4e8b\u52a1\u7684\u5f00\u59cb\u90fd\u53bb\u8fde\u63a5\u4e00\u6b21\u6570\u636e\u5e93\u3002<\/p>\n<p>\u793a\u4f8b\u4ee3\u7801\u4e3a\uff1a<\/p>\n<p><span style=\"color: #993300;\">  $dbh = DBI-&gt;connect($dataSource, $user, $passwd, {RaiseError =&gt; 1});<br \/>\n  $dbh-&gt;begin_work<br \/>\n  eval {<br \/>\n      foo(&#8230;)        # do lots of work here<br \/>\n      bar(&#8230;)        # including inserts<br \/>\n      baz(&#8230;)        # and updates<br \/>\n      $dbh-&gt;commit;   # commit the changes if we get this far<br \/>\n  };<br \/>\n  if ($@) {<br \/>\n      warn &#8220;Transaction aborted because $@&#8221;;<br \/>\n      # now rollback to undo the incomplete changes<br \/>\n      # but do it in an eval{} as it may also fail<br \/>\n      $dbh-&gt;rollback;<br \/>\n  }<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u548c\u5176\u5b83\u7684\u8bed\u8a00\u4e00\u6837, perl DBI\u5bf9\u6570\u636e\u5e93\u7684\u64cd\u4f5c\u4e5f\u652f\u6301\u4e8b\u52a1\u5904\u7406\uff0c \u5b83\u7684\u5b9e\u73b0\u65b9\u5f0f\u6709\u4e24\u4e2a\uff1a \u4e00\uff1a \u5728\u8fde\u63a5\u6570\u636e\u5e93\u7684\u65f6\u5019\u5c31\u5f00\u59cb\u4e00\u4e2a\u4e8b\u52a1 $dbh = DBI-&gt;connect($dataSource, $user, $passwd, {AutoCommit =&gt; 0}); \u53ef\u4ee5\u770b\u5230\u5728\u8fde\u63a5\u7684\u65f6\u5019\u8bbe\u7f6e\u4e86AutoCommit\u4e3afalse\uff0c \u4e5f\u5c31\u662f\u8bf4\u5f53\u4f60\u5bf9\u6570\u636e\u5e93\u8fdb\u884c\u66f4\u65b0\u64cd\u4f5c\u7684\u65f6\u5019\uff0c \u5b83\u4e0d\u4f1a\u81ea\u52a8\u5730\u628a\u90a3\u4e9b\u66f4\u65b0\u76f4\u63a5\u5199\u5230\u6570\u636e\u5e93\u91cc\uff0c \u800c\u662f\u8981\u7a0b\u5e8f\u901a\u8fc7 $dbh-&gt;commit\u6765\u4f7f\u6570\u636e\u771f\u6b63\u5730\u5199\u5230\u6570\u636e\u5e93\u91cc\uff0c \u6216 $dbh-&gt;rollback \u6765\u56de\u6eda\u521a\u624d\u7684\u64cd\u4f5c \u4e8c\uff1a\u901a\u8fc7$dbh-&gt;begin_work()\u8bed\u53e5\u6765\u5f00\u59cb\u4e00\u4e2a\u4e8b\u52a1 \u8fd9\u79cd\u65b9\u5f0f\u5c31\u4e0d\u9700\u8981\u5728\u8fde\u63a5\u6570\u636e\u5e93\u7684\u65f6\u5019\u8bbe\u7f6eAutoCommit \uff1d 0\u4e86\uff0c \u672c\u4eba\u8ba4\u4e3a\u8fd9\u79cd\u65b9\u5f0f\u66f4\u597d\uff0c \u53ef\u4ee5\u4e00\u6b21\u6570\u636e\u5e93\u8fde\u63a5\u8fdb\u884c\u591a\u6b21\u4e8b\u52a1\u64cd\u4f5c\uff0c \u4e0d\u7528\u6bcf\u4e00\u6b21\u4e8b\u52a1\u7684\u5f00\u59cb\u90fd\u53bb\u8fde\u63a5\u4e00\u6b21\u6570\u636e\u5e93\u3002 \u793a\u4f8b\u4ee3\u7801\u4e3a\uff1a $dbh = DBI-&gt;connect($dataSource, $user, $passwd, {RaiseError =&gt; 1}); $dbh-&gt;begin_work eval { foo(&#8230;) # do lots of work here bar(&#8230;) # including inserts baz(&#8230;) # and &#8230; <a title=\"perl DBI \u4e8b\u52a1\u652f\u6301\" class=\"read-more\" href=\"https:\/\/www.genepeng.com\/index.php\/123\" aria-label=\"More on perl DBI \u4e8b\u52a1\u652f\u6301\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8,4,3],"tags":[42],"_links":{"self":[{"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/posts\/123"}],"collection":[{"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/comments?post=123"}],"version-history":[{"count":3,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/posts\/123\/revisions"}],"predecessor-version":[{"id":125,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/posts\/123\/revisions\/125"}],"wp:attachment":[{"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/media?parent=123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/categories?post=123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/tags?post=123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}