From d33065b15ce783f9af1c0d63f14ac0623dd7ee8f Mon Sep 17 00:00:00 2001 From: dsyer Date: Tue, 13 Nov 2007 14:16:07 +0000 Subject: [PATCH] RESOLVED - issue BATCH-184: sample jobs don't work with hibernate repository http://opensource.atlassian.com/projects/spring/browse/BATCH-184 --- .../repository/dao/BatchStatusUserType.java | 2 +- .../repository/dao/HibernateJobDao.java | 7 +- .../repository/dao/JobInstance.hbm.xml | 2 +- .../repository/dao/AbstractJobDaoTests.java | 5 +- .../repository/dao/HibernateJobDaoTests.java | 64 +++++++++++++------ .../repository/dao/HibernateStepDaoTests.java | 25 ++++++++ samples/src/main/resources/batch.properties | 7 ++ .../main/resources/data-source-context.xml | 2 +- .../src/main/resources/hibernate-context.xml | 35 ++++++++++ .../resources/simple-container-definition.xml | 25 ++++++-- 10 files changed, 139 insertions(+), 35 deletions(-) create mode 100644 samples/src/main/resources/hibernate-context.xml diff --git a/execution/src/main/java/org/springframework/batch/execution/repository/dao/BatchStatusUserType.java b/execution/src/main/java/org/springframework/batch/execution/repository/dao/BatchStatusUserType.java index 8e97f800f..9f73de74b 100644 --- a/execution/src/main/java/org/springframework/batch/execution/repository/dao/BatchStatusUserType.java +++ b/execution/src/main/java/org/springframework/batch/execution/repository/dao/BatchStatusUserType.java @@ -58,7 +58,7 @@ public class BatchStatusUserType extends ImmutableValueUserType { */ public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException { - st.setString(index, value.toString()); + st.setString(index, value!=null ? value.toString() : null); } /* (non-Javadoc) diff --git a/execution/src/main/java/org/springframework/batch/execution/repository/dao/HibernateJobDao.java b/execution/src/main/java/org/springframework/batch/execution/repository/dao/HibernateJobDao.java index a06262940..e4f2ae544 100644 --- a/execution/src/main/java/org/springframework/batch/execution/repository/dao/HibernateJobDao.java +++ b/execution/src/main/java/org/springframework/batch/execution/repository/dao/HibernateJobDao.java @@ -146,12 +146,15 @@ public class HibernateJobDao extends HibernateDaoSupport implements JobDao { + "before it can be updated."); } - if (getHibernateTemplate() - .get(JobExecution.class, jobExecution.getId()) == null) { + JobExecution other = (JobExecution) getHibernateTemplate() + .get(JobExecution.class, jobExecution.getId()); + if (other == null) { throw new NoSuchBatchDomainObjectException( "Invalid JobExecution, ID " + jobExecution.getId() + " not found."); } + + getHibernateTemplate().evict(other); getHibernateTemplate().update(jobExecution); } diff --git a/execution/src/main/resources/org/springframework/batch/execution/repository/dao/JobInstance.hbm.xml b/execution/src/main/resources/org/springframework/batch/execution/repository/dao/JobInstance.hbm.xml index d9b6e2491..0ad7df1d4 100644 --- a/execution/src/main/resources/org/springframework/batch/execution/repository/dao/JobInstance.hbm.xml +++ b/execution/src/main/resources/org/springframework/batch/execution/repository/dao/JobInstance.hbm.xml @@ -14,7 +14,7 @@ diff --git a/execution/src/test/java/org/springframework/batch/execution/repository/dao/AbstractJobDaoTests.java b/execution/src/test/java/org/springframework/batch/execution/repository/dao/AbstractJobDaoTests.java index a58e7d201..2e0a9d964 100644 --- a/execution/src/test/java/org/springframework/batch/execution/repository/dao/AbstractJobDaoTests.java +++ b/execution/src/test/java/org/springframework/batch/execution/repository/dao/AbstractJobDaoTests.java @@ -177,12 +177,11 @@ public abstract class AbstractJobDaoTests extends public void testUpdateInvalidJobExecution() { - JobExecution execution = new JobExecution(job); // id is invalid - execution.setId(new Long(29432)); + JobExecution execution = new JobExecution(job, new Long(29432)); try { jobDao.update(execution); - fail(); + fail("Expected NoSuchBatchDomainObjectException"); } catch (NoSuchBatchDomainObjectException ex) { // expected } diff --git a/execution/src/test/java/org/springframework/batch/execution/repository/dao/HibernateJobDaoTests.java b/execution/src/test/java/org/springframework/batch/execution/repository/dao/HibernateJobDaoTests.java index 4a10764a8..f75817cfb 100644 --- a/execution/src/test/java/org/springframework/batch/execution/repository/dao/HibernateJobDaoTests.java +++ b/execution/src/test/java/org/springframework/batch/execution/repository/dao/HibernateJobDaoTests.java @@ -32,11 +32,12 @@ public class HibernateJobDaoTests extends AbstractJobDaoTests { private SessionFactory sessionFactory; - protected String[] getConfigLocations(){ - return new String[] { ClassUtils.addResourcePathToPackagePath(getClass(), "hibernate-dao-test.xml") }; + protected String[] getConfigLocations() { + return new String[] { ClassUtils.addResourcePathToPackagePath( + getClass(), "hibernate-dao-test.xml") }; } - public void setSessionFactory(SessionFactory sessionFactory){ + public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } @@ -48,12 +49,34 @@ public class HibernateJobDaoTests extends AbstractJobDaoTests { sessionFactory.getCurrentSession().flush(); - List executions = jdbcTemplate.queryForList("SELECT * FROM BATCH_JOB_EXECUTION where JOB_ID=?", new Object[] {job.getId()}); + List executions = jdbcTemplate.queryForList( + "SELECT * FROM BATCH_JOB_EXECUTION where JOB_ID=?", + new Object[] { job.getId() }); assertEquals(1, executions.size()); - assertEquals(jobExecution.getEndTime(), ((Map)executions.get(0)).get("END_TIME")); + assertEquals(jobExecution.getEndTime(), ((Map) executions.get(0)) + .get("END_TIME")); } - public void testCreateSimpleJobExecution(){ + public void testUpdateDetachedJobExecution() { + + sessionFactory.getCurrentSession().evict(jobExecution); + + jobExecution.setStatus(BatchStatus.COMPLETED); + jobExecution.setEndTime(new Timestamp(System.currentTimeMillis())); + jobDao.update(jobExecution); + + sessionFactory.getCurrentSession().flush(); + + List executions = jdbcTemplate.queryForList( + "SELECT * FROM BATCH_JOB_EXECUTION where JOB_ID=?", + new Object[] { job.getId() }); + assertEquals(1, executions.size()); + assertEquals(jobExecution.getEndTime(), ((Map) executions.get(0)) + .get("END_TIME")); + + } + + public void testCreateSimpleJobExecution() { JobIdentifier simpleIdentifier = new SimpleJobIdentifier("SimpleJob"); @@ -62,45 +85,46 @@ public class HibernateJobDaoTests extends AbstractJobDaoTests { List jobs = jobDao.findJobs(simpleIdentifier); assertEquals(jobs.size(), 1); - JobInstance testJob = (JobInstance)jobs.get(0); + JobInstance testJob = (JobInstance) jobs.get(0); assertEquals(simpleJob, testJob); } - public void testNullIdentifierName(){ + public void testNullIdentifierName() { JobIdentifier simpleIdentifier = new SimpleJobIdentifier(null); - try{ + try { jobDao.createJob(simpleIdentifier); fail(); - }catch(IllegalArgumentException ex){ - //expected + } catch (IllegalArgumentException ex) { + // expected } } - public void testEmptyIdentifierName(){ + public void testEmptyIdentifierName() { JobIdentifier simpleIdentifier = new SimpleJobIdentifier(""); - try{ + try { jobDao.createJob(simpleIdentifier); fail(); - }catch(IllegalArgumentException ex){ - //expected + } catch (IllegalArgumentException ex) { + // expected } } - public void testNullScheduleDate(){ + public void testNullScheduleDate() { - ScheduledJobIdentifier scheduledIdentifier = new ScheduledJobIdentifier("ScheduledJob"); + ScheduledJobIdentifier scheduledIdentifier = new ScheduledJobIdentifier( + "ScheduledJob"); scheduledIdentifier.setJobKey(null); - try{ + try { jobDao.createJob(scheduledIdentifier); fail(); - }catch(IllegalArgumentException ex){ - //expected + } catch (IllegalArgumentException ex) { + // expected } } diff --git a/execution/src/test/java/org/springframework/batch/execution/repository/dao/HibernateStepDaoTests.java b/execution/src/test/java/org/springframework/batch/execution/repository/dao/HibernateStepDaoTests.java index dc88ecb1f..cabd823ea 100644 --- a/execution/src/test/java/org/springframework/batch/execution/repository/dao/HibernateStepDaoTests.java +++ b/execution/src/test/java/org/springframework/batch/execution/repository/dao/HibernateStepDaoTests.java @@ -16,9 +16,13 @@ package org.springframework.batch.execution.repository.dao; +import java.sql.Timestamp; +import java.util.List; +import java.util.Map; import java.util.Properties; import org.hibernate.SessionFactory; +import org.springframework.batch.core.domain.BatchStatus; import org.springframework.batch.core.domain.JobExecution; import org.springframework.batch.core.domain.StepExecution; import org.springframework.batch.core.domain.StepInstance; @@ -54,4 +58,25 @@ public class HibernateStepDaoTests extends AbstractStepDaoTests { //assertEquals("x=y, a=b", returnedStatistics); assertEquals(fromDb, statistics); } + + public void testUpdateDetachedStepExecution() { + + sessionFactory.getCurrentSession().evict(stepExecution); + + stepExecution.setStatus(BatchStatus.COMPLETED); + stepExecution.setEndTime(new Timestamp(System.currentTimeMillis())); + stepDao.update(stepExecution); + + sessionFactory.getCurrentSession().flush(); + + List executions = jdbcTemplate.queryForList( + "SELECT * FROM BATCH_STEP_EXECUTION where STEP_ID=?", + new Object[] { step1.getId() }); + assertEquals(1, executions.size()); + assertEquals(stepExecution.getEndTime(), ((Map) executions.get(0)) + .get("END_TIME")); + + } + + } diff --git a/samples/src/main/resources/batch.properties b/samples/src/main/resources/batch.properties index a59281d72..073b6d057 100644 --- a/samples/src/main/resources/batch.properties +++ b/samples/src/main/resources/batch.properties @@ -21,3 +21,10 @@ batch.database.incrementer.class=org.springframework.jdbc.support.incrementer.Hs # for HSQLDB: incrementerParent.columnName=ID +# Switch Dao implementations +# batch.jobdao=hibernateJobDao +# batch.stepdao=hibernateStepDao +# batch.transaction.manager=hibernateTransactionManager +batch.jobdao=sqlJobDao +batch.stepdao=sqlStepDao +batch.transaction.manager=sqlTransactionManager diff --git a/samples/src/main/resources/data-source-context.xml b/samples/src/main/resources/data-source-context.xml index 4645b978a..ecd8fa052 100644 --- a/samples/src/main/resources/data-source-context.xml +++ b/samples/src/main/resources/data-source-context.xml @@ -15,7 +15,7 @@ - + diff --git a/samples/src/main/resources/hibernate-context.xml b/samples/src/main/resources/hibernate-context.xml new file mode 100644 index 000000000..202fd8e04 --- /dev/null +++ b/samples/src/main/resources/hibernate-context.xml @@ -0,0 +1,35 @@ + + + + + + + classpath:/org/springframework/batch/execution/repository/dao/JobInstance.hbm.xml + classpath:/org/springframework/batch/execution/repository/dao/JobExecution.hbm.xml + classpath:/org/springframework/batch/execution/repository/dao/StepInstance.hbm.xml + classpath:/org/springframework/batch/execution/repository/dao/StepExecution.hbm.xml + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/samples/src/main/resources/simple-container-definition.xml b/samples/src/main/resources/simple-container-definition.xml index c7536c1bc..4cae0532e 100644 --- a/samples/src/main/resources/simple-container-definition.xml +++ b/samples/src/main/resources/simple-container-definition.xml @@ -10,6 +10,7 @@ http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> + - + @@ -58,17 +59,17 @@ - + - - + + - @@ -76,13 +77,23 @@ ref="jobExecutionIncrementer" /> - - + + + + + + + + +