Skip to content

Commit

Permalink
Add a new property skipSetAutoCommitOnClose to JdbcTransactionFactory
Browse files Browse the repository at this point in the history
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
  • Loading branch information
harawata committed Apr 10, 2022
1 parent 0883505 commit ddd931b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,17 @@ public class JdbcTransaction implements Transaction {
protected DataSource dataSource;
protected TransactionIsolationLevel level;
protected boolean autoCommit;
protected boolean skipSetAutoCommitOnClose;

public JdbcTransaction(DataSource ds, TransactionIsolationLevel desiredLevel, boolean desiredAutoCommit) {
this(ds, desiredLevel, desiredAutoCommit, false);
}

public JdbcTransaction(DataSource ds, TransactionIsolationLevel desiredLevel, boolean desiredAutoCommit, boolean skipSetAutoCommitOnClose) {
dataSource = ds;
level = desiredLevel;
autoCommit = desiredAutoCommit;
this.skipSetAutoCommitOnClose = skipSetAutoCommitOnClose;
}

public JdbcTransaction(Connection connection) {
Expand Down Expand Up @@ -113,7 +119,7 @@ protected void setDesiredAutoCommit(boolean desiredAutoCommit) {

protected void resetAutoCommit() {
try {
if (!connection.getAutoCommit()) {
if (!skipSetAutoCommitOnClose && !connection.getAutoCommit()) {
// MyBatis does not call commit/rollback on a connection if just selects were performed.
// Some databases start transactions with select statements
// and they mandate a commit/rollback before closing the connection.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.apache.ibatis.transaction.jdbc;

import java.sql.Connection;
import java.util.Properties;

import javax.sql.DataSource;

Expand All @@ -32,13 +33,26 @@
*/
public class JdbcTransactionFactory implements TransactionFactory {

private boolean skipSetAutoCommitOnClose;

@Override
public void setProperties(Properties props) {
if (props == null) {
return;
}
String value = props.getProperty("skipSetAutoCommitOnClose");
if (value != null) {
skipSetAutoCommitOnClose = Boolean.parseBoolean(value);
}
}

@Override
public Transaction newTransaction(Connection conn) {
return new JdbcTransaction(conn);
}

@Override
public Transaction newTransaction(DataSource ds, TransactionIsolationLevel level, boolean autoCommit) {
return new JdbcTransaction(ds, level, autoCommit);
return new JdbcTransaction(ds, level, autoCommit, skipSetAutoCommitOnClose);
}
}

0 comments on commit ddd931b

Please sign in to comment.