Skip to content

Commit 42b1464

Browse files
GGHDMSfmbenhassine
authored andcommitted
Fix dirty flag on ExecutionContext
Before this commit, the dirty flag was reset on any put operation, even those that replace existing keys with the same values. This commit fixes the dirty flag to be cleared only by meaningful put operations. Resolves #4685 Resolves #4692
1 parent a79b6f7 commit 42b1464

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/ExecutionContext.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2023 the original author or authors.
2+
* Copyright 2006-2025 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.
@@ -37,6 +37,7 @@
3737
* @author Lucas Ward
3838
* @author Douglas Kaminsky
3939
* @author Mahmoud Ben Hassine
40+
* @author Seokmun Heo
4041
*/
4142
public class ExecutionContext implements Serializable {
4243

@@ -124,19 +125,21 @@ public void putDouble(String key, double value) {
124125
public void put(String key, @Nullable Object value) {
125126
if (value != null) {
126127
Object result = this.map.put(key, value);
127-
this.dirty = result == null || !result.equals(value);
128+
this.dirty = this.dirty || result == null || !result.equals(value);
128129
}
129130
else {
130131
Object result = this.map.remove(key);
131-
this.dirty = result != null;
132+
this.dirty = this.dirty || result != null;
132133
}
133134
}
134135

135136
/**
136137
* Indicates if context has been changed with a "put" operation since the dirty flag
137138
* was last cleared. Note that the last time the flag was cleared might correspond to
138-
* creation of the context.
139-
* @return True if "put" operation has occurred since flag was last cleared
139+
* creation of the context. A context is only dirty if a new value is put or an old
140+
* one is removed.
141+
* @return True if a new value was put or an old one was removed since the last time
142+
* the flag was cleared
140143
*/
141144
public boolean isDirty() {
142145
return this.dirty;

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/ExecutionContextTests.java

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2024 the original author or authors.
2+
* Copyright 2006-2025 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.
@@ -36,7 +36,7 @@
3636
/**
3737
* @author Lucas Ward
3838
* @author Mahmoud Ben Hassine
39-
*
39+
* @author Seokmun Heo
4040
*/
4141
class ExecutionContextTests {
4242

@@ -94,11 +94,13 @@ void testNotDirtyWithDuplicate() {
9494
}
9595

9696
@Test
97-
void testNotDirtyWithRemoveMissing() {
97+
void testDirtyWithRemoveMissing() {
9898
context.putString("1", "test");
9999
assertTrue(context.isDirty());
100100
context.putString("1", null); // remove an item that was present
101101
assertTrue(context.isDirty());
102+
103+
context.clearDirtyFlag();
102104
context.putString("1", null); // remove a non-existent item
103105
assertFalse(context.isDirty());
104106
}
@@ -167,6 +169,15 @@ void testCopyConstructorNullInput() {
167169
assertTrue(context.isEmpty());
168170
}
169171

172+
@Test
173+
void testDirtyWithDuplicate() {
174+
ExecutionContext context = new ExecutionContext();
175+
context.put("1", "testString1");
176+
assertTrue(context.isDirty());
177+
context.put("1", "testString1"); // put the same value
178+
assertTrue(context.isDirty());
179+
}
180+
170181
/**
171182
* Value object for testing serialization
172183
*/

0 commit comments

Comments
 (0)