先看看下面代碼的注釋,在Spring DataSourceTransactionManager.java, function doBegin:// 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); } 在我當(dāng)前的Java Web應(yīng)用中,autocommit沒有特別的設(shè)置,那么默認(rèn)就是true。所以每一次事務(wù)開始時(shí),設(shè)置數(shù)據(jù)庫(kù)連接autocommit為false;當(dāng)事務(wù)退出的時(shí)候,又設(shè)置回true; 我理解這是一種標(biāo)準(zhǔn)做法。由Spring管理事務(wù),所有的SQL都在@Transactional標(biāo)記的代碼塊中執(zhí)行,也就是手動(dòng)提交。所有的數(shù)據(jù)庫(kù)連接都從連接池中獲取。目前訪問量在每小時(shí)300萬左右。所以一個(gè)典型的流程就是這樣:conn = dataSource.getConnection();conn.setAutoCommit(false);stmt = conn.createStatement();stmt.executeQuery(...);conn.commit()/conn.rollback();conn.setAutoCommit(true);我的問題是,來回設(shè)置autocommit是否有必要?是否昂貴?能否直接在數(shù)據(jù)庫(kù)連接池上設(shè)置autocommit=false,來保持連接始終為false,跳過上面的第2步和第6步?
DataSource連接池的autocommit是否應(yīng)該被設(shè)置為false
12345678_0001
2019-03-14 14:15:32