将Spring JpaRepository保存为推迟插入是否合法?

时间:2017-06-26 17:12:45

标签: spring hibernate spring-data-jpa spring-batch spring-batch-admin

我使用Spring Batch并在步骤I中通过JpaRepository.save()方法插入值。

我已启用:

logging.level.org.hibernate.SQL: DEBUG
spring.jpa.properties.hibernate.format_sql: true

并且所有SQL select语句都以与代码相同的顺序出现在日志中。当我逐步调试调试器时,每个find*都会导致org.hibernate.SQL记录器生成日志事件。

但是在离开String Batch Step之前我没有看到insert SQL语句。使用调试器行走显示insert仅在以下情况下记录:

package org.springframework.transaction.support;

public class TransactionTemplate extends DefaultTransactionDefinition
    ....
    @Override
    public <T> T execute(TransactionCallback<T> action) throws TransactionException {
        ...
        this.transactionManager.commit(status);

推迟insert可以吗?为什么Spring / Hibernate没有在事务中实际insert而是延迟了这个动作?它是Spring还是Hibernate功能?

这一发现让我感到惊讶。在解决问题时我需要考虑到这一点......是否可以禁用此Spring Batch或Spring Data或Hibernate魔法?

UPDATE 以下是魔术点的追踪:

java.lang.Thread.State: RUNNABLE
  at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:151)
  at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:271)
  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)

0 个答案:

没有答案