2016-04-28 47 views
1

我使用org.springframework.batch.item.database.JdbcBatchItemWriter将文件写入数据库并使用org.springframework.batch.item.file.mapping.PassThroughFieldSetMapper来映射列。数据没有被插入到数据库中,也没有在日志中发现任何错误。无法使用弹簧批量将数据加载到MySQL DB

<bean id="ItemWriter" class="org.springframework.batch.item.database.JdbcBatchItemWriter"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="sql"> 
     <value> 
      <![CDATA[ 
       insert into Student_Details(Name,Id,ClassId,Rank) values (:Name, :Id, :ClassId, :Rank) 
      ]]> 
     </value> 
    </property>  
    <property name="itemSqlParameterSourceProvider"> 
     <bean class="org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider" /> 
    </property> 

2016-04-28 05:45:59,904 INFO [com.sam.test.mine.scheduler.SchedulerService] [fileFirmsChannelPool-2] INFO - <Ok file received: Student_details_20160116.OK> 
Apr 28, 2016 5:45:59 AM org.springframework.batch.core.launch.support.SimpleJobLauncher run 
INFO: Job: [FlowJob: [name=StudentDetailsJob]] launched with the following parameters: [{groupId=0, size=0,filename=file:/app/data/Student_details_20160116.txt, filenames=file:/app/data/Student_details_20160116.txt, now=1461836759909,type=STUDENT_DET}] 
Apr 28, 2016 5:46:00 AM org.springframework.batch.core.job.SimpleStepHandler handleStep 
INFO: Executing step: [cleanStudentDetails] 
2016-04-28 05:46:00,362 INFO [com.sam.test.mine.batch.JdbcUpdateTasklet] [fileFirmsChannelPool-2] INFO - <Deleted table Student_Details successfully.> 
Apr 28, 2016 5:46:00 AM org.springframework.batch.core.job.SimpleStepHandler handleStep 
INFO: Executing step: [studentDetailsStep] 
Apr 28, 2016 5:46:00 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 
INFO: Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml] 
Apr 28, 2016 5:46:00 AM org.springframework.jdbc.support.SQLErrorCodesFactory <init> 
INFO: SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase, Hana] 
Apr 28, 2016 5:46:00 AM org.springframework.batch.core.job.SimpleStepHandler handleStep 
INFO: Executing step: [archiveStudentDetails] 
2016-04-28 05:46:00,894 INFO [com.sam.test.mine.batch.FileArchiverTasklet] [fileFirmsChannelPool-2] INFO - <Archiving ... > 
2016-04-28 05:46:00,902 INFO [com.sam.test.mine.batch.FileArchiverTasklet] [fileFirmsChannelPool-2] INFO - <success moving file to archive: /app/data/Student_details_20160116.txt to /app/archive/20160428/Student_details_20160116.txt.execution#33912> 
Apr 28, 2016 5:46:00 AM org.springframework.batch.core.launch.support.SimpleJobLauncher run 
INFO: Job: [FlowJob: [name=StudentDetailsJob]] completed with the following parameters: [{groupId=0, size=0, filename=file:/app/data/Student_details_20160116.txt, filenames=file:/app/data/Student_details_20160116.txt, now=1461836759909, type=STUDENT_DET}] and the following status: [COMPLETED] 
2016-04-28 05:46:00,975 INFO [com.sam.test.mine.scheduler.SchedulerService] [fileFirmsChannelPool-2] INFO - <finish deleting Ok file /app/data/Student_details_20160116.OK>> 
+0

我测试了你的配置(从你的XML样本),它实际上是在数据库中插入记录。你在用什么DMBS?你可以发布'dataSource' bean(没有用户/密码)吗?你的依赖关系中是否有正确的驱动程序? – Thrax

+0

它的我的SQL数据库,我能够使用“org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper”与同一个作家插入数据,所以希望不会有任何数据源或连接问题。 “com.microsoft.sqlserver.jdbc.SQLServerDriver”是驱动程序。 – Amuthan

+1

为了使它与MySQL DBMS一起工作,我使用了'com.mysql.jdbc.Driver',依赖关系是:' mysql mysql-connector-java'。 – Thrax

回答

0

我看到在BATCH_STEP_EXECUTION表,但WRITE_COUNT READ_COUNT值0和WRITE_SKIP_COUNT也一样读计数。

-Amuthan

当你READ_COUNT符合您WRITE_SKIP_COUNT,它意味着一个例外是在你JdbcBatchItemWriter已注册为skippable-exception-classes属性可跳过的异常被抛出。

<step id="step1"> 
    <tasklet> 
     <chunk reader="soemReader" writer="jdbcWriter" 
      commit-interval="10" skip-limit="10"> 
     <skippable-exception-classes> 
      <include class="com.package.YourException"/> 
     </skippable-exception-classes> 
     </chunk> 
    </tasklet> 
</step> 

我会删除任何可跳过的异常,除非您有真正的业务来吞下这样的错误。