Reverted back to original prepared statement setter. The ExecutionContext wasn't doing much good in it, since other database reader changes weren't committed. However, being able to set the JobParameters still provides value for the cursor item reader.
This commit is contained in:
@@ -24,7 +24,6 @@ import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.StepExecutionListener;
|
||||
import org.springframework.batch.core.listener.StepExecutionListenerSupport;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.jdbc.core.PreparedStatementSetter;
|
||||
import org.springframework.jdbc.core.SqlTypeValue;
|
||||
@@ -44,18 +43,13 @@ public class StepExecutionPreparedStatementSetter extends StepExecutionListenerS
|
||||
|
||||
private List parameterKeys;
|
||||
private JobParameters jobParameters;
|
||||
private ExecutionContext executionContext;
|
||||
|
||||
public void setValues(PreparedStatement ps) throws SQLException {
|
||||
Map parameters = jobParameters.getParameters();
|
||||
for(int i = 0; i < parameterKeys.size(); i++){
|
||||
String parameterKey = parameterKeys.get(i).toString();
|
||||
Object arg = executionContext.get(parameterKey);
|
||||
Object arg = parameters.get(parameterKeys.get(i));
|
||||
if(arg == null){
|
||||
arg = parameters.get(parameterKey);
|
||||
if(arg == null){
|
||||
throw new IllegalStateException("No job parameter found for with key of: [" + parameterKeys.get(i) + "]");
|
||||
}
|
||||
throw new IllegalStateException("No job parameter found for with key of: [" + parameterKeys.get(i) + "]");
|
||||
}
|
||||
StatementCreatorUtils.setParameterValue(ps, i + 1, SqlTypeValue.TYPE_UNKNOWN, arg);
|
||||
}
|
||||
@@ -63,7 +57,6 @@ public class StepExecutionPreparedStatementSetter extends StepExecutionListenerS
|
||||
|
||||
public void beforeStep(StepExecution stepExecution) {
|
||||
this.jobParameters = stepExecution.getJobParameters();
|
||||
this.executionContext = stepExecution.getExecutionContext();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -27,7 +27,6 @@ import org.springframework.batch.core.JobParametersBuilder;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.job.SimpleJob;
|
||||
import org.springframework.batch.core.step.tasklet.TaskletStep;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowCallbackHandler;
|
||||
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
|
||||
@@ -36,7 +35,7 @@ import org.springframework.test.AbstractTransactionalDataSourceSpringContextTest
|
||||
* @author Lucas Ward
|
||||
*
|
||||
*/
|
||||
public class JobParametersPreparedStatementSetterTests extends AbstractTransactionalDataSourceSpringContextTests {
|
||||
public class StepExecutionPreparedStatementSetterTests extends AbstractTransactionalDataSourceSpringContextTests {
|
||||
|
||||
JdbcTemplate jdbcTemplate;
|
||||
StepExecutionPreparedStatementSetter pss;
|
||||
@@ -106,27 +105,4 @@ public class JobParametersPreparedStatementSetterTests extends AbstractTransacti
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void testMixedProperties(){
|
||||
|
||||
ExecutionContext executionContext = new ExecutionContext();
|
||||
executionContext.putLong("begin.id", 2);
|
||||
stepExecution.setExecutionContext(executionContext);
|
||||
pss.beforeStep(stepExecution);
|
||||
|
||||
List parameterNames = new ArrayList();
|
||||
parameterNames.add("begin.id");
|
||||
parameterNames.add("end.id");
|
||||
pss.setParameterKeys(parameterNames);
|
||||
|
||||
final List results = new ArrayList();
|
||||
jdbcTemplate.query("SELECT NAME from T_FOOS where ID > ? and ID < ?", pss, new RowCallbackHandler(){
|
||||
|
||||
public void processRow(ResultSet rs) throws SQLException {
|
||||
results.add(rs.getString(1));
|
||||
}});
|
||||
|
||||
assertEquals(1, results.size());
|
||||
assertEquals("bar3", results.get(0));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user