Skip to content

Commit 6fa4e45

Browse files
committed
Refine contribution #4376
* Rename variable * Update Javadocs * Update tests Related to #804
1 parent 34eda1f commit 6fa4e45

File tree

3 files changed

+33
-30
lines changed

3 files changed

+33
-30
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class JpaItemWriter<T> implements ItemWriter<T>, InitializingBean {
5454

5555
private boolean usePersist = false;
5656

57-
private boolean clearEntityManager = true;
57+
private boolean clearPersistenceContext = true;
5858

5959
/**
6060
* Set the EntityManager to be used internally.
@@ -73,12 +73,12 @@ public void setUsePersist(boolean usePersist) {
7373
}
7474

7575
/**
76-
* Flag to indicate that the EntityManager should be cleared and flushed at the end of
77-
* the write (default true).
78-
* @param clearEntityManager the flag value to set
76+
* Flag to indicate that the persistence context should be cleared and flushed at the
77+
* end of the write (default true).
78+
* @param clearPersistenceContext the flag value to set
7979
*/
80-
public void setClearEntityManager(boolean clearEntityManager) {
81-
this.clearEntityManager = clearEntityManager;
80+
public void setClearPersistenceContext(boolean clearPersistenceContext) {
81+
this.clearPersistenceContext = clearPersistenceContext;
8282
}
8383

8484
/**
@@ -103,7 +103,7 @@ public void write(Chunk<? extends T> items) {
103103
}
104104
doWrite(entityManager, items);
105105
entityManager.flush();
106-
if (clearEntityManager) {
106+
if (this.clearPersistenceContext) {
107107
entityManager.clear();
108108
}
109109
}

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class JpaItemWriterBuilder<T> {
3434

3535
private boolean usePersist = false;
3636

37-
private boolean clearEntityManager = true;
37+
private boolean clearPersistenceContext = true;
3838

3939
/**
4040
* The JPA {@link EntityManagerFactory} to obtain an entity manager from. Required.
@@ -62,13 +62,14 @@ public JpaItemWriterBuilder<T> usePersist(boolean usePersist) {
6262

6363
/**
6464
* If set to false, the {@link jakarta.persistence.EntityManager} will not be cleared
65-
* at the end of the chunk.
66-
* @param clearEntityManager defaults to true
65+
* at the end of the chunk. defaults to true
66+
* @param clearPersistenceContext true if the persistence context should be cleared
67+
* after writing items, false otherwise
6768
* @return this instance for method chaining
68-
* @see org.springframework.batch.item.database.JpaItemWriter#setClearEntityManager(boolean)
69+
* @see org.springframework.batch.item.database.JpaItemWriter#setClearPersistenceContext(boolean)
6970
*/
70-
public JpaItemWriterBuilder<T> clearEntityManager(boolean clearEntityManager) {
71-
this.clearEntityManager = clearEntityManager;
71+
public JpaItemWriterBuilder<T> clearPersistenceContext(boolean clearPersistenceContext) {
72+
this.clearPersistenceContext = clearPersistenceContext;
7273

7374
return this;
7475
}
@@ -83,7 +84,7 @@ public JpaItemWriter<T> build() {
8384
JpaItemWriter<T> writer = new JpaItemWriter<>();
8485
writer.setEntityManagerFactory(this.entityManagerFactory);
8586
writer.setUsePersist(this.usePersist);
86-
writer.setClearEntityManager(this.clearEntityManager);
87+
writer.setClearPersistenceContext(this.clearPersistenceContext);
8788

8889
return writer;
8990
}

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

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,22 +76,6 @@ void testConfiguration() throws Exception {
7676
verify(this.entityManager).clear();
7777
}
7878

79-
@Test
80-
void testConfigurationClearEntityManager() throws Exception {
81-
JpaItemWriter<String> itemWriter = new JpaItemWriterBuilder<String>().clearEntityManager(false)
82-
.entityManagerFactory(this.entityManagerFactory).build();
83-
84-
itemWriter.afterPropertiesSet();
85-
86-
Chunk<String> chunk = Chunk.of("foo", "bar");
87-
88-
itemWriter.write(chunk);
89-
90-
verify(this.entityManager).merge(chunk.getItems().get(0));
91-
verify(this.entityManager).merge(chunk.getItems().get(1));
92-
verify(this.entityManager, never()).clear();
93-
}
94-
9579
@Test
9680
void testValidation() {
9781
Exception exception = assertThrows(IllegalStateException.class,
@@ -114,6 +98,24 @@ void testPersist() throws Exception {
11498

11599
verify(this.entityManager).persist(chunk.getItems().get(0));
116100
verify(this.entityManager).persist(chunk.getItems().get(1));
101+
verify(this.entityManager).clear();
102+
}
103+
104+
@Test
105+
void testClearPersistenceContext() throws Exception {
106+
JpaItemWriter<String> itemWriter = new JpaItemWriterBuilder<String>().clearPersistenceContext(false)
107+
.entityManagerFactory(this.entityManagerFactory)
108+
.build();
109+
110+
itemWriter.afterPropertiesSet();
111+
112+
Chunk<String> chunk = Chunk.of("foo", "bar");
113+
114+
itemWriter.write(chunk);
115+
116+
verify(this.entityManager).merge(chunk.getItems().get(0));
117+
verify(this.entityManager).merge(chunk.getItems().get(1));
118+
verify(this.entityManager, never()).clear();
117119
}
118120

119121
}

0 commit comments

Comments
 (0)