RESOLVED - issue BATCH-184: sample jobs don't work with hibernate repository
http://opensource.atlassian.com/projects/spring/browse/BATCH-184
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
</id>
|
||||
|
||||
<discriminator
|
||||
formula="case when SCHEDULE_DATE is NULL then 'Scheduled' else 'Simple' end"
|
||||
formula="case when JOB_KEY is NULL then 'Simple' when SCHEDULE_DATE is NULL then 'Default' else 'Scheduled' end"
|
||||
type="string" />
|
||||
|
||||
<version name="version" access="field" />
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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"));
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
<property name="password" value="${batch.jdbc.password}" />
|
||||
</bean>
|
||||
|
||||
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
|
||||
<bean id="sqlTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" lazy-init="true">
|
||||
<property name="dataSource" ref="dataSource" />
|
||||
</bean>
|
||||
|
||||
|
||||
35
samples/src/main/resources/hibernate-context.xml
Normal file
35
samples/src/main/resources/hibernate-context.xml
Normal file
@@ -0,0 +1,35 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
|
||||
|
||||
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" lazy-init="true">
|
||||
<property name="mappingLocations">
|
||||
<list>
|
||||
<value>classpath:/org/springframework/batch/execution/repository/dao/JobInstance.hbm.xml</value>
|
||||
<value>classpath:/org/springframework/batch/execution/repository/dao/JobExecution.hbm.xml</value>
|
||||
<value>classpath:/org/springframework/batch/execution/repository/dao/StepInstance.hbm.xml</value>
|
||||
<value>classpath:/org/springframework/batch/execution/repository/dao/StepExecution.hbm.xml</value>
|
||||
</list>
|
||||
</property>
|
||||
<property name="hibernateProperties">
|
||||
<value><![CDATA[
|
||||
hibernate.show_sql=true
|
||||
hibernate.format_sql=true
|
||||
hibernate.dialect=org.hibernate.dialect.HSQLDialect
|
||||
]]></value>
|
||||
</property>
|
||||
<property name="dataSource" ref="dataSource" />
|
||||
<property name="lobHandler">
|
||||
<bean class="org.springframework.jdbc.support.lob.DefaultLobHandler"/>
|
||||
</property>
|
||||
<property name="entityInterceptor">
|
||||
<bean class="org.springframework.batch.execution.repository.dao.EntityNameInterceptor"/>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" lazy-init="true">
|
||||
<property name="sessionFactory" ref="sessionFactory" />
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -10,6 +10,7 @@
|
||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
|
||||
|
||||
<import resource="data-source-context.xml" />
|
||||
<import resource="hibernate-context.xml" />
|
||||
|
||||
<!-- register the step scope with the application context -->
|
||||
<bean id="stepScope"
|
||||
@@ -38,7 +39,7 @@
|
||||
advice-ref="txAdvice" />
|
||||
</aop:config>
|
||||
|
||||
<tx:advice id="txAdvice" transaction-manager="transactionManager">
|
||||
<tx:advice id="txAdvice" transaction-manager="${batch.transaction.manager}">
|
||||
<tx:attributes>
|
||||
<tx:method name="*" />
|
||||
</tx:attributes>
|
||||
@@ -58,17 +59,17 @@
|
||||
<bean id="stepExecutor"
|
||||
class="org.springframework.batch.execution.step.simple.SimpleStepExecutor"
|
||||
scope="prototype">
|
||||
<property name="transactionManager" ref="transactionManager" />
|
||||
<property name="transactionManager" ref="${batch.transaction.manager}" />
|
||||
<property name="repository" ref="simpleJobRepository" />
|
||||
</bean>
|
||||
|
||||
<bean id="simpleJobRepository"
|
||||
class="org.springframework.batch.execution.repository.SimpleJobRepository">
|
||||
<constructor-arg ref="jobDao" />
|
||||
<constructor-arg ref="stepDao" />
|
||||
<constructor-arg ref="${batch.jobdao}" />
|
||||
<constructor-arg ref="${batch.stepdao}" />
|
||||
</bean>
|
||||
|
||||
<bean id="jobDao"
|
||||
<bean id="sqlJobDao" lazy-init="true"
|
||||
class="org.springframework.batch.execution.repository.dao.SqlJobDao">
|
||||
<property name="jdbcTemplate" ref="jdbcTemplate" />
|
||||
<property name="jobIncrementer" ref="jobIncrementer" />
|
||||
@@ -76,13 +77,23 @@
|
||||
ref="jobExecutionIncrementer" />
|
||||
</bean>
|
||||
|
||||
<bean id="stepDao"
|
||||
<bean id="sqlStepDao" lazy-init="true"
|
||||
class="org.springframework.batch.execution.repository.dao.SqlStepDao">
|
||||
<property name="jdbcTemplate" ref="jdbcTemplate" />
|
||||
<property name="stepIncrementer" ref="stepIncrementer" />
|
||||
<property name="stepExecutionIncrementer"
|
||||
ref="stepExecutionIncrementer" />
|
||||
<property name="jobDao" ref="jobDao" />
|
||||
<property name="jobDao" ref="sqlJobDao" />
|
||||
</bean>
|
||||
|
||||
<bean id="hibernateJobDao" lazy-init="true"
|
||||
class="org.springframework.batch.execution.repository.dao.HibernateJobDao">
|
||||
<property name="sessionFactory" ref="sessionFactory"/>
|
||||
</bean>
|
||||
|
||||
<bean id="hibernateStepDao" lazy-init="true"
|
||||
class="org.springframework.batch.execution.repository.dao.HibernateStepDao">
|
||||
<property name="sessionFactory" ref="sessionFactory"/>
|
||||
</bean>
|
||||
|
||||
<bean id="jobRuntimeInformationFactory"
|
||||
|
||||
Reference in New Issue
Block a user