I create a job in xml configuration like this:
<batch:job id="myBatchJob" restartable="true" job-repository="jobRepository">
<batch:step id="myBatchJob_firstStep" allow-start-if-complete="true">
<batch:tasklet transaction-manager="transactionManager" allow-start-if-complete="true">
<batch:chunk reader="myItemReader"
processor="myItemProcessor"
writer="myItemWriter"
commit-interval="5">
</batch:chunk>
<batch:listeners>
<batch:listener ref="myItemReader"/>
<batch:listener ref="myItemProcessor"/>
<batch:listener ref="myItemWriter"/>
</batch:listeners>
</batch:tasklet>
</batch:step>
<batch:listeners>
<batch:listener ref="myBatchJobListener"/>
</batch:listeners>
</batch:job>
<bean id = "jobRepository"
class = "org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
<property name = "dataSource" ref = "dataSource" />
<property name = "transactionManager" ref = "transactionManager" />
<property name = "databaseType" value = "oracle" />
<property name="tablePrefix" value="BATCH_"/>
<property name="maxVarCharLength" value="1000"/>
</bean>
<bean id = "jobLauncher"
class = "org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name = "jobRepository" ref = "jobRepository" />
</bean>
<bean id="myItemReader" scope="step" class="...MyItemReader">
<property name="sessionFactory" ref="sessionFactory"/>
<property name="pageSize" value="10"/>
<property name="fetchSize" value="10"/>
<property name="queryName" value="noQueryName"/>
</bean>
<bean id="myItemProcessor" scope="step"
class="...MyItemProcessor">
...
</bean>
<bean id="myItemWriter" scope="step"
class="...MyItemWriter">
...
</bean>
<bean id="myBatchJobListener"
class="...MyBatchJobListener">
...
</bean>
This job work if run in quartz :
protected List<someData> execute() {
Job job = (Job) SpringUtils.getBean("myBatchJob");
JobExecution execution = getJobLauncher().run(job, new JobParameters());
}
But not work in controller and throw this exception:
javax.persistence.TransactionRequiredException: no transaction is in progress
at org.hibernate.internal.AbstractSharedSessionContract.checkTransactionNeededForUpdateOperation(AbstractSharedSessionContract.java:409) ~[hibernate-core-5.4.4.Final.jar:5.4.4.Final]
at org.hibernate.internal.SessionImpl.checkTransactionNeededForUpdateOperation(SessionImpl.java:3602) ~[hibernate-core-5.4.4.Final.jar:5.4.4.Final]
at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1483) ~[hibernate-core-5.4.4.Final.jar:5.4.4.Final]
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1479) ~[hibernate-core-5.4.4.Final.jar:5.4.4.Final]
at org.springframework.orm.hibernate5.SessionFactoryUtils.flush(SessionFactoryUtils.java:147) ~[spring-orm-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.orm.hibernate5.SpringSessionSynchronization.beforeCommit(SpringSessionSynchronization.java:95) ~[spring-orm-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.transaction.support.TransactionSynchronizationUtils.triggerBeforeCommit(TransactionSynchronizationUtils.java:96) ~[spring-tx-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.transaction.support.AbstractPlatformTransactionManager.triggerBeforeCommit(AbstractPlatformTransactionManager.java:922) ~[spring-tx-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:730) ~[spring-tx-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:714) ~[spring-tx-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:152) ~[spring-tx-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:273) ~[spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:82) ~[spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:375) ~[spring-batch-infrastructure-4.1.2.RELEASE.jar:4.1.2.RELEASE]
at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215) ~[spring-batch-infrastructure-4.1.2.RELEASE.jar:4.1.2.RELEASE]
at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:145) ~[spring-batch-infrastructure-4.1.2.RELEASE.jar:4.1.2.RELEASE]
at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:258) ~[spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:203) [spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148) [spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:68) [spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:67) [spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:169) [spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:144) [spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:136) [spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:313) [spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:144) [spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50) [spring-core-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:137) [spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
My item reader is like this:
public class MyItemReader extends HibernatePagingItemReader<SomeEntity> implements StepExecutionListener {
(...)
@Override
protected void doReadPage() {
if (this.results == null) {
this.results = new CopyOnWriteArrayList();
} else {
this.results.clear();
}
try {
Page page = getMyManager().searchByExample(...);
this.results.addAll(page.getList());
}catch (Exception ex) {
logger.error(this.getClass().getName(), ex);
}
}
public void beforeStep(StepExecution stepExecution) {
(...)
}
public ExitStatus afterStep(StepExecution stepExecution) {
return stepExecution.getExitStatus();
}
}
and my item process:
public class MyItemProcessor implements ItemProcessor<SomeEntity, SomeEntity>, StepExecutionListener {
(...)
@Override
public SomeEntityprocess(SomeEntityitem) throws Exception {
return item;
}
public void beforeStep(StepExecution stepExecution) {
}
public ExitStatus afterStep(StepExecution stepExecution) {
return stepExecution.getExitStatus();
}
}
and item writer
public class MyiItemWriter extends HibernateItemWriter<SomeEntity> implements StepExecutionListener {
(...)
@Override
public void write(List<? extends SomeEntity> items) {
System.out.println(items);
}
public void beforeStep(StepExecution stepExecution) {
}
public ExitStatus afterStep(StepExecution stepExecution) {
return stepExecution.getExitStatus();
}
}
This exception throw when the itemReder, read first chunk oh data and itemProcessor is done and itemWriter write first chunk of data and before start next process then Exception is throw.
Can any one help me?
It seems that Spring Batch cannot recognize Hibernate for its own internal process
Mahdi Abedi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.