diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/AbstractCursorItemReader.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/AbstractCursorItemReader.java index 17f77b060a..17f5ac9a98 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/AbstractCursorItemReader.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/AbstractCursorItemReader.java @@ -102,6 +102,7 @@ * @author Peter Zozom * @author Robert Kasanicky * @author Thomas Risberg + * @author Michael Minella */ public abstract class AbstractCursorItemReader extends AbstractItemCountingItemStreamItemReader implements InitializingBean { @@ -134,9 +135,9 @@ public abstract class AbstractCursorItemReader extends AbstractItemCountingIt private boolean useSharedExtendedConnection = false; - private Boolean connectionAutoCommit; + private boolean connectionAutoCommit; - private Boolean initialConnectionAutoCommit; + private boolean initialConnectionAutoCommit; public AbstractCursorItemReader() { super(); @@ -364,20 +365,12 @@ public boolean isUseSharedExtendedConnection() { * Connection / Datasource default configuration. * * @param autoCommit value used for {@link Connection#setAutoCommit(boolean)}. + * @since 4.0 */ public void setConnectionAutoCommit(boolean autoCommit) { this.connectionAutoCommit = autoCommit; } - /** - * Return whether "autoCommit" should be overridden for the connection used by the cursor. - * - * @return the "autoCommit" value, or {@code null} if none to be applied. - */ - public Boolean getConnectionAutoCommit() { - return this.connectionAutoCommit; - } - public abstract String getSql(); /** @@ -402,9 +395,9 @@ protected void doClose() throws Exception { JdbcUtils.closeResultSet(this.rs); rs = null; cleanupOnClose(); - if (this.initialConnectionAutoCommit != null) { - this.con.setAutoCommit(initialConnectionAutoCommit); - } + + this.con.setAutoCommit(initialConnectionAutoCommit); + if (useSharedExtendedConnection && dataSource instanceof ExtendedConnectionDataSourceProxy) { ((ExtendedConnectionDataSourceProxy)dataSource).stopCloseSuppression(this.con); if (!TransactionSynchronizationManager.isActualTransactionActive()) { @@ -449,8 +442,10 @@ protected void initializeConnection() { else { this.con = dataSource.getConnection(); } - if (this.connectionAutoCommit != null && this.con.getAutoCommit() != this.connectionAutoCommit) { - this.initialConnectionAutoCommit = this.con.getAutoCommit(); + + this.initialConnectionAutoCommit = this.con.getAutoCommit(); + + if (this.con.getAutoCommit() != this.connectionAutoCommit) { this.con.setAutoCommit(this.connectionAutoCommit); } } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JdbcCursorItemReaderConfigTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JdbcCursorItemReaderConfigTests.java index 91e352eb97..1aaf7a7fbd 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JdbcCursorItemReaderConfigTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JdbcCursorItemReaderConfigTests.java @@ -15,23 +15,16 @@ */ package org.springframework.batch.item.database; -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.reset; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; - import javax.sql.DataSource; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; import org.mockito.ArgumentCaptor; + import org.springframework.batch.item.ExecutionContext; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.PlatformTransactionManager; @@ -39,6 +32,13 @@ import org.springframework.transaction.support.TransactionCallback; import org.springframework.transaction.support.TransactionTemplate; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.reset; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + @RunWith(JUnit4.class) public class JdbcCursorItemReaderConfigTests { @@ -107,8 +107,8 @@ public Void doInTransaction(TransactionStatus status) { @Test public void testOverrideConnectionAutoCommit() throws Exception { - boolean initialAutoCommit= false; - boolean neededAutoCommit = true; + boolean initialAutoCommit= false; + boolean neededAutoCommit = true; DataSource ds = mock(DataSource.class); Connection con = mock(Connection.class);