From 23385650107406897321da9fb42e7c48c06a46c1 Mon Sep 17 00:00:00 2001 From: dsyer Date: Tue, 26 Feb 2008 15:48:14 +0000 Subject: [PATCH] RESOLVED - issue BATCH-344: A JobExecution does not need a start time until it actually starts http://jira.springframework.org/browse/BATCH-344 --- .../batch/core/domain/JobExecution.java | 2 +- .../batch/execution/job/simple/SimpleJob.java | 1 + .../repository/SimpleJobRepository.java | 4 ++-- .../repository/dao/JdbcJobExecutionDao.java | 1 - .../src/main/resources/schema-db2.sql | 2 +- .../src/main/resources/schema-derby.sql | 2 +- .../src/main/resources/schema-hsqldb.sql | 2 +- .../src/main/resources/schema-mysql.sql | 2 +- .../src/main/resources/schema-oracle10g.sql | 2 +- .../src/main/resources/schema-postgresql.sql | 2 +- .../src/main/sql/init.sql.vpp | 2 +- .../execution/job/simple/SimpleJobTests.java | 2 ++ .../SimpleJobRepositoryIntegrationTests.java | 20 +++++++++++++++++++ .../batch/execution/repository/dao/init.sql | 2 +- 14 files changed, 34 insertions(+), 12 deletions(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/domain/JobExecution.java b/spring-batch-core/src/main/java/org/springframework/batch/core/domain/JobExecution.java index 5e7378549..361d68b5e 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/domain/JobExecution.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/domain/JobExecution.java @@ -37,7 +37,7 @@ public class JobExecution extends Entity { private BatchStatus status = BatchStatus.STARTING; - private Date startTime = new Date(System.currentTimeMillis()); + private Date startTime = null; private Date endTime = null; diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/job/simple/SimpleJob.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/job/simple/SimpleJob.java index 5b5be4a7c..70aa8fc73 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/job/simple/SimpleJob.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/job/simple/SimpleJob.java @@ -69,6 +69,7 @@ public class SimpleJob extends JobSupport { throw new JobInterruptedException("JobExecution already stopped before being executed."); } + execution.setStartTime(new Date()); updateStatus(execution, BatchStatus.STARTING); int startedCount = 0; diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/SimpleJobRepository.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/SimpleJobRepository.java index 55d938432..f27b68ac5 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/SimpleJobRepository.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/SimpleJobRepository.java @@ -178,7 +178,8 @@ public class SimpleJobRepository implements JobRepository { if (lastExecution == null) { lastExecution = execution; } - if (lastExecution.getStartTime().getTime() < execution.getStartTime().getTime()) { + if (execution.getStartTime() != null && lastExecution.getStartTime() != null + && lastExecution.getStartTime().getTime() < execution.getStartTime().getTime()) { lastExecution = execution; } @@ -203,7 +204,6 @@ public class SimpleJobRepository implements JobRepository { } - private JobExecution generateJobExecution(JobInstance jobInstance) { JobExecution execution = jobInstance.createJobExecution(); // Save the JobExecution so that it picks up an ID (useful for clients diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/JdbcJobExecutionDao.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/JdbcJobExecutionDao.java index 7cd41b0c1..4d72406ab 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/JdbcJobExecutionDao.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/JdbcJobExecutionDao.java @@ -114,7 +114,6 @@ public class JdbcJobExecutionDao extends AbstractJdbcBatchMetadataDao implements Assert.notNull(jobExecution); Assert.notNull(jobExecution.getJobId(), "JobExecution Job-Id cannot be null."); - Assert.notNull(jobExecution.getStartTime(), "JobExecution start time cannot be null."); Assert.notNull(jobExecution.getStatus(), "JobExecution status cannot be null."); } diff --git a/spring-batch-execution/src/main/resources/schema-db2.sql b/spring-batch-execution/src/main/resources/schema-db2.sql index dae12dd84..37f25363e 100644 --- a/spring-batch-execution/src/main/resources/schema-db2.sql +++ b/spring-batch-execution/src/main/resources/schema-db2.sql @@ -20,7 +20,7 @@ CREATE TABLE BATCH_JOB_EXECUTION ( JOB_EXECUTION_ID BIGINT PRIMARY KEY , VERSION BIGINT, JOB_INSTANCE_ID BIGINT NOT NULL, - START_TIME TIMESTAMP NOT NULL , + START_TIME TIMESTAMP, END_TIME TIMESTAMP , STATUS VARCHAR(10), CONTINUABLE CHAR(1), diff --git a/spring-batch-execution/src/main/resources/schema-derby.sql b/spring-batch-execution/src/main/resources/schema-derby.sql index cfbc1e887..6304572eb 100644 --- a/spring-batch-execution/src/main/resources/schema-derby.sql +++ b/spring-batch-execution/src/main/resources/schema-derby.sql @@ -20,7 +20,7 @@ CREATE TABLE BATCH_JOB_EXECUTION ( JOB_EXECUTION_ID BIGINT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, VERSION BIGINT, JOB_INSTANCE_ID BIGINT NOT NULL, - START_TIME TIMESTAMP NOT NULL , + START_TIME TIMESTAMP, END_TIME TIMESTAMP , STATUS VARCHAR(10), CONTINUABLE CHAR(1), diff --git a/spring-batch-execution/src/main/resources/schema-hsqldb.sql b/spring-batch-execution/src/main/resources/schema-hsqldb.sql index c42ed8ab8..fdf777822 100644 --- a/spring-batch-execution/src/main/resources/schema-hsqldb.sql +++ b/spring-batch-execution/src/main/resources/schema-hsqldb.sql @@ -20,7 +20,7 @@ CREATE TABLE BATCH_JOB_EXECUTION ( JOB_EXECUTION_ID BIGINT IDENTITY PRIMARY KEY , VERSION BIGINT, JOB_INSTANCE_ID BIGINT NOT NULL, - START_TIME TIMESTAMP NOT NULL , + START_TIME TIMESTAMP, END_TIME TIMESTAMP , STATUS VARCHAR(10), CONTINUABLE CHAR(1), diff --git a/spring-batch-execution/src/main/resources/schema-mysql.sql b/spring-batch-execution/src/main/resources/schema-mysql.sql index 56ef8fda9..71f460e86 100644 --- a/spring-batch-execution/src/main/resources/schema-mysql.sql +++ b/spring-batch-execution/src/main/resources/schema-mysql.sql @@ -20,7 +20,7 @@ CREATE TABLE BATCH_JOB_EXECUTION ( JOB_EXECUTION_ID BIGINT unsigned PRIMARY KEY , VERSION BIGINT, JOB_INSTANCE_ID BIGINT NOT NULL, - START_TIME TIMESTAMP NOT NULL , + START_TIME TIMESTAMP, END_TIME TIMESTAMP , STATUS VARCHAR(10), CONTINUABLE CHAR(1), diff --git a/spring-batch-execution/src/main/resources/schema-oracle10g.sql b/spring-batch-execution/src/main/resources/schema-oracle10g.sql index ce7e67582..0216a0253 100644 --- a/spring-batch-execution/src/main/resources/schema-oracle10g.sql +++ b/spring-batch-execution/src/main/resources/schema-oracle10g.sql @@ -20,7 +20,7 @@ CREATE TABLE BATCH_JOB_EXECUTION ( JOB_EXECUTION_ID NUMBER(38) PRIMARY KEY , VERSION NUMBER(38), JOB_INSTANCE_ID NUMBER(38) NOT NULL, - START_TIME TIMESTAMP NOT NULL , + START_TIME TIMESTAMP, END_TIME TIMESTAMP , STATUS VARCHAR(10), CONTINUABLE CHAR(1), diff --git a/spring-batch-execution/src/main/resources/schema-postgresql.sql b/spring-batch-execution/src/main/resources/schema-postgresql.sql index dae12dd84..37f25363e 100644 --- a/spring-batch-execution/src/main/resources/schema-postgresql.sql +++ b/spring-batch-execution/src/main/resources/schema-postgresql.sql @@ -20,7 +20,7 @@ CREATE TABLE BATCH_JOB_EXECUTION ( JOB_EXECUTION_ID BIGINT PRIMARY KEY , VERSION BIGINT, JOB_INSTANCE_ID BIGINT NOT NULL, - START_TIME TIMESTAMP NOT NULL , + START_TIME TIMESTAMP, END_TIME TIMESTAMP , STATUS VARCHAR(10), CONTINUABLE CHAR(1), diff --git a/spring-batch-execution/src/main/sql/init.sql.vpp b/spring-batch-execution/src/main/sql/init.sql.vpp index 6894f5ea0..cc40c5c05 100644 --- a/spring-batch-execution/src/main/sql/init.sql.vpp +++ b/spring-batch-execution/src/main/sql/init.sql.vpp @@ -9,7 +9,7 @@ CREATE TABLE BATCH_JOB_EXECUTION ( JOB_EXECUTION_ID ${BIGINT} $!{IDENTITY} PRIMARY KEY $!{GENERATED}, VERSION ${BIGINT}, JOB_INSTANCE_ID ${BIGINT} NOT NULL, - START_TIME TIMESTAMP NOT NULL , + START_TIME TIMESTAMP, END_TIME TIMESTAMP , STATUS VARCHAR(10), CONTINUABLE CHAR(1), diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/job/simple/SimpleJobTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/job/simple/SimpleJobTests.java index fde209747..fec02f292 100644 --- a/spring-batch-execution/src/test/java/org/springframework/batch/execution/job/simple/SimpleJobTests.java +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/job/simple/SimpleJobTests.java @@ -133,6 +133,8 @@ public class SimpleJobTests extends TestCase { job.execute(jobExecution); assertEquals(2, list.size()); checkRepository(BatchStatus.COMPLETED); + assertNotNull(jobExecution.getEndTime()); + assertNotNull(jobExecution.getStartTime()); } public void testRunWithSimpleStepExecutor() throws Exception { diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/SimpleJobRepositoryIntegrationTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/SimpleJobRepositoryIntegrationTests.java index 0f575437a..b82f458d7 100644 --- a/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/SimpleJobRepositoryIntegrationTests.java +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/SimpleJobRepositoryIntegrationTests.java @@ -54,6 +54,7 @@ public class SimpleJobRepositoryIntegrationTests extends AbstractTransactionalDa JobParameters jobParams = new JobParameters(stringParams, longParams, dateParams); JobExecution firstExecution = jobRepository.createJobExecution(job, jobParams); + firstExecution.setStartTime(new Date()); assertEquals(job, firstExecution.getJobInstance().getJob()); @@ -65,4 +66,23 @@ public class SimpleJobRepositoryIntegrationTests extends AbstractTransactionalDa assertEquals(firstExecution.getJobInstance(), secondExecution.getJobInstance()); assertEquals(job, secondExecution.getJobInstance().getJob()); } + + /** + * Create two job executions for same job+parameters tuple. Check both + * executions belong to the same job instance and job. + */ + public void testCreateAndFindWithNoStartDate() throws Exception { + + JobSupport job = new JobSupport("testJob"); + job.setRestartable(true); + JobParameters jobParams = new JobParameters(); + + JobExecution firstExecution = jobRepository.createJobExecution(job, jobParams); + firstExecution.stop(); + jobRepository.saveOrUpdate(firstExecution); + JobExecution secondExecution = jobRepository.createJobExecution(job, jobParams); + + assertEquals(firstExecution.getJobInstance(), secondExecution.getJobInstance()); + assertEquals(job, secondExecution.getJobInstance().getJob()); + } } \ No newline at end of file diff --git a/spring-batch-execution/src/test/resources/org/springframework/batch/execution/repository/dao/init.sql b/spring-batch-execution/src/test/resources/org/springframework/batch/execution/repository/dao/init.sql index 76462d6fd..a03135b35 100644 --- a/spring-batch-execution/src/test/resources/org/springframework/batch/execution/repository/dao/init.sql +++ b/spring-batch-execution/src/test/resources/org/springframework/batch/execution/repository/dao/init.sql @@ -9,7 +9,7 @@ CREATE TABLE BATCH_JOB_EXECUTION ( JOB_EXECUTION_ID BIGINT IDENTITY PRIMARY KEY , VERSION BIGINT, JOB_INSTANCE_ID BIGINT NOT NULL, - START_TIME TIMESTAMP NOT NULL , + START_TIME TIMESTAMP, END_TIME TIMESTAMP , STATUS VARCHAR(10), CONTINUABLE CHAR(1),