使用JpaPagingItemReader的Spring Boot Batch

时间:2018-09-05 06:43:34

标签: spring spring-boot spring-data-jpa spring-batch

我正在使用页面大小为40的JpaPagingItemReader。但是每次批量执行时,我都在获取java.lang.IllegalStateException:EntityManager已关闭

下面是代码

@Autowired
    private EntityManagerFactory entityManagerFactory;
@PersistenceContext
    private EntityManager em;

    @Bean(destroyMethod = "")
        public ItemReader<QuoteOfferFulfillment> reader() {
            JpaPagingItemReader<QuoteOfferFulfillment> fullfillment = new JpaPagingItemReader<>();
            try {
                String sqlQuery = "SELECT * FROM QUOTE_OFFER_FULFILLMENT QOF WHERE QOF.STATUS='"
                        + FULFILLMENT_READY + "'" + " OR QOF.STATUS='"
                        + FAILED + "'" + " AND RETRY_COUNT < 3";
                JpaNativeQueryProvider<QuoteOfferFulfillment> queryProvider = new JpaNativeQueryProvider<QuoteOfferFulfillment>();
                queryProvider.setSqlQuery(sqlQuery);
                queryProvider.setEntityClass(QuoteOfferFulfillment.class);
                queryProvider.afterPropertiesSet();
                fullfillment.setEntityManagerFactory(em.getEntityManagerFactory());
                //fullfillment.setEntityManagerFactory(entityManagerFactory);
                fullfillment.setPageSize(40);
                fullfillment.setQueryProvider(queryProvider);
                fullfillment.afterPropertiesSet();
                fullfillment.setSaveState(true);
            }
            catch (Exception e) {
                System.out.println("BatchConfiguration.reader() ==> error " + e.getMessage());
            }
            return fullfillment;
        }

我面临的异常:

[ERROR] org.springframework.batch.core.step.AbstractStep - Encountered an error executing step offersFulfillmentstep in job offersFulfillmentJob
java.lang.IllegalStateException: EntityManager is closed
    at org.hibernate.jpa.internal.EntityManagerImpl.checkOpen(EntityManagerImpl.java:97)
    at org.hibernate.jpa.internal.EntityManagerImpl.checkOpen(EntityManagerImpl.java:88)
    at org.hibernate.jpa.internal.EntityManagerImpl.getSession(EntityManagerImpl.java:103)
    at org.hibernate.jpa.internal.TransactionImpl.getSession(TransactionImpl.java:35)
    at org.hibernate.jpa.internal.TransactionImpl.begin(TransactionImpl.java:45)
    at org.springframework.batch.item.database.JpaPagingItemReader.doReadPage(JpaPagingItemReader.java:197)
    at org.springframework.batch.item.database.AbstractPagingItemReader.doRead(AbstractPagingItemReader.java:108)
    at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.read(AbstractItemCountingItemStreamItemReader.java:88)
    at org.springframework.batch.core.step.item.SimpleChunkProvider.doRead(SimpleChunkProvider.java:91)
    at org.springframework.batch.core.step.item.SimpleChunkProvider.read(SimpleChunkProvider.java:157)
    at org.springframework.batch.core.step.item.SimpleChunkProvider$1.doInIteration(SimpleChunkProvider.java:116)
    at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:374)
    at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
    at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144)
    at org.springframework.batch.core.step.item.SimpleChunkProvider.provide(SimpleChunkProvider.java:110)
    at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:69)
    at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:406)
    at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:330)
    at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
    at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:272)
    at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:81)
    at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:374)
    at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
    at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144)
    at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:257)
    at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:200)
    at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148)
    at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:64)
    at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:67)
    at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:169)
    at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:144)
    at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:134)
    at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:306)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:135)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
[DEBUG] org.hibernate.jpa.spi.AbstractEntityManagerImpl - Mark transaction for rollback
[ERROR] org.springframework.batch.core.step.AbstractStep - Exception while closing step execution resources in step offersFulfillmentstep in job offersFulfillmentJob
org.springframework.batch.item.ItemStreamException: Error while closing item reader
    at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.close(AbstractItemCountingItemStreamItemReader.java:136)
    at org.springframework.batch.item.support.CompositeItemStream.close(CompositeItemStream.java:85)
    at org.springframework.batch.core.step.tasklet.TaskletStep.close(TaskletStep.java:305)
    at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:271)
    at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148)
    at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:64)
    at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:67)
    at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:169)
    at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:144)
    at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:134)
    at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:306)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:135)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalStateException: EntityManager is closed
    at org.hibernate.jpa.internal.EntityManagerImpl.checkOpen(EntityManagerImpl.java:97)
    at org.hibernate.jpa.internal.EntityManagerImpl.checkOpen(EntityManagerImpl.java:88)
    at org.hibernate.jpa.internal.EntityManagerImpl.close(EntityManagerImpl.java:140)
    at sun.reflect.GeneratedMethodAccessor1564.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:347)
    at com.sun.proxy.$Proxy1171.close(Unknown Source)
    at org.springframework.batch.item.database.JpaPagingItemReader.doClose(JpaPagingItemReader.java:236)
    at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.close(AbstractItemCountingItemStreamItemReader.java:133)
    ... 14 common frames omitted

观察:每个事务都回滚并且正在成功处理。不知道发生了什么

我相信JpaPagingItemReader有问题。每次迭代都会导致实体关闭问题,然后再次处理成功。请让我知道是否有人面临相同的问题以及任何其他选择。

0 个答案:

没有答案