Skip to content

Commit ddd931b

Browse files
committed
Add a new property skipSetAutoCommitOnClose to JdbcTransactionFactory
Sample configuration: ```xml <transactionManager type="JDBC"> <property name="skipSetAutoCommitOnClose" value="true"/> </transactionManager> ``` - Enabling this switch skips `setAutoCommit(true)` call when closing the transaction. - Enabling this switch may improve performance with some drivers (e.g. mysql-connector-java, mssql-jdbc), but not others (e.g. Oracle). - Enabling this switch may cause exception with some drivers (e.g. Derby). Should fix mybatis#2426 TODO: docs
1 parent 0883505 commit ddd931b

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/main/java/org/apache/ibatis/transaction/jdbc/JdbcTransaction.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,17 @@ public class JdbcTransaction implements Transaction {
4444
protected DataSource dataSource;
4545
protected TransactionIsolationLevel level;
4646
protected boolean autoCommit;
47+
protected boolean skipSetAutoCommitOnClose;
4748

4849
public JdbcTransaction(DataSource ds, TransactionIsolationLevel desiredLevel, boolean desiredAutoCommit) {
50+
this(ds, desiredLevel, desiredAutoCommit, false);
51+
}
52+
53+
public JdbcTransaction(DataSource ds, TransactionIsolationLevel desiredLevel, boolean desiredAutoCommit, boolean skipSetAutoCommitOnClose) {
4954
dataSource = ds;
5055
level = desiredLevel;
5156
autoCommit = desiredAutoCommit;
57+
this.skipSetAutoCommitOnClose = skipSetAutoCommitOnClose;
5258
}
5359

5460
public JdbcTransaction(Connection connection) {
@@ -113,7 +119,7 @@ protected void setDesiredAutoCommit(boolean desiredAutoCommit) {
113119

114120
protected void resetAutoCommit() {
115121
try {
116-
if (!connection.getAutoCommit()) {
122+
if (!skipSetAutoCommitOnClose && !connection.getAutoCommit()) {
117123
// MyBatis does not call commit/rollback on a connection if just selects were performed.
118124
// Some databases start transactions with select statements
119125
// and they mandate a commit/rollback before closing the connection.

src/main/java/org/apache/ibatis/transaction/jdbc/JdbcTransactionFactory.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.apache.ibatis.transaction.jdbc;
1717

1818
import java.sql.Connection;
19+
import java.util.Properties;
1920

2021
import javax.sql.DataSource;
2122

@@ -32,13 +33,26 @@
3233
*/
3334
public class JdbcTransactionFactory implements TransactionFactory {
3435

36+
private boolean skipSetAutoCommitOnClose;
37+
38+
@Override
39+
public void setProperties(Properties props) {
40+
if (props == null) {
41+
return;
42+
}
43+
String value = props.getProperty("skipSetAutoCommitOnClose");
44+
if (value != null) {
45+
skipSetAutoCommitOnClose = Boolean.parseBoolean(value);
46+
}
47+
}
48+
3549
@Override
3650
public Transaction newTransaction(Connection conn) {
3751
return new JdbcTransaction(conn);
3852
}
3953

4054
@Override
4155
public Transaction newTransaction(DataSource ds, TransactionIsolationLevel level, boolean autoCommit) {
42-
return new JdbcTransaction(ds, level, autoCommit);
56+
return new JdbcTransaction(ds, level, autoCommit, skipSetAutoCommitOnClose);
4357
}
4458
}

0 commit comments

Comments
 (0)