2016-04-28 91 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> 

我會刪除任何可跳過的異常,除非您有真正的業務來吞下這樣的錯誤。