diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/builder/JdbcCursorItemReaderBuilder.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/builder/JdbcCursorItemReaderBuilder.java index 9d56869b1..463da9165 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/builder/JdbcCursorItemReaderBuilder.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/builder/JdbcCursorItemReaderBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2019 the original author or authors. + * Copyright 2016-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,6 +35,7 @@ import org.springframework.util.StringUtils; * @author Glenn Renfro * @author Drummond Dawson * @author Mahmoud Ben Hassine + * @author Ankur Trapasiya * @since 4.0 */ public class JdbcCursorItemReaderBuilder { @@ -69,6 +70,8 @@ public class JdbcCursorItemReaderBuilder { private int currentItemCount; + private boolean connectionAutoCommit; + /** * Configure if the state of the {@link org.springframework.batch.item.ItemStreamSupport} * should be persisted within the {@link org.springframework.batch.item.ExecutionContext} @@ -321,6 +324,20 @@ public class JdbcCursorItemReaderBuilder { return this; } + /** + * Set whether "autoCommit" should be overridden for the connection used by the cursor. + * If not set, defaults to Connection / Datasource default configuration. + * + * @param connectionAutoCommit value to set on underlying JDBC connection + * @return this instance for method chaining + * @see JdbcCursorItemReader#setConnectionAutoCommit(boolean) + */ + public JdbcCursorItemReaderBuilder connectionAutoCommit(boolean connectionAutoCommit) { + this.connectionAutoCommit = connectionAutoCommit; + + return this; + } + /** * Validates configuration and builds a new reader instance. * @@ -356,6 +373,7 @@ public class JdbcCursorItemReaderBuilder { reader.setQueryTimeout(this.queryTimeout); reader.setUseSharedExtendedConnection(this.useSharedExtendedConnection); reader.setVerifyCursorPosition(this.verifyCursorPosition); + reader.setConnectionAutoCommit(this.connectionAutoCommit); return reader; } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/builder/JdbcCursorItemReaderBuilderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/builder/JdbcCursorItemReaderBuilderTests.java index 24a0452db..521ee3849 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/builder/JdbcCursorItemReaderBuilderTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/builder/JdbcCursorItemReaderBuilderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2018 the original author or authors. + * Copyright 2016-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -47,6 +47,7 @@ import static org.junit.Assert.fail; /** * @author Michael Minella * @author Drummond Dawson + * @author Ankur Trapasiya */ public class JdbcCursorItemReaderBuilderTests { @@ -302,6 +303,7 @@ public class JdbcCursorItemReaderBuilderTests { .ignoreWarnings(true) .driverSupportsAbsolute(true) .useSharedExtendedConnection(true) + .connectionAutoCommit(true) .beanRowMapper(Foo.class) .build(); @@ -309,6 +311,7 @@ public class JdbcCursorItemReaderBuilderTests { assertEquals(2, ReflectionTestUtils.getField(reader, "queryTimeout")); assertTrue((boolean) ReflectionTestUtils.getField(reader, "ignoreWarnings")); assertTrue((boolean) ReflectionTestUtils.getField(reader, "driverSupportsAbsolute")); + assertTrue((boolean) ReflectionTestUtils.getField(reader, "connectionAutoCommit")); } @Test