2013年7月20日星期六

Test setAutoCommit (false) and executeBatch () found setAutoCommit (false) did not work!

Code:
try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager .getConnection("jdbc:mysql://localhost/zhonghongfa?" + "user=root&password=841017"); conn.setAutoCommit(false); pstmt = conn.createStatement(); pstmt.addBatch("insert into ddltable(id,name) values(301,'zhang')"); pstmt.addBatch("insert into ddltable(id,name) values(302,'zhsdfg')"); pstmt.executeBatch(); conn.rollback(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException ex) { ex.printStackTrace(); System.out.println("SQLException: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("VendorError: " + ex.getErrorCode()); try { if(conn!=null) { conn.rollback(); conn.setAutoCommit(true); } } catch (SQLException e) { e.printStackTrace(); } }
code no price conn.commit (); even rollback conn.rollback (); But look at the database, but the data are inserted!
Why is this so?

------ solutions ------ --------------------------------------------------
and setAutoCommit (false) does not work since it has any relationship.

mysql database is created, you need to specify the type of transaction. Not all mysql databases are two-phase commit.

------ solutions ----------------------------------- ---------------------
you use a storage engine supports transactions do?
------ solutions --------------------------------------- -----------------
http://hi.baidu.com/piaochen/item/96730da849a69f706cd455ff

没有评论:

发表评论