請參閱春天DataSourceTransactionManager.java,功能doBegin評論:應該將數據源的自動提交設置爲false?
// 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);
}
在該項目中我的工作,自動提交未配置。所以這是默認情況下。我們使用Spring來管理事務,所有SQL都在@Transactional註釋函數中執行。所以交易實際上是手動提交的。每次事務開始時,數據庫連接都將autocommit設置爲false,並在事務退出autocommit設置爲true之後。典型的工作流程是(在JDBC級別):
- conn = dataSource.getConnection();
- conn.setAutoCommit(false);
- stmt = conn.createStatement();
- stmt.executeQuery(...);
- conn.commit()/ conn.rollback();
- conn.setAutoCommit(true);
設置autocommit來回昂貴嗎?爲了性能原因,我們應該配置數據源連接池autocommit = false嗎?跳過步驟2和步驟6.
我已經編輯我的問題來說清楚。 – ShadowDancer