Refine contribution #4376
* Rename variable * Update Javadocs * Update tests Related to #804
This commit is contained in:
@@ -54,7 +54,7 @@ public class JpaItemWriter<T> implements ItemWriter<T>, InitializingBean {
|
||||
|
||||
private boolean usePersist = false;
|
||||
|
||||
private boolean clearEntityManager = true;
|
||||
private boolean clearPersistenceContext = true;
|
||||
|
||||
/**
|
||||
* Set the EntityManager to be used internally.
|
||||
@@ -73,12 +73,12 @@ public class JpaItemWriter<T> implements ItemWriter<T>, InitializingBean {
|
||||
}
|
||||
|
||||
/**
|
||||
* Flag to indicate that the EntityManager should be cleared and flushed at the end of
|
||||
* the write (default true).
|
||||
* @param clearEntityManager the flag value to set
|
||||
* Flag to indicate that the persistence context should be cleared and flushed at the
|
||||
* end of the write (default true).
|
||||
* @param clearPersistenceContext the flag value to set
|
||||
*/
|
||||
public void setClearEntityManager(boolean clearEntityManager) {
|
||||
this.clearEntityManager = clearEntityManager;
|
||||
public void setClearPersistenceContext(boolean clearPersistenceContext) {
|
||||
this.clearPersistenceContext = clearPersistenceContext;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -103,7 +103,7 @@ public class JpaItemWriter<T> implements ItemWriter<T>, InitializingBean {
|
||||
}
|
||||
doWrite(entityManager, items);
|
||||
entityManager.flush();
|
||||
if (clearEntityManager) {
|
||||
if (this.clearPersistenceContext) {
|
||||
entityManager.clear();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ public class JpaItemWriterBuilder<T> {
|
||||
|
||||
private boolean usePersist = false;
|
||||
|
||||
private boolean clearEntityManager = true;
|
||||
private boolean clearPersistenceContext = true;
|
||||
|
||||
/**
|
||||
* The JPA {@link EntityManagerFactory} to obtain an entity manager from. Required.
|
||||
@@ -62,13 +62,14 @@ public class JpaItemWriterBuilder<T> {
|
||||
|
||||
/**
|
||||
* If set to false, the {@link jakarta.persistence.EntityManager} will not be cleared
|
||||
* at the end of the chunk.
|
||||
* @param clearEntityManager defaults to true
|
||||
* at the end of the chunk. defaults to true
|
||||
* @param clearPersistenceContext true if the persistence context should be cleared
|
||||
* after writing items, false otherwise
|
||||
* @return this instance for method chaining
|
||||
* @see org.springframework.batch.item.database.JpaItemWriter#setClearEntityManager(boolean)
|
||||
* @see org.springframework.batch.item.database.JpaItemWriter#setClearPersistenceContext(boolean)
|
||||
*/
|
||||
public JpaItemWriterBuilder<T> clearEntityManager(boolean clearEntityManager) {
|
||||
this.clearEntityManager = clearEntityManager;
|
||||
public JpaItemWriterBuilder<T> clearPersistenceContext(boolean clearPersistenceContext) {
|
||||
this.clearPersistenceContext = clearPersistenceContext;
|
||||
|
||||
return this;
|
||||
}
|
||||
@@ -83,7 +84,7 @@ public class JpaItemWriterBuilder<T> {
|
||||
JpaItemWriter<T> writer = new JpaItemWriter<>();
|
||||
writer.setEntityManagerFactory(this.entityManagerFactory);
|
||||
writer.setUsePersist(this.usePersist);
|
||||
writer.setClearEntityManager(this.clearEntityManager);
|
||||
writer.setClearPersistenceContext(this.clearPersistenceContext);
|
||||
|
||||
return writer;
|
||||
}
|
||||
|
||||
@@ -76,22 +76,6 @@ class JpaItemWriterBuilderTests {
|
||||
verify(this.entityManager).clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testConfigurationClearEntityManager() throws Exception {
|
||||
JpaItemWriter<String> itemWriter = new JpaItemWriterBuilder<String>().clearEntityManager(false)
|
||||
.entityManagerFactory(this.entityManagerFactory).build();
|
||||
|
||||
itemWriter.afterPropertiesSet();
|
||||
|
||||
Chunk<String> chunk = Chunk.of("foo", "bar");
|
||||
|
||||
itemWriter.write(chunk);
|
||||
|
||||
verify(this.entityManager).merge(chunk.getItems().get(0));
|
||||
verify(this.entityManager).merge(chunk.getItems().get(1));
|
||||
verify(this.entityManager, never()).clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testValidation() {
|
||||
Exception exception = assertThrows(IllegalStateException.class,
|
||||
@@ -114,6 +98,24 @@ class JpaItemWriterBuilderTests {
|
||||
|
||||
verify(this.entityManager).persist(chunk.getItems().get(0));
|
||||
verify(this.entityManager).persist(chunk.getItems().get(1));
|
||||
verify(this.entityManager).clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testClearPersistenceContext() throws Exception {
|
||||
JpaItemWriter<String> itemWriter = new JpaItemWriterBuilder<String>().clearPersistenceContext(false)
|
||||
.entityManagerFactory(this.entityManagerFactory)
|
||||
.build();
|
||||
|
||||
itemWriter.afterPropertiesSet();
|
||||
|
||||
Chunk<String> chunk = Chunk.of("foo", "bar");
|
||||
|
||||
itemWriter.write(chunk);
|
||||
|
||||
verify(this.entityManager).merge(chunk.getItems().get(0));
|
||||
verify(this.entityManager).merge(chunk.getItems().get(1));
|
||||
verify(this.entityManager, never()).clear();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user