From 9c9fe8f6ca9feeca6e5987aabc07ef74f16f93df Mon Sep 17 00:00:00 2001 From: robokaso Date: Tue, 20 Jan 2009 13:55:39 +0000 Subject: [PATCH] OPEN - BATCH-999: JobExecution ExecutionContext should not be persisted by Step? fix JobRepositoryUtilsTests refactor internals of JdbcExecutionContextDao --- .../dao/JdbcExecutionContextDao.java | 77 ++++++++----------- .../dao/AbstractExecutionContextDaoTests.java | 18 ++--- .../SimpleJobRepositoryIntegrationTests.java | 3 - .../batch/test/JobRepositoryTestUtils.java | 2 +- 4 files changed, 42 insertions(+), 58 deletions(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcExecutionContextDao.java b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcExecutionContextDao.java index f9b1a9505..4e23dbc14 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcExecutionContextDao.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcExecutionContextDao.java @@ -46,26 +46,20 @@ import org.springframework.util.Assert; */ public class JdbcExecutionContextDao extends AbstractJdbcBatchMetadataDao implements ExecutionContextDao { - private static final String COUNT_JOB_EXECUTION_CONTEXT = "SELECT COUNT(*) FROM %PREFIX%JOB_EXECUTION_CONTEXT " - + "WHERE JOB_EXECUTION_ID = ?"; - private static final String FIND_JOB_EXECUTION_CONTEXT = "SELECT SHORT_CONTEXT, SERIALIZED_CONTEXT " + "FROM %PREFIX%JOB_EXECUTION_CONTEXT WHERE JOB_EXECUTION_ID = ?"; private static final String INSERT_JOB_EXECUTION_CONTEXT = "INSERT INTO %PREFIX%JOB_EXECUTION_CONTEXT " - + "(JOB_EXECUTION_ID, SHORT_CONTEXT, SERIALIZED_CONTEXT) " + "VALUES(?, ?, ?)"; + + "(SHORT_CONTEXT, SERIALIZED_CONTEXT, JOB_EXECUTION_ID) " + "VALUES(?, ?, ?)"; private static final String UPDATE_JOB_EXECUTION_CONTEXT = "UPDATE %PREFIX%JOB_EXECUTION_CONTEXT " + "SET SHORT_CONTEXT = ?, SERIALIZED_CONTEXT = ? " + "WHERE JOB_EXECUTION_ID = ?"; - private static final String COUNT_STEP_EXECUTION_CONTEXT = "SELECT COUNT(*) FROM %PREFIX%STEP_EXECUTION_CONTEXT " - + "WHERE STEP_EXECUTION_ID = ?"; - private static final String FIND_STEP_EXECUTION_CONTEXT = "SELECT SHORT_CONTEXT, SERIALIZED_CONTEXT " + "FROM %PREFIX%STEP_EXECUTION_CONTEXT WHERE STEP_EXECUTION_ID = ?"; private static final String INSERT_STEP_EXECUTION_CONTEXT = "INSERT INTO %PREFIX%STEP_EXECUTION_CONTEXT " - + "(STEP_EXECUTION_ID, SHORT_CONTEXT, SERIALIZED_CONTEXT) " + "VALUES(?, ?, ?)"; + + "(SHORT_CONTEXT, SERIALIZED_CONTEXT, STEP_EXECUTION_ID) " + "VALUES(?, ?, ?)"; private static final String UPDATE_STEP_EXECUTION_CONTEXT = "UPDATE %PREFIX%STEP_EXECUTION_CONTEXT " + "SET SHORT_CONTEXT = ?, SERIALIZED_CONTEXT = ? " + "WHERE STEP_EXECUTION_ID = ?"; @@ -125,7 +119,7 @@ public class JdbcExecutionContextDao extends AbstractJdbcBatchMetadataDao implem String serializedContext = serializeContext(executionContext); - persistSerializedContext(executionId, serializedContext, true); + persistSerializedContext(executionId, serializedContext, UPDATE_JOB_EXECUTION_CONTEXT); } /** @@ -142,15 +136,30 @@ public class JdbcExecutionContextDao extends AbstractJdbcBatchMetadataDao implem String serializedContext = serializeContext(executionContext); - persistSerializedContext(executionId, serializedContext, false); + persistSerializedContext(executionId, serializedContext, UPDATE_STEP_EXECUTION_CONTEXT); } public void saveExecutionContext(JobExecution jobExecution) { - updateExecutionContext(jobExecution); + + Long executionId = jobExecution.getId(); + ExecutionContext executionContext = jobExecution.getExecutionContext(); + Assert.notNull(executionId, "ExecutionId must not be null."); + Assert.notNull(executionContext, "The ExecutionContext must not be null."); + + String serializedContext = serializeContext(executionContext); + + persistSerializedContext(executionId, serializedContext, INSERT_JOB_EXECUTION_CONTEXT); } public void saveExecutionContext(StepExecution stepExecution) { - updateExecutionContext(stepExecution); + Long executionId = stepExecution.getId(); + ExecutionContext executionContext = stepExecution.getExecutionContext(); + Assert.notNull(executionId, "ExecutionId must not be null."); + Assert.notNull(executionContext, "The ExecutionContext must not be null."); + + String serializedContext = serializeContext(executionContext); + + persistSerializedContext(executionId, serializedContext, INSERT_STEP_EXECUTION_CONTEXT); } public void setLobHandler(LobHandler lobHandler) { @@ -164,13 +173,7 @@ public class JdbcExecutionContextDao extends AbstractJdbcBatchMetadataDao implem ((XStreamExecutionContextStringSerializer) serializer).afterPropertiesSet(); } - private void persistSerializedContext(final Long executionId, String serializedContext, - boolean isJobExecutionContext) { - String countSql = isJobExecutionContext ? COUNT_JOB_EXECUTION_CONTEXT : COUNT_STEP_EXECUTION_CONTEXT; - String updateSql = isJobExecutionContext ? UPDATE_JOB_EXECUTION_CONTEXT : UPDATE_STEP_EXECUTION_CONTEXT; - String insertSql = isJobExecutionContext ? INSERT_JOB_EXECUTION_CONTEXT : INSERT_STEP_EXECUTION_CONTEXT; - - int count = getJdbcTemplate().queryForInt(getQuery(countSql), executionId); + private void persistSerializedContext(final Long executionId, String serializedContext, String sql) { final String shortContext; final String longContext; @@ -183,34 +186,18 @@ public class JdbcExecutionContextDao extends AbstractJdbcBatchMetadataDao implem longContext = null; } - if (count > 0) { - getJdbcTemplate().getJdbcOperations().update(getQuery(updateSql), new PreparedStatementSetter() { - public void setValues(PreparedStatement ps) throws SQLException { - ps.setString(1, shortContext); - if (longContext != null) { - lobHandler.getLobCreator().setClobAsString(ps, 2, longContext); - } - else { - ps.setNull(2, Types.CLOB); - } - ps.setLong(3, executionId); + getJdbcTemplate().getJdbcOperations().update(getQuery(sql), new PreparedStatementSetter() { + public void setValues(PreparedStatement ps) throws SQLException { + ps.setString(1, shortContext); + if (longContext != null) { + lobHandler.getLobCreator().setClobAsString(ps, 2, longContext); } - }); - } - else { - getJdbcTemplate().getJdbcOperations().update(getQuery(insertSql), new PreparedStatementSetter() { - public void setValues(PreparedStatement ps) throws SQLException { - ps.setLong(1, executionId); - ps.setString(2, shortContext); - if (longContext != null) { - lobHandler.getLobCreator().setClobAsString(ps, 3, longContext); - } - else { - ps.setNull(3, Types.CLOB); - } + else { + ps.setNull(2, Types.CLOB); } - }); - } + ps.setLong(3, executionId); + } + }); } private String serializeContext(ExecutionContext ctx) { diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/repository/dao/AbstractExecutionContextDaoTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/dao/AbstractExecutionContextDaoTests.java index 7714f38df..43cfe8be9 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/repository/dao/AbstractExecutionContextDaoTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/dao/AbstractExecutionContextDaoTests.java @@ -72,11 +72,11 @@ public abstract class AbstractExecutionContextDaoTests extends AbstractTransacti @Transactional @Test - public void testSaveAndFindContext() { + public void testSaveAndFindJobContext() { ExecutionContext ctx = new ExecutionContext(Collections. singletonMap("key", "value")); jobExecution.setExecutionContext(ctx); - contextDao.updateExecutionContext(jobExecution); + contextDao.saveExecutionContext(jobExecution); ExecutionContext retrieved = contextDao.getExecutionContext(jobExecution); assertEquals(ctx, retrieved); @@ -84,11 +84,11 @@ public abstract class AbstractExecutionContextDaoTests extends AbstractTransacti @Transactional @Test - public void testSaveAndFindEmptyContext() { + public void testSaveAndFindEmptyJobContext() { ExecutionContext ctx = new ExecutionContext(); jobExecution.setExecutionContext(ctx); - contextDao.updateExecutionContext(jobExecution); + contextDao.saveExecutionContext(jobExecution); ExecutionContext retrieved = contextDao.getExecutionContext(jobExecution); assertEquals(ctx, retrieved); @@ -101,7 +101,7 @@ public abstract class AbstractExecutionContextDaoTests extends AbstractTransacti ExecutionContext ctx = new ExecutionContext(Collections . singletonMap("key", "value")); jobExecution.setExecutionContext(ctx); - contextDao.updateExecutionContext(jobExecution); + contextDao.saveExecutionContext(jobExecution); ctx.putLong("longKey", 7); contextDao.updateExecutionContext(jobExecution); @@ -117,7 +117,7 @@ public abstract class AbstractExecutionContextDaoTests extends AbstractTransacti ExecutionContext ctx = new ExecutionContext(Collections. singletonMap("key", "value")); stepExecution.setExecutionContext(ctx); - contextDao.updateExecutionContext(stepExecution); + contextDao.saveExecutionContext(stepExecution); ExecutionContext retrieved = contextDao.getExecutionContext(stepExecution); assertEquals(ctx, retrieved); @@ -129,7 +129,7 @@ public abstract class AbstractExecutionContextDaoTests extends AbstractTransacti ExecutionContext ctx = new ExecutionContext(); stepExecution.setExecutionContext(ctx); - contextDao.updateExecutionContext(stepExecution); + contextDao.saveExecutionContext(stepExecution); ExecutionContext retrieved = contextDao.getExecutionContext(stepExecution); assertEquals(ctx, retrieved); @@ -141,7 +141,7 @@ public abstract class AbstractExecutionContextDaoTests extends AbstractTransacti ExecutionContext ctx = new ExecutionContext(Collections. singletonMap("key", "value")); stepExecution.setExecutionContext(ctx); - contextDao.updateExecutionContext(stepExecution); + contextDao.saveExecutionContext(stepExecution); ctx.putLong("longKey", 7); contextDao.updateExecutionContext(stepExecution); @@ -158,7 +158,7 @@ public abstract class AbstractExecutionContextDaoTests extends AbstractTransacti ExecutionContext ec = new ExecutionContext(); ec.put("intValue", new Integer(343232)); stepExecution.setExecutionContext(ec); - contextDao.updateExecutionContext(stepExecution); + contextDao.saveExecutionContext(stepExecution); ExecutionContext restoredEc = contextDao.getExecutionContext(stepExecution); assertEquals(ec, restoredEc); } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/SimpleJobRepositoryIntegrationTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/SimpleJobRepositoryIntegrationTests.java index 31c51a743..48370485b 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/SimpleJobRepositoryIntegrationTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/SimpleJobRepositoryIntegrationTests.java @@ -105,7 +105,6 @@ public class SimpleJobRepositoryIntegrationTests { // first execution JobExecution firstJobExec = jobRepository.createJobExecution(job.getName(), jobParameters); StepExecution firstStepExec = new StepExecution(step.getName(), firstJobExec); - jobRepository.update(firstJobExec); jobRepository.add(firstStepExec); assertEquals(1, jobRepository.getStepExecutionCount(firstJobExec.getJobInstance(), step.getName())); @@ -124,7 +123,6 @@ public class SimpleJobRepositoryIntegrationTests { // second execution JobExecution secondJobExec = jobRepository.createJobExecution(job.getName(), jobParameters); StepExecution secondStepExec = new StepExecution(step.getName(), secondJobExec); - jobRepository.update(secondJobExec); jobRepository.add(secondStepExec); assertEquals(2, jobRepository.getStepExecutionCount(secondJobExec.getJobInstance(), step.getName())); @@ -150,7 +148,6 @@ public class SimpleJobRepositoryIntegrationTests { stepExec.setExecutionContext(ctx); jobRepository.add(stepExec); - jobRepository.updateExecutionContext(stepExec); StepExecution retrievedStepExec = jobRepository.getLastStepExecution(jobExec.getJobInstance(), step.getName()); assertEquals(stepExec, retrievedStepExec); diff --git a/spring-batch-test/src/main/java/org/springframework/batch/test/JobRepositoryTestUtils.java b/spring-batch-test/src/main/java/org/springframework/batch/test/JobRepositoryTestUtils.java index d9507263e..e52e01075 100644 --- a/spring-batch-test/src/main/java/org/springframework/batch/test/JobRepositoryTestUtils.java +++ b/spring-batch-test/src/main/java/org/springframework/batch/test/JobRepositoryTestUtils.java @@ -147,9 +147,9 @@ public class JobRepositoryTestUtils implements InitializingBean { stepExecutionId); jdbcTemplate.update("delete from BATCH_STEP_EXECUTION where STEP_EXECUTION_ID=?", stepExecutionId); } - jdbcTemplate.update("delete from BATCH_JOB_EXECUTION where JOB_EXECUTION_ID=?", jobExecution.getId()); jdbcTemplate.update("delete from BATCH_JOB_EXECUTION_CONTEXT where JOB_EXECUTION_ID=?", jobExecution .getId()); + jdbcTemplate.update("delete from BATCH_JOB_EXECUTION where JOB_EXECUTION_ID=?", jobExecution.getId()); jdbcTemplate.update("delete from BATCH_JOB_PARAMS where JOB_INSTANCE_ID=?", jobExecution.getJobId()); jdbcTemplate.update("delete from BATCH_JOB_INSTANCE where JOB_INSTANCE_ID=?", jobExecution.getJobId()); }