Hello Guys
I have the follow code, trying to make a solution for this case
How To: Read Once and Write 3 times in parallel
First, the step is
The processor isCode:<batch:step id="clienteCuentaCabeceraStep" > <batch:tasklet transaction-manager="transactionManager"> <batch:chunk reader="clienteJdbcPagingItemReader" processor="clienteCuentaCabeceraListItemProcessor" writer="clienteCuentaCabeceraJdbcBatchWriter" commit-interval="50" /> </batch:tasklet> </batch:step>
Each List contains three itemsCode:public class ClienteCuentaCabeceraListItemProcessor implements ItemProcessor<Cliente,List<ClienteCuentaCabecera>>{ .... @Override public List<ClienteCuentaCabecera> process(Cliente cliente) throws Exception { List<ClienteCuentaCabecera> clientesCuentaCabecera = new ArrayList<ClienteCuentaCabecera>(); for(int i=0;i<3;i++){ .... } return clientesCuentaCabecera; } }
The writer is
Where ClienteCuentaCabeceraItemPreparedStatementSetter isCode:<bean id="clienteCuentaCabeceraJdbcBatchWriter" class="org.springframework.batch.item.database.JdbcBatchItemWriter"> <property name="dataSource" ref="dataSource"/> <property name="sql" value="INSERT INTO clientecuentacabecera(idClienteCuentaCabecera, fechaCreacionClienteCuentaCabecera, totalDisponibleClienteCuentaCabecera, estadoClienteCuentaCabecera, idCliente, idMoneda) VALUES(?,?,?,?,?,?)" /> <property name="itemPreparedStatementSetter" > <bean class="com.manuel.jordan.batch.writer.ClienteCuentaCabeceraItemPreparedStatementSetter"/> </property> </bean>
When I execute the code I getCode:public class ClienteCuentaCabeceraItemPreparedStatementSetter implements ItemPreparedStatementSetter<ClienteCuentaCabecera>{ @Override public void setValues(ClienteCuentaCabecera clienteCuentaCabecera, PreparedStatement ps) throws SQLException { ps.setString(1,clienteCuentaCabecera.getIdClienteCuentaCabecera() ); ... } }
Well even when the JdbcBatchItemWriter can write a List of items, the SQL statement arise the exception because it expect a list of Items, not a List of List of items.Code:2012-10-29 13:42:46,421 DEBUG [org.springframework.batch.core.repository.dao.JdbcStepExecutionDao] - <Truncating long message before update of StepExecution, original message is: java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.manuel.jordan.domain.ClienteCuentaCabecera at com.manuel.jordan.batch.writer.ClienteCuentaCabeceraItemPreparedStatementSetter.setValues(ClienteCuentaCabeceraItemPreparedStatementSetter.java:1)
Even when like playing I did the follow variation shown below
Always write the last item of each collection, the thirdCode:public class ClienteCuentaCabeceraListItemPreparedStatementSetter implements ItemPreparedStatementSetter<List<ClienteCuentaCabecera>>{ @Override public void setValues(List<ClienteCuentaCabecera> clientesCuentaCabecera, PreparedStatement ps) throws SQLException { for(ClienteCuentaCabecera clienteCuentaCabecera : clientesCuentaCabecera){ ps.setString(1,clienteCuentaCabecera.getIdClienteCuentaCabecera() ); ... } } }
Has sense because it just iterates, and the first and second are overrided by the third and this last value assignation is really the only what is write it in the DB.
How I could handle this?
I am thinking in use a ItemWriteAdapter to call a customized service where the repository work with the jdbcTemplate with the method batchUpdate method.
Exists other way to handle this situation with Spring Batch itself?
Thanks in advanced


Reply With Quote