Results 1 to 3 of 3

Thread: pb JdbcBatchItemWriter with CompositeItemWriter

  1. #1
    Join Date
    Feb 2009
    Posts
    21

    Default pb JdbcBatchItemWriter with CompositeItemWriter

    Hi,

    I have problem with the transaction when i use two writers with CompositeItemWriter. I have a writer to write lines in files and a writer to insert data in database.

    The transaction of JdbcBatchItemWriter isn't managed by the transaction manager. Each call of write method commit the insert. I would like that the commit is called in same time that the commit managed by the transactionManager.

    My first writer, a FlatFileItemWriter work good. The lines is flush in my file only when the commit is called by the transaction manager.

    This is a big problem because when the first writer (the JdbcBatchItemWriter) succeed and the second writer (flatFileItemWriter) crash, the insert is already commit. It's not good.

    So, How precised to JdbcBatchItemWriter to commit in same time that the transactionManager ??

    I see in the JavaDoc of the JdbcBatchItemWriter class the line below but i don't find the solution :

    Code:
    It is expected that write(List) is called inside a transaction.
    Below, a party of my application context :

    Code:
    <!-- Liste des writers -->
    <beans:bean id="myItemWriter" class="org.springframework.batch.item.support.CompositeItemWriter">
    	<beans:property name="delegates">
    		<beans:list>
    			<beans:ref bean="sqlItemWriter" />
    			<beans:ref bean="fileItemWriter" />
    		</beans:list>
    	</beans:property>
    </beans:bean>
    
    <!-- Ecrit les items dans des fichiers -->
    <beans:bean id="fileItemWriter" scope="step" class="com.kevin.FileItemWriter">
    	<beans:property name="path" value="#{jobParameters[output.file.path]}" />
    	<beans:property name="util" ref="util" />
    </beans:bean>
    	
    <!-- Une insertion en base par item -->
    <beans:bean id="sqlItemWriter" class="org.springframework.batch.item.database.JdbcBatchItemWriter">
    	<beans:property name="dataSource" ref="p2dDataSource" />
    	<beans:property name="itemPreparedStatementSetter">
    		<beans:bean class="com.kevin.MyPreparedStatementSetter" />
    	</beans:property>
    	<beans:property name="sql" value="INSERT INTO P2CAPCACCL VALUES (?, ?)" />
    </beans:bean>

    Thanks for advance.

  2. #2
    Join Date
    Jun 2005
    Posts
    4,230

    Default

    So, How precised to JdbcBatchItemWriter to commit in same time that the transactionManager ??
    Make sure you use the same DataSource in your ItemWriter as the JobRepository.

  3. #3
    Join Date
    Feb 2009
    Posts
    21

    Default Pb of transaction in use JdbcBatchItemWriter in a CompositeWriter

    OK, i didn't know that the datasource use by JdbcBatchItemWriter have to be the same that the jobRepository to manage correctly the transaction.

    But, actually in my case, the dataSource are different. I try to do accept of use the same DataSource between the schema database Spring Batch and the schema database of my business. It's not sure.

    Otherwise, i think that i find a good solution. I create my owner ItemWriter who execute multiple sql request and i manage the transaction with the class TransactionSynchronizationManager like if it use in the FlatFileItemWriter.

    I bind an instance of Connection in the transaction and i use it to commit or rollback in the afterCompletion method. This method allow to commit or rollback the transaction in same time that the transactionManager.

    This solution seem good, no ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •