2012-03-13 67 views
1

我正在使用spring批處理從文件讀取數據並將數據寫入數據庫。 它工作正常,但我想將作業的信息(參數,狀態...)保存在「dataSourceBatch」數據庫中。如何在數據庫中保存工作信息(參數,狀態......)?

我的批處理文件:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:batch="http://www.springframework.org/schema/batch" 
xmlns:task="http://www.springframework.org/schema/task" 
xmlns:p="http://www.springframework.org/schema/p" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 

xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/batch 
    http://www.springframework.org/schema/batch/spring-batch-2.0.xsd 
    http://www.springframework.org/schema/task 
    http://www.springframework.org/schema/task/spring-task-3.0.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
    http://www.springframework.org/schema/batch 
    http://www.springframework.org/schema/batch/spring-batch-2.0.xsd 
    "> 

<bean id="dataSourceBatch" 
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
    <property name="url" 
     value="jdbc:mysql://localhost:3306/batchdb?characterEncoding=UTF-8" /> 
    <property name="username" value="user" /> 
    <property name="password" value="pass" /> 
</bean> 


<bean id="myWriter" class="org.jobs.MyWriter" /> 
<bean id="myReader" class="org.jobs.myReader" /> 

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" 
    p:ignoreUnresolvablePlaceholders="true" /> 

<bean 
    id="transactionManager" 
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager" 
    p:dataSource-ref="dataSourceBatch" /> 
<!-- 
<bean id="transactionManager" 
    class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/> 
--> 

<bean id="jobLauncher" 
    class="org.springframework.batch.core.launch.support.SimpleJobLauncher" 
    p:jobRepository-ref="jobRepository" /> 

<bean 
    class="org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor" 
    p:jobRegistry-ref="jobRegistry" /> 

<!-- 
<batch:job-repository id="jobRepository" data-source="dataSourceBatch" 
    transaction-manager="transactionManager" isolation-level-for-create="SERIALIZABLE" 
    table-prefix="BATCH_" /> 
--> 
<bean 
    id="jobRepository" 
    class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean" 
    p:transactionManager-ref="transactionManager" /> 

<bean 
    id="jobRegistry" 
    class="org.springframework.batch.core.configuration.support.MapJobRegistry" /> 



<bean 
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" 
    p:ignoreUnresolvablePlaceholders="true" /> 


<batch:job id="insertToDBJob" job-repository="jobRepository"> 
    <batch:step id="insertToDBStep"> 
     <batch:tasklet 
     transaction-manager="transactionManager"> 
      <batch:chunk 
      reader="myReader" 
      writer="myWriter" 
      commit-interval="2" /> 
     </batch:tasklet> 
     <batch:end on="COMPLETED" /> 
    </batch:step> 
</batch:job> 

在我的搜索,我發現:

<batch:job-repository id="jobRepository" data-source="dataSourceBatch" 
    transaction-manager="transactionManager" isolation-level-for-create="SERIALIZABLE" 
    table-prefix="BATCH_" /> 

但是,通過使用這個 「jobRepository」 我得到這個異常:

org.springframework.dao.DuplicateKeyException: PreparedStatementCallback; SQL [INSERT into BATCH_JOB_INSTANCE(JOB_INSTANCE_ID, JOB_NAME, JOB_KEY, VERSION) values (?, ?, ?, ?)]; Duplicate entry '0' for key 'PRIMARY'; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '0' for key 'PRIMARY' 
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:241) 
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72) 
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:602) 
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:811) 
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:867) 
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:871) 
at org.springframework.batch.core.repository.dao.JdbcJobInstanceDao.createJobInstance(JdbcJobInstanceDao.java:117) 
at org.springframework.batch.core.repository.support.SimpleJobRepository.createJobExecution(SimpleJobRepository.java:127) 

我正在疏導喬b相驗證碼:

ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("batch.xml"); 
classPathXmlApplicationContext.start(); 
// launch the job 
JobLauncher jobLauncher = (JobLauncher)classPathXmlApplicationContext.getBean("jobLauncher"); 
Job job = (Job)classPathXmlApplicationContext.getBean(jobName); 
JobParametersBuilder jobParametersBuilder = new JobParametersBuilder().addDate("date", new Date()); 
id++; 
jobParametersBuilder.addLong("id", id); 
jobLauncher.run(job, jobParametersBuilder.toJobParameters()); 

感謝提前:)

+0

你的id的範圍是什麼?它看起來像是多次0,並且您正在獲取一個dupe鍵,假設JOB_INSTANCE_ID是主鍵。你可以使用標識列嗎? – 2012-03-13 14:50:26

+0

tnx for try,但我得到另一個異常 – 2012-03-15 08:34:08

+0

我試圖改變「batch_job_instance」表中的「JOB_INSTANCE_ID」列到標識列,但現在我得到這個異常:org.springframework.dao.DataIntegrityViolationException:PreparedStatementCallback; SQL [插入BATCH_JOB_PARAMS(JOB_INSTANCE_ID,KEY_NAME,TYPE_CD,STRING_VAL,DATE_VAL,LONG_VAL,DOUBLE_VAL)值(?,?,?,?,?,?,?)];無法添加或更新子行:外鍵約束失敗('batchdb'.'batch_job_params',CONSTRAINT'JOB_INSTANCE_PARAMS_FK' FOREIGN KEY('JOB_INSTANCE_ID')REFERENCES'batch_job_instance'('JOB_INSTANCE_ID'));嵌套異常是co – 2012-03-15 08:32:55

回答

相關問題