AUTOCOMMIT = 0
如果插入查询看起来像这样,在 MySQL 中批量插入数据之前使用(用于性能调整)是否更好
INSERT INTO SomeTable (column1, column2) VALUES (val1,val2),(val3,val4),...
?
我想执行一些语句,并在最后提交整个批处理。我尝试了以下方法:
connection.setAutoCommit(false); // Don't commit for each statement
int[] returnCodes = pstmt.executeBatch(); // Execute all statements
connection.setAutoCommit(true); // Back to normal state - future statements
// will be committed instantly
connection.commit(); // Commit our batch
Run Code Online (Sandbox Code Playgroud)
哪个失败了:
java.sql.SQLException: Can't call commit when autocommit=true
--reference to the line with connection.commit()--
Run Code Online (Sandbox Code Playgroud)
正确的做法是什么?是否connection.setAutoCommit(true)
提交所有已执行的批处理语句?