I’m new to Spring Batch and still learning.
Question: I’m encountering an issue with Spring Batch project. I am trying to read, process and and update entities based on their processed flag in a database. Here’s a breakdown of my setup and the problem:
Before: Database has 300 entries, if the processed is “False” – it should be considered for processing in the batch job.
enter image description here
Expected: After the batch job run, all the non processed records (processed = “False”) should be read and Amount should incremented by 1 and writen back into DB with processed = “true
Problem Description:
I’ve set up a Spring Batch job to read, process, and update BankTransaction entities. The goal is to process only transactions that haven’t been processed (processed = false), add $1 to their amount, mark them as processed (processed = true), and save them back to the database.
Current Behavior:
The job reads 10 items, processes them, updates their processed flag to true, and saves them. However, during the subsequent read operation, the reader skips the next 10 items and continues this pattern.
enter image description here
Here is my code:
Reader class
package com.dev.springbatch.batch;
import com.dev.springbatch.entity.BankTransaction;
import jakarta.persistence.EntityManagerFactory;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.database.JpaPagingItemReader;
import org.springframework.batch.item.database.builder.JpaPagingItemReaderBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class BankTransactionsItemReader implements ItemReader<JpaPagingItemReader<BankTransaction>> {
private final EntityManagerFactory entityManagerFactory;
@Autowired
public BankTransactionsItemReader(EntityManagerFactory entityManagerFactory) {
this.entityManagerFactory = entityManagerFactory;
}
@Override
public JpaPagingItemReader<BankTransaction> read() {
return bankTransactionsReader();
}
public JpaPagingItemReader<BankTransaction> bankTransactionsReader() {
final String SELECT_ALL_QUERY = "SELECT c FROM BankTransaction c where c.processed = false";
return new JpaPagingItemReaderBuilder<BankTransaction>()
.name("bankTransactionItemReader")
.entityManagerFactory(entityManagerFactory)
.queryString(SELECT_ALL_QUERY)
.saveState(false) // Ensures the reader does not maintain state between runs
.pageSize(10)
.build();
}
}
processor class
package com.dev.springbatch.batch;
import com.dev.springbatch.entity.BankTransaction;
import lombok.NonNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
@Component
public class BankTransactionItemProcessor implements ItemProcessor<BankTransaction, BankTransaction> {
private static final Logger logger = LoggerFactory.getLogger(BankTransactionItemProcessor.class);
private static final BigDecimal ONE_DOLLAR = BigDecimal.valueOf(1);
@Override
public BankTransaction process(@NonNull BankTransaction bankTransaction) {
return updateBankTransaction(bankTransaction);
}
// adding 1 dollar to the amount
private BankTransaction updateBankTransaction(BankTransaction bankTransaction) {
logger.info("Processing item: {}", bankTransaction);
BigDecimal updatedAmount = bankTransaction.getAmount().add(ONE_DOLLAR);
bankTransaction.setAmount(updatedAmount);
bankTransaction.setProcessed(true);
return bankTransaction;
}
}
writer class
package com.dev.springbatch.batch;
import com.dev.springbatch.entity.BankTransaction;
import com.dev.springbatch.repository.BankTransactionsRepository;
import jakarta.persistence.EntityManagerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class BankTransactionsItemWriter implements ItemWriter<BankTransaction> {
private final BankTransactionsRepository bankTransactionsRepository;
private static final Logger logger = LoggerFactory.getLogger(BankTransactionsItemWriter.class);
@Autowired
public BankTransactionsItemWriter(final BankTransactionsRepository bankTransactionsRepository) {
this.bankTransactionsRepository = bankTransactionsRepository;
}
@Override
public void write(final Chunk<? extends BankTransaction> chunk) {
List<? extends BankTransaction> bankTransactionsList = chunk.getItems();
logger.info("Received chunk with {} items. starting from id: {}", bankTransactionsList.size(), bankTransactionsList.get(0).getId());
bankTransactionsRepository.saveAll(bankTransactionsList);
}
}
Batch configuration
package com.dev.springbatch.config;
import com.dev.springbatch.batch.BankTransactionItemProcessor;
import com.dev.springbatch.batch.BankTransactionsItemReader;
import com.dev.springbatch.batch.BankTransactionsItemWriter;
import com.dev.springbatch.entity.BankTransaction;
import com.dev.springbatch.listner.StepSkipListner;
import com.dev.springbatch.repository.BankTransactionsRepository;
import com.dev.springbatch.skippolicies.ExceptionSkipPolicy;
import jakarta.persistence.EntityManagerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.core.step.skip.SkipPolicy;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
@Configuration
public class BankTransactionsBatchConfig {
@Autowired
BankTransactionsRepository bankTransactionsRepository;
@Autowired
@Qualifier("bankingEntityManagerFactory")
EntityManagerFactory entityManagerFactory;
@Bean
public ItemProcessor<BankTransaction, BankTransaction> processor() {
return new BankTransactionItemProcessor();
}
@Bean
public BankTransactionsItemWriter writer() {
return new BankTransactionsItemWriter(bankTransactionsRepository);
}
@Bean
public ItemReader<BankTransaction> reader() throws Exception {
return new BankTransactionsItemReader(entityManagerFactory).read();
}
@Bean
public Job importBankTransactionJob(final JobRepository jobRepository,
@Qualifier("incrementTransactionByDollarStep") final Step step1) {
return new JobBuilder("importBankTransactionJob", jobRepository)
.start(step1)
.build();
}
@Bean("incrementTransactionByDollarStep")
public Step step1(final JobRepository jobRepository,
final PlatformTransactionManager transactionManager,
final ItemReader<BankTransaction> reader,
final ItemProcessor<BankTransaction, BankTransaction> processor,
final ItemWriter<BankTransaction> writer) {
return new StepBuilder("step1", jobRepository)
.<BankTransaction, BankTransaction>chunk(10, transactionManager)
.reader(reader)
.processor(processor)
.writer(writer)
.build();
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
@Bean
public SkipPolicy skipPolicy() {
return new ExceptionSkipPolicy();
}
@Bean
public StepSkipListner skipListener() {
return new StepSkipListner();
}
}
Database configuration
package com.dev.springbatch.databaseconfig.bankingdbconfig;
import jakarta.persistence.EntityManagerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
basePackages = "com.dev.springbatch.repository",
entityManagerFactoryRef = "bankingEntityManagerFactory",
transactionManagerRef = "bankingTransactionManager"
)
public class BankingDBConfiguration {
@Primary
@Bean(name = "bankingDataSource")
@ConfigurationProperties(prefix = "spring.banking.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "bankingEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder,
@Qualifier("bankingDataSource") DataSource dataSource) {
Map<String, Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto", "update");
return builder
.dataSource(dataSource)
.properties(properties)
.packages("com.dev.springbatch.entity")
.persistenceUnit("banking")
.build();
}
@Bean(name = "bankingTransactionManager")
public PlatformTransactionManager transactionManager(@Qualifier("bankingEntityManagerFactory")
EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
Repository Configuration
package com.dev.springbatch.repository;
import com.dev.springbatch.entity.BankTransaction;
import org.springframework.data.jpa.repository.JpaRepository;
public interface BankTransactionsRepository extends JpaRepository<BankTransaction, Integer> {
}
Entity class
package com.dev.springbatch.entity;
import jakarta.persistence.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.math.BigDecimal;
@Entity
@Table(name = "bank_transaction_yearly")
@Data
@NoArgsConstructor
public class BankTransaction {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private int month;
@Column(nullable = false)
private int day;
@Column(nullable = false)
private int hour;
@Column(nullable = false)
private int minute;
@Column(nullable = false, precision = 10, scale = 2)
private BigDecimal amount;
@Column(nullable = false, length = 36)
private String merchant;
@Column(nullable = false)
private boolean processed;
}
logs:
**Chunk 1: Starting at 1: **
Hibernate: select bt1_0.id,bt1_0.amount,bt1_0.day,bt1_0.hour,bt1_0.merchant,bt1_0.minute,bt1_0.month,bt1_0.processed from bank_transaction_yearly bt1_0 where bt1_0.processed=0 limit ?,?
2024-07-01T01:51:50.489-07:00 INFO 30334 --- [spring batch application] [nio-8080-exec-2] c.d.s.b.BankTransactionItemProcessor : Processing item: BankTransaction(id=1, month=8, day=5, hour=19, minute=6, amount=912.65, merchant=Merchant_1, processed=false)
2024-07-01T01:51:50.492-07:00 INFO 30334 --- [spring batch application] [nio-8080-exec-2] c.d.s.b.BankTransactionItemProcessor : Processing item: BankTransaction(id=2, month=1, day=26, hour=17, minute=19, amount=-706.03, merchant=Merchant_2, processed=false)
2024-07-01T01:51:50.492-07:00 INFO 30334 --- [spring batch application] [nio-8080-exec-2] c.d.s.b.BankTransactionItemProcessor : Processing item: BankTransaction(id=3, month=12, day=13, hour=21, minute=15, amount=826.86, merchant=Merchant_3, processed=false)
2024-07-01T01:51:50.492-07:00 INFO 30334 --- [spring batch application] [nio-8080-exec-2] c.d.s.b.BankTransactionItemProcessor : Processing item: BankTransaction(id=4, month=2, day=13, hour=13, minute=47, amount=377.95, merchant=Merchant_4, processed=false)
2024-07-01T01:51:50.492-07:00 INFO 30334 --- [spring batch application] [nio-8080-exec-2] c.d.s.b.BankTransactionItemProcessor : Processing item: BankTransaction(id=5, month=11, day=25, hour=3, minute=40, amount=794.50, merchant=Merchant_5, processed=false)
2024-07-01T01:51:50.492-07:00 INFO 30334 --- [spring batch application] [nio-8080-exec-2] c.d.s.b.BankTransactionItemProcessor : Processing item: BankTransaction(id=6, month=12, day=20, hour=13, minute=30, amount=508.66, merchant=Merchant_6, processed=false)
2024-07-01T01:51:50.493-07:00 INFO 30334 --- [spring batch application] [nio-8080-exec-2] c.d.s.b.BankTransactionItemProcessor : Processing item: BankTransaction(id=7, month=6, day=20, hour=8, minute=11, amount=846.55, merchant=Merchant_7, processed=false)
2024-07-01T01:51:50.493-07:00 INFO 30334 --- [spring batch application] [nio-8080-exec-2] c.d.s.b.BankTransactionItemProcessor : Processing item: BankTransaction(id=8, month=7, day=6, hour=3, minute=7, amount=-16.81, merchant=Merchant_8, processed=false)
2024-07-01T01:51:50.493-07:00 INFO 30334 --- [spring batch application] [nio-8080-exec-2] c.d.s.b.BankTransactionItemProcessor : Processing item: BankTransaction(id=9, month=6, day=16, hour=22, minute=22, amount=600.64, merchant=Merchant_9, processed=false)
2024-07-01T01:51:50.493-07:00 INFO 30334 --- [spring batch application] [nio-8080-exec-2] c.d.s.b.BankTransactionItemProcessor : Processing item: BankTransaction(id=10, month=4, day=16, hour=13, minute=36, amount=-880.63, merchant=Merchant_10, processed=false)
2024-07-01T01:51:50.493-07:00 INFO 30334 --- [spring batch application] [nio-8080-exec-2] c.d.s.batch.BankTransactionsItemWriter : Received chunk with 10 items. starting from id: 1
Hibernate: select bt1_0.id,bt1_0.amount,bt1_0.day,bt1_0.hour,bt1_0.merchant,bt1_0.minute,bt1_0.month,bt1_0.processed from bank_transaction_yearly bt1_0 where bt1_0.id=?
Hibernate: select bt1_0.id,bt1_0.amount,bt1_0.day,bt1_0.hour,bt1_0.merchant,bt1_0.minute,bt1_0.month,bt1_0.processed from bank_transaction_yearly bt1_0 where bt1_0.id=?
Hibernate: select bt1_0.id,bt1_0.amount,bt1_0.day,bt1_0.hour,bt1_0.merchant,bt1_0.minute,bt1_0.month,bt1_0.processed from bank_transaction_yearly bt1_0 where bt1_0.id=?
Hibernate: select bt1_0.id,bt1_0.amount,bt1_0.day,bt1_0.hour,bt1_0.merchant,bt1_0.minute,bt1_0.month,bt1_0.processed from bank_transaction_yearly bt1_0 where bt1_0.id=?
Hibernate: select bt1_0.id,bt1_0.amount,bt1_0.day,bt1_0.hour,bt1_0.merchant,bt1_0.minute,bt1_0.month,bt1_0.processed from bank_transaction_yearly bt1_0 where bt1_0.id=?
Hibernate: select bt1_0.id,bt1_0.amount,bt1_0.day,bt1_0.hour,bt1_0.merchant,bt1_0.minute,bt1_0.month,bt1_0.processed from bank_transaction_yearly bt1_0 where bt1_0.id=?
Hibernate: select bt1_0.id,bt1_0.amount,bt1_0.day,bt1_0.hour,bt1_0.merchant,bt1_0.minute,bt1_0.month,bt1_0.processed from bank_transaction_yearly bt1_0 where bt1_0.id=?
Hibernate: select bt1_0.id,bt1_0.amount,bt1_0.day,bt1_0.hour,bt1_0.merchant,bt1_0.minute,bt1_0.month,bt1_0.processed from bank_transaction_yearly bt1_0 where bt1_0.id=?
Hibernate: select bt1_0.id,bt1_0.amount,bt1_0.day,bt1_0.hour,bt1_0.merchant,bt1_0.minute,bt1_0.month,bt1_0.processed from bank_transaction_yearly bt1_0 where bt1_0.id=?
Hibernate: select bt1_0.id,bt1_0.amount,bt1_0.day,bt1_0.hour,bt1_0.merchant,bt1_0.minute,bt1_0.month,bt1_0.processed from bank_transaction_yearly bt1_0 where bt1_0.id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
------------------------------------------------------------------------------------
**Chunk 2: Starting at 21: ** (This should be 11)
Hibernate: select bt1_0.id,bt1_0.amount,bt1_0.day,bt1_0.hour,bt1_0.merchant,bt1_0.minute,bt1_0.month,bt1_0.processed from bank_transaction_yearly bt1_0 where bt1_0.processed=0 limit ?,?
2024-07-01T01:51:50.529-07:00 INFO 30334 --- [spring batch application] [nio-8080-exec-2] c.d.s.b.BankTransactionItemProcessor : Processing item: BankTransaction(id=21, month=9, day=23, hour=9, minute=29, amount=727.24, merchant=Merchant_21, processed=false)
2024-07-01T01:51:50.529-07:00 INFO 30334 --- [spring batch application] [nio-8080-exec-2] c.d.s.b.BankTransactionItemProcessor : Processing item: BankTransaction(id=22, month=11, day=15, hour=23, minute=19, amount=862.11, merchant=Merchant_22, processed=false)
2024-07-01T01:51:50.529-07:00 INFO 30334 --- [spring batch application] [nio-8080-exec-2] c.d.s.b.BankTransactionItemProcessor : Processing item: BankTransaction(id=23, month=10, day=22, hour=8, minute=51, amount=-441.05, merchant=Merchant_23, processed=false)
2024-07-01T01:51:50.529-07:00 INFO 30334 --- [spring batch application] [nio-8080-exec-2] c.d.s.b.BankTransactionItemProcessor : Processing item: BankTransaction(id=24, month=4, day=19, hour=1, minute=15, amount=248.38, merchant=Merchant_24, processed=false)
2024-07-01T01:51:50.529-07:00 INFO 30334 --- [spring batch application] [nio-8080-exec-2] c.d.s.b.BankTransactionItemProcessor : Processing item: BankTransaction(id=25, month=6, day=16, hour=21, minute=7, amount=-29.46, merchant=Merchant_25, processed=false)
2024-07-01T01:51:50.529-07:00 INFO 30334 --- [spring batch application] [nio-8080-exec-2] c.d.s.b.BankTransactionItemProcessor : Processing item: BankTransaction(id=26, month=8, day=3, hour=17, minute=48, amount=555.81, merchant=Merchant_26, processed=false)
2024-07-01T01:51:50.529-07:00 INFO 30334 --- [spring batch application] [nio-8080-exec-2] c.d.s.b.BankTransactionItemProcessor : Processing item: BankTransaction(id=27, month=12, day=9, hour=11, minute=21, amount=-417.86, merchant=Merchant_27, processed=false)
2024-07-01T01:51:50.529-07:00 INFO 30334 --- [spring batch application] [nio-8080-exec-2] c.d.s.b.BankTransactionItemProcessor : Processing item: BankTransaction(id=28, month=6, day=11, hour=16, minute=38, amount=-547.42, merchant=Merchant_28, processed=false)
2024-07-01T01:51:50.529-07:00 INFO 30334 --- [spring batch application] [nio-8080-exec-2] c.d.s.b.BankTransactionItemProcessor : Processing item: BankTransaction(id=29, month=1, day=20, hour=1, minute=41, amount=384.50, merchant=Merchant_29, processed=false)
2024-07-01T01:51:50.529-07:00 INFO 30334 --- [spring batch application] [nio-8080-exec-2] c.d.s.b.BankTransactionItemProcessor : Processing item: BankTransaction(id=30, month=3, day=4, hour=9, minute=28, amount=-119.99, merchant=Merchant_30, processed=false)
2024-07-01T01:51:50.529-07:00 INFO 30334 --- [spring batch application] [nio-8080-exec-2] c.d.s.batch.BankTransactionsItemWriter : Received chunk with 10 items. starting from id: 21
Hibernate: select bt1_0.id,bt1_0.amount,bt1_0.day,bt1_0.hour,bt1_0.merchant,bt1_0.minute,bt1_0.month,bt1_0.processed from bank_transaction_yearly bt1_0 where bt1_0.id=?
Hibernate: select bt1_0.id,bt1_0.amount,bt1_0.day,bt1_0.hour,bt1_0.merchant,bt1_0.minute,bt1_0.month,bt1_0.processed from bank_transaction_yearly bt1_0 where bt1_0.id=?
Hibernate: select bt1_0.id,bt1_0.amount,bt1_0.day,bt1_0.hour,bt1_0.merchant,bt1_0.minute,bt1_0.month,bt1_0.processed from bank_transaction_yearly bt1_0 where bt1_0.id=?
Hibernate: select bt1_0.id,bt1_0.amount,bt1_0.day,bt1_0.hour,bt1_0.merchant,bt1_0.minute,bt1_0.month,bt1_0.processed from bank_transaction_yearly bt1_0 where bt1_0.id=?
Hibernate: select bt1_0.id,bt1_0.amount,bt1_0.day,bt1_0.hour,bt1_0.merchant,bt1_0.minute,bt1_0.month,bt1_0.processed from bank_transaction_yearly bt1_0 where bt1_0.id=?
Hibernate: select bt1_0.id,bt1_0.amount,bt1_0.day,bt1_0.hour,bt1_0.merchant,bt1_0.minute,bt1_0.month,bt1_0.processed from bank_transaction_yearly bt1_0 where bt1_0.id=?
Hibernate: select bt1_0.id,bt1_0.amount,bt1_0.day,bt1_0.hour,bt1_0.merchant,bt1_0.minute,bt1_0.month,bt1_0.processed from bank_transaction_yearly bt1_0 where bt1_0.id=?
Hibernate: select bt1_0.id,bt1_0.amount,bt1_0.day,bt1_0.hour,bt1_0.merchant,bt1_0.minute,bt1_0.month,bt1_0.processed from bank_transaction_yearly bt1_0 where bt1_0.id=?
Hibernate: select bt1_0.id,bt1_0.amount,bt1_0.day,bt1_0.hour,bt1_0.merchant,bt1_0.minute,bt1_0.month,bt1_0.processed from bank_transaction_yearly bt1_0 where bt1_0.id=?
Hibernate: select bt1_0.id,bt1_0.amount,bt1_0.day,bt1_0.hour,bt1_0.merchant,bt1_0.minute,bt1_0.month,bt1_0.processed from bank_transaction_yearly bt1_0 where bt1_0.id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
Hibernate: update bank_transaction_yearly set amount=?,day=?,hour=?,merchant=?,minute=?,month=?,processed=? where id=?
------------------------------------------------------------------------------------
**Chunk 2: Starting at 41: ** (Ideally this should be 31)
Hibernate: select bt1_0.id,bt1_0.amount,bt1_0.day,bt1_0.hour,bt1_0.merchant,bt1_0.minute,bt1_0.month,bt1_0.processed from bank_transaction_yearly bt1_0 where bt1_0.processed=0 limit ?,?
2024-07-01T01:51:50.547-07:00 INFO 30334 --- [spring batch application] [nio-8080-exec-2] c.d.s.b.BankTransactionItemProcessor : Processing item: BankTransaction(id=41, month=11, day=3, hour=19, minute=11, amount=995.35, merchant=Merchant_41, processed=false)
2024-07-01T01:51:50.547-07:00 INFO 30334 --- [spring batch application] [nio-8080-exec-2] c.d.s.b.BankTransactionItemProcessor : Processing item: BankTransaction(id=42, month=8, day=10, hour=5, minute=15, amount=151.00, merchant=Merchant_42, processed=false)
2024-07-01T01:51:50.547-07:00 INFO 30334 --- [spring batch application] [nio-8080-exec-2] c.d.s.b.BankTransactionItemProcessor : Processing item: BankTransaction(id=43, month=2, day=6, hour=18, minute=54, amount=-396.13, merchant=Merchant_43, processed=false)
.
.
c.d.s.batch.BankTransactionsItemWriter : Received chunk with 10 items. starting from id: 41
.
.
.
.
Your help is much appreciated. Thanks in advance.
Everything works fine when I update the query in the reader
public JpaPagingItemReader<BankTransaction> bankTransactionsReader() {
final String SELECT_ALL_QUERY = "SELECT c FROM BankTransaction c where c.processed = false";
return new JpaPagingItemReaderBuilder<BankTransaction>()
.name("bankTransactionItemReader")
.entityManagerFactory(entityManagerFactory)
.queryString(SELECT_ALL_QUERY)
.saveState(false) // Ensures the reader does not maintain state between runs
.pageSize(10)
.build();
}
and take all the records and only filter them manually in processor
// adding 1 dollar to the amount
private BankTransaction updateBankTransaction(BankTransaction bankTransaction) {
logger.info("Processing item: {}", bankTransaction);
if (!bankTransaction.isProcessed()) {
BigDecimal updatedAmount = bankTransaction.getAmount().add(ONE_DOLLAR);
bankTransaction.setAmount(updatedAmount);
bankTransaction.setProcessed(true);
}
return bankTransaction;
}
But in this way I have to read and update all the records in database everything, which doesn’t look correct.
vivek sumanth is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.