Here you are :
Something to say you : First i used the JPA dialect, then I remove it and implement my own writer (my writer do nothing!) In my processor I inject a Service (VenteService) that do some business logic and manage transaction
Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">
<import resource="classpath:/applicationContext-dao.xml" />
<!-- Instruct Spring to perform declarative transaction management automatically
on annotated classes. -->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="venteReader" class="org.springframework.batch.item.file.FlatFileItemReader">
<property name="resource" value="classpath:prixvente_000001.txt" />
<property name="lineMapper">
<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<property name="lineTokenizer">
<bean
class="org.springframework.batch.item.file.transform.PatternMatchingCompositeLineTokenizer">
<property name="tokenizers">
<map>
<entry key="1*" value-ref="line1Tokenizer" />
<entry key="2*" value-ref="line2Tokenizer" />
</map>
</property>
</bean>
</property>
<property name="fieldSetMapper">
<bean class="com.ubik.newapp.batch.VenteFieldSetMapper">
</bean>
</property>
</bean>
</property>
</bean>
<bean id="line1Tokenizer"
class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<property name="names"
value="ligneType,numeroCondition, debutValidite, finValidite, prixValeur, miseAJour, numeroArticle, originePV, topPrixDeVenteConseille, prixVenteMin, codePrixDeVente" />
<property name="delimiter">
<util:constant
static-field="org.springframework.batch.item.file.transform.DelimitedLineTokenizer.DELIMITER_TAB" />
</property>
</bean>
<bean id="line2Tokenizer"
class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<property name="names"
value="ligneType,numeroCondition,numeroEntiteMagasin,debutValiditeMag,finValiditeMag,miseAJour,extra1,extra2,extra3,extra4,extra5" />
<property name="delimiter">
<util:constant
static-field="org.springframework.batch.item.file.transform.DelimitedLineTokenizer.DELIMITER_TAB" />
</property>
</bean>
<!-- <bean id="venteWriter" class="org.springframework.batch.item.database.JpaItemWriter"> -->
<!-- <property name="entityManagerFactory" ref="entityManagerFactory" /> -->
<!-- </bean> -->
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://127.0.0.1:5433/ubik" />
<property name="username" value="postgres" />
<property name="password" value="ubikubik" />
</bean>
<!-- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> -->
<!-- <property name="dataSource" ref="dataSource"/> -->
<!-- </bean> -->
<bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
<!-- <property name="transactionManager" ref="transactionManager" /> -->
<!-- <property name="isolationLevelForCreate" value="PROPAGATION_REQUIRED" /> -->
</bean>
<!-- ========================= BUSINESS OBJECT DEFINITIONS ========================= -->
<!-- Activates various annotations to be detected in bean classes: Spring's
@Required and @Autowired, as well as JSR 250's @Resource. -->
<context:annotation-config />
<context:component-scan base-package="com.ubik.newapp"
scoped-proxy="targetClass" />
<job id="importVentes" xmlns="http://www.springframework.org/schema/batch" >
<step id="readWriteVente">
<tasklet >
<chunk reader="venteReader" processor="venteProcessor"
reader-transactional-queue="true" processor-transactional="true" writer="venteWriter" chunk-completion-policy="completionPolicy" >
</chunk>
</tasklet>
</step>
</job>
<bean id="completionPolicy" class="com.ubik.newapp.batch.CompletionPolicy" />
<bean id="venteProcessor" class="com.ubik.newapp.batch.VenteProcessor" />
<bean id="venteWriter" class="com.ubik.newapp.batch.VenteWriter" />
</beans>
VenteService :
Code:
package com.ubik.newapp.service.third.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import com.ubik.newapp.dao.third.VenteDao;
import com.ubik.newapp.dto.third.Vente;
import com.ubik.newapp.service.third.VenteService;
@Service
public class VenteServiceImpl implements VenteService {
@Autowired
private VenteDao venteDao;
@Autowired
private PlatformTransactionManager transactionManager;
private static final int LIGNE_TYPE_MAG = 1;
private boolean commit;
public void create(Vente p) {
//BLOC COMMIT WORKS WITH :
//ISOLATION_DEFAULT
//ISOLATION_READ_COMMITTED
//ISOLATION_READ_UNCOMMITTED
//ISOLATION_SERIALIZABLE
//PROPAGATION_MANDATORY
//PROPAGATION_REQUIRED
//PROPAGATION_SUPPORTS
//TIMEOUT_DEFAULT
TransactionStatus transaction = transactionManager.getTransaction(new DefaultTransactionDefinition(
TransactionDefinition.PROPAGATION_REQUIRED));
venteDao.save(p);
if(commit && p.getLigneType() == LIGNE_TYPE_MAG){
transaction.flush();
transactionManager.commit(transaction);
commit = false;
}
else if(p.getLigneType()== LIGNE_TYPE_MAG){
commit = true;
}
}
}