我正在使用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)
我正在用这段代码解雇这份工作:
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 :(得分:1)
我删除了我的数据库并再次创建它,现在它可以工作:)
是的,就是这么简单
如何创建数据库:http://www.javabeat.net/articles/272-the-spring-batch-infrastructure-1.html