Skip to content

Commit bbabe02

Browse files
trapaankfmbenhassine
authored andcommitted
Add connectionAutoCommit property in JdbcCursorItemReaderBuilder
Issue #3717
1 parent 8a6a2bd commit bbabe02

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/builder/JdbcCursorItemReaderBuilder.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,6 +35,7 @@
3535
* @author Glenn Renfro
3636
* @author Drummond Dawson
3737
* @author Mahmoud Ben Hassine
38+
* @author Ankur Trapasiya
3839
* @since 4.0
3940
*/
4041
public class JdbcCursorItemReaderBuilder<T> {
@@ -69,6 +70,8 @@ public class JdbcCursorItemReaderBuilder<T> {
6970

7071
private int currentItemCount;
7172

73+
private boolean connectionAutoCommit;
74+
7275
/**
7376
* Configure if the state of the {@link org.springframework.batch.item.ItemStreamSupport}
7477
* should be persisted within the {@link org.springframework.batch.item.ExecutionContext}
@@ -321,6 +324,20 @@ public JdbcCursorItemReaderBuilder<T> beanRowMapper(Class<T> mappedClass) {
321324
return this;
322325
}
323326

327+
/**
328+
* Set whether "autoCommit" should be overridden for the connection used by the cursor.
329+
* If not set, defaults to Connection / Datasource default configuration.
330+
*
331+
* @param connectionAutoCommit value to set on underlying JDBC connection
332+
* @return this instance for method chaining
333+
* @see JdbcCursorItemReader#setConnectionAutoCommit(boolean)
334+
*/
335+
public JdbcCursorItemReaderBuilder<T> connectionAutoCommit(boolean connectionAutoCommit) {
336+
this.connectionAutoCommit = connectionAutoCommit;
337+
338+
return this;
339+
}
340+
324341
/**
325342
* Validates configuration and builds a new reader instance.
326343
*
@@ -356,6 +373,7 @@ public JdbcCursorItemReader<T> build() {
356373
reader.setQueryTimeout(this.queryTimeout);
357374
reader.setUseSharedExtendedConnection(this.useSharedExtendedConnection);
358375
reader.setVerifyCursorPosition(this.verifyCursorPosition);
376+
reader.setConnectionAutoCommit(this.connectionAutoCommit);
359377

360378
return reader;
361379
}

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/builder/JdbcCursorItemReaderBuilderTests.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -47,6 +47,7 @@
4747
/**
4848
* @author Michael Minella
4949
* @author Drummond Dawson
50+
* @author Ankur Trapasiya
5051
*/
5152
public class JdbcCursorItemReaderBuilderTests {
5253

@@ -302,13 +303,15 @@ public void testOtherProperties() {
302303
.ignoreWarnings(true)
303304
.driverSupportsAbsolute(true)
304305
.useSharedExtendedConnection(true)
306+
.connectionAutoCommit(true)
305307
.beanRowMapper(Foo.class)
306308
.build();
307309

308310
assertEquals(1, ReflectionTestUtils.getField(reader, "fetchSize"));
309311
assertEquals(2, ReflectionTestUtils.getField(reader, "queryTimeout"));
310312
assertTrue((boolean) ReflectionTestUtils.getField(reader, "ignoreWarnings"));
311313
assertTrue((boolean) ReflectionTestUtils.getField(reader, "driverSupportsAbsolute"));
314+
assertTrue((boolean) ReflectionTestUtils.getField(reader, "connectionAutoCommit"));
312315
}
313316

314317
@Test

0 commit comments

Comments
 (0)