mysql 设置事物自动提交_mysql事务自动提交的问题
1:mysql的aut0commit配置默認(rèn)是開(kāi)啟的,也就是沒(méi)執(zhí)行一條sql都會(huì)提交一次,就算顯示的開(kāi)啟事務(wù)也會(huì)導(dǎo)致多條SQL不在一個(gè)事務(wù)中,
如果需要相關(guān)的SQL在同一個(gè)事務(wù)中執(zhí)行,那么必須將autocommit設(shè)置為OFF,再顯式開(kāi)啟事務(wù)。
2:如果使用spring的事務(wù),那么不存在這個(gè)問(wèn)題,spring的事務(wù)默認(rèn)是關(guān)閉自動(dòng)提交的,做法是判斷連接池是否開(kāi)啟事務(wù)自動(dòng)提交,如果連接池開(kāi)啟自動(dòng)提交則設(shè)置自動(dòng)提交為關(guān)閉,否則不做操作,因?yàn)槟承﹋dbc驅(qū)動(dòng)做設(shè)置自動(dòng)提交關(guān)閉代價(jià)昂貴。
/**
* This implementation sets the isolation level but ignores the timeout.
*/
@Override
protected void doBegin(Object transaction, TransactionDefinition definition) {
.........省略某些代碼
// Switch to manual commit if necessary. This is very expensive in some JDBC drivers,
// so we don't want to do it unnecessarily (for example if we've explicitly
// configured the connection pool to set it already).
if (con.getAutoCommit()) {
txObject.setMustRestoreAutoCommit(true);
if (logger.isDebugEnabled()) {
logger.debug("Switching JDBC Connection [" + con + "] to manual commit");
}
con.setAutoCommit(false);
}
}
總結(jié)
以上是生活随笔為你收集整理的mysql 设置事物自动提交_mysql事务自动提交的问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 05 替换空格
- 下一篇: [SQL]SQL server 常用代码