{"id":299,"date":"2012-06-29T03:53:48","date_gmt":"2012-06-29T03:53:48","guid":{"rendered":"http:\/\/blog.zhukunqian.com\/?p=299"},"modified":"2012-06-29T03:53:48","modified_gmt":"2012-06-29T03:53:48","slug":"hibernate-mysql-autocommit%e4%ba%8c","status":"publish","type":"post","link":"https:\/\/blog.zhukunqian.com\/?p=299","title":{"rendered":"hibernate mysql autocommit(\u4e8c)"},"content":{"rendered":"<p>\u67e5\u770b \u4e0a\u4e00\u7bc7hibernate\u6027\u80fd\u4f18\u5316:<\/p>\n<p>hibernate mysql autocommit : http:\/\/blog.zhukunqian.com\/?p=162<\/p>\n<p>\u672c\u6b21\u8981\u89e3\u51b3 mysql jdbc driver\u4e2d\u7684\u00a0 SET autocommit=1 \u7684\u95ee\u9898.<\/p>\n<p>\u9a71\u52a8\u4e2d\u9ed8\u8ba4\u662f\u6bcf\u6b21\u4e8b\u52a1\u5f00\u542f\u524d<\/p>\n<p>SET autocommit=0,<\/p>\n<p>\u4e8b\u52a1\u5f00\u59cb<\/p>\n<p>\u4e8b\u52a1\u7ed3\u675f<\/p>\n<p>set autocommit=1,<\/p>\n<p>\u53ef\u4ee5\u770b\u5230\u4ee5\u4e0a\u6d41\u7a0b\u4e2d\u7684set autocommit=1\u662f\u5b8c\u5168\u6ca1\u6709\u6bd5\u8981,\u800c\u4e14\u4f1a\u5f71\u54cd\u5230\u6570\u636e\u5e93\u6027\u80fd.\u66f4\u4e3a\u91cd\u8981\u7684\u662f,\u8fd9\u5b8c\u5168\u662f\u786c\u7f16\u7801\u5728\u9a71\u52a8\u4e2d\u7684,\u6240\u4ee5\u6211\u4eec\u65e0\u6cd5\u4f7f\u7528\u8bbe\u7f6e\u53c2\u6570\u6765\u8bd5\u56fe\u53d6\u6d88autocommit.<\/p>\n<p>mysql jdbc driver 5.1.6 \u7248\u4e2d\u63d0\u4f9b\u4e86com.mysql.jdbc.ConnectionLifecycleInterceptor,\u4f7f\u6211\u4eec\u53ef\u4ee5\u5229\u7528interceptor\u7684\u673a\u5236\u6765\u63a7\u5236mysql\u4e2d\u7684\u4e8b\u52a1\u6d41\u7a0b.<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u6837\u4f8b:<\/p>\n<p>package game.joycube.common.util.jdbc;<\/p>\n<p>import java.sql.SQLException;<br \/>\nimport java.sql.Savepoint;<br \/>\nimport java.util.Properties;<\/p>\n<p>import org.apache.commons.logging.Log;<br \/>\nimport org.apache.commons.logging.LogFactory;<\/p>\n<p>import com.mysql.jdbc.Connection;<\/p>\n<p>public class ConnectionLifecycleInterceptor implements<br \/>\ncom.mysql.jdbc.ConnectionLifecycleInterceptor {<\/p>\n<p>private static final Log log = LogFactory<br \/>\n.getLog(ConnectionLifecycleInterceptor.class);<\/p>\n<p>public void close() throws SQLException {<br \/>\nlog.info(&#8220;close&#8221;);<br \/>\n}<\/p>\n<p>public boolean commit() throws SQLException {<br \/>\nlog.info(&#8220;commit&#8221;);<br \/>\nreturn true;<br \/>\n}<\/p>\n<p>public boolean rollback() throws SQLException {<br \/>\nlog.info(&#8220;rollback&#8221;);<br \/>\n\/\/ \u8fd9\u91cc\u7684rollback,\u6ca1\u6709\u8bbe\u7f6esavepoint,\u5e94\u8be5\u6bcf\u6b21\u90fd\u4e0drollback<br \/>\nreturn false;<br \/>\n}<\/p>\n<p>public boolean rollback(Savepoint arg0) throws SQLException {<br \/>\nlog.info(&#8220;rollback savepoint &#8221; + arg0);<br \/>\n\/\/ \u8bbe\u7f6e\u4e86savepoint,\u5e94\u8be5rollback<br \/>\nreturn true;<br \/>\n}<\/p>\n<p>public boolean setAutoCommit(boolean arg0) throws SQLException {<br \/>\nlog.info(&#8220;setAutoCommit &#8221; + arg0);<br \/>\n\/\/ \u5982\u679c\u8981\u8bbe\u7f6e set autoCommit=false,\u5141\u8bb8<br \/>\n\/\/ \u5982\u679c\u8981\u8bbe\u7f6e set autoCommit=true,\u4e0d\u5141\u8bb8<br \/>\nif (!arg0) {<br \/>\nreturn true;<br \/>\n}<br \/>\nreturn false;<br \/>\n}<\/p>\n<p>public boolean setCatalog(String arg0) throws SQLException {<br \/>\nlog.info(&#8220;setCatalog &#8221; + arg0);<br \/>\nreturn true;<br \/>\n}<\/p>\n<p>public boolean transactionBegun() throws SQLException {<br \/>\nlog.info(&#8220;transactionBegun&#8221;);<br \/>\nreturn true;<br \/>\n}<\/p>\n<p>public boolean transactionCompleted() throws SQLException {<br \/>\nlog.info(&#8220;transactionCompleted&#8221;);<br \/>\nreturn true;<br \/>\n}<\/p>\n<p>public void destroy() {<br \/>\nlog.info(&#8220;destroy&#8221;);<br \/>\n}<\/p>\n<p>public void init(Connection arg0, Properties arg1) throws SQLException {<br \/>\nlog.info(&#8220;ConnectionLifecycleInterceptor init &#8221; + arg0 + &#8220;\\t&#8221; + arg1);<br \/>\n}<br \/>\n}<\/p>\n<p>\u4ee5\u4e0a\u63a5\u53e3\u4e2d\u8fd4\u56deboolean\u4e2d,true\u8868\u793a\u6309\u9a71\u52a8\u539f\u903b\u8f91\u6267\u884c,false\u8868\u793a\u9a71\u52a8\u4e0d\u6267\u884c\u6b64\u6b21\u64cd\u4f5c<\/p>\n<p>public boolean setAutoCommit(boolean arg0) throws SQLException {<br \/>\nlog.info(&#8220;setAutoCommit &#8221; + arg0);<br \/>\n\/\/ \u5982\u679c\u8981\u8bbe\u7f6e set autoCommit=false,\u5141\u8bb8<br \/>\n\/\/ \u5982\u679c\u8981\u8bbe\u7f6e set autoCommit=true,\u4e0d\u5141\u8bb8<br \/>\nif (!arg0) {<br \/>\nreturn true;<br \/>\n}<br \/>\nreturn false;<br \/>\n}<\/p>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u542b\u4e49\u4e3a:<\/p>\n<p>\u5f53\u9a71\u52a8\u8981\u6267\u884c:set autocommit=0\u65f6,<\/p>\n<p>arg0\u4e3afalse,\u8fd9\u91cc\u8fd4\u56detrue,\u9a71\u52a8\u6309\u539f\u903b\u8f91\u6267\u884c<\/p>\n<p>\u5f53\u6267\u884cset autocomit=1\u65f6<\/p>\n<p>arg0\u4e3atrue,\u8fd9\u91cc\u8fd4\u56defalse,\u7981\u6b62\u9a71\u52a8\u6267\u884c\u672c\u6b21\u64cd\u4f5c<\/p>\n<p>OK,\u4ee3\u7801\u51c6\u5907\u597d\u540e,\u6211\u4eec\u5728\u9a71\u52a8\u4e2d\u542f\u7528\u6b64<em>interceptor<\/em><\/p>\n<p><em><\/em>jdbc:mysql:\/\/127.0.0.1\/wgame?connectionLifecycleInterceptors=game.joycube.common.util.jdbc.ConnectionLifecycleInterceptor<\/p>\n<p>\u8fd9\u6837\u5c31\u542f\u7528\u4e86\u6b64interceptor.<\/p>\n<p>\u5982\u679c \u542f\u7528\u540e\u65e0\u6cd5\u751f\u6548,\u8bf7\u68c0\u67e5\u4e00\u4e0bjdbc\u9a71\u52a8\u7684\u7248\u672c,(\u7248\u672c:5.1.6 +)<br \/>\n\u53c2\u8003:http:\/\/www.jroller.com\/mmatthews\/entry\/huh_i_didn_t_know<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u67e5\u770b \u4e0a\u4e00\u7bc7hibernate\u6027\u80fd\u4f18\u5316: hibernate mysql aut &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/blog.zhukunqian.com\/?p=299\">\u7ee7\u7eed\u9605\u8bfb &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/posts\/299"}],"collection":[{"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=299"}],"version-history":[{"count":2,"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/posts\/299\/revisions"}],"predecessor-version":[{"id":301,"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/posts\/299\/revisions\/301"}],"wp:attachment":[{"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}