INSERT到BATCH_STEP_EXECUTION的SpringBatch错误

时间:2017-10-25 22:30:00

标签: spring spring-batch

我有一个简单的SpringBatch版本3.0.8作业,它读取远程数据库并将数据复制/写入本地数据库。

我收到此错误:

    Caused by: org.springframework.dao.DataIntegrityViolationException: PreparedStatementCallback; SQL [INSERT into BATCH_STEP_EXECUTION(STEP_EXECUTION_ID, VERSION, STEP_NAME, JOB_EXECUTION_ID, START_TIME, END_TIME, STATUS, COMMIT_COUNT, READ_COUNT, FILTER_COUNT, WRITE_COUNT, EXIT_CODE, EXIT_MESSAGE, READ_SKIP_COUNT, WRITE_SKIP_COUNT, PROCESS_SKIP_COUNT, ROLLBACK_COUNT, LAST_UPDATED) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)]; DB2 SQL Error: SQLCODE=-530, SQLSTATE=23503, SQLERRMC=DB2ADMIN.BATCH_STEP_EXECUTION.JOB_EXEC_STEP_FK, DRIVER=4.13.127; nested exception is com.ibm.db2.jcc.am.SqlIntegrityConstraintViolationException: DB2 SQL Error: SQLCODE=-530, SQLSTATE=23503, SQLERRMC=DB2ADMIN.BATCH_STEP_EXECUTION.JOB_EXEC_STEP_FK, DRIVER=4.13.127
    at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:243)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:649)
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:870)
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:931)
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:936)
    at org.springframework.batch.core.repository.dao.JdbcStepExecutionDao.saveStepExecution(JdbcStepExecutionDao.java:127)
    at org.springframework.batch.core.repository.support.SimpleJobRepository.add(SimpleJobRepository.java:170)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
    at com.sun.proxy.$Proxy2.add(Unknown Source)
    at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:144)
    at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:64)
    at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:67)
    at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:169)
    ... 7 more
Caused by: com.ibm.db2.jcc.am.SqlIntegrityConstraintViolationException: DB2 SQL Error: SQLCODE=-530, SQLSTATE=23503, SQLERRMC=DB2ADMIN.BATCH_STEP_EXECUTION.JOB_EXEC_STEP_FK, DRIVER=4.13.127
    at com.ibm.db2.jcc.am.id.a(id.java:673)
    at com.ibm.db2.jcc.am.id.a(id.java:60)
    at com.ibm.db2.jcc.am.id.a(id.java:127)
    at com.ibm.db2.jcc.am.no.b(no.java:2310)
    at com.ibm.db2.jcc.am.no.c(no.java:2293)
    at com.ibm.db2.jcc.t4.cb.l(cb.java:370)
    at com.ibm.db2.jcc.t4.cb.a(cb.java:62)
    at com.ibm.db2.jcc.t4.q.a(q.java:50)
    at com.ibm.db2.jcc.t4.tb.b(tb.java:220)
    at com.ibm.db2.jcc.am.oo.oc(oo.java:3428)
    at com.ibm.db2.jcc.am.oo.b(oo.java:4383)
    at com.ibm.db2.jcc.am.oo.gc(oo.java:784)
    at com.ibm.db2.jcc.am.oo.executeUpdate(oo.java:763)
    at org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:877)
    at org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:870)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:633)
    ... 29 more

出于某种原因,我发现我的数据库初始化脚本不止一次运行。我在日志跟踪中看到以下三次:一次在作业开始之前,两次在作业运行期间(中间步骤)。为什么呢?

[DEBUG] DataSourceUtils - Fetching JDBC Connection from DataSource
[DEBUG] DriverManagerDataSource - Creating new JDBC DriverManager Connection to [jdbc:db2://localhost:50000/bluecost]
[INFO] ScriptUtils - Executing SQL script from class path resource [org/springframework/batch/core/schema-drop-db2.sql]
[DEBUG] ScriptUtils - 0 returned as update count for SQL: DROP TABLE BATCH_STEP_EXECUTION_CONTEXT 
[DEBUG] ScriptUtils - 0 returned as update count for SQL: DROP TABLE BATCH_JOB_EXECUTION_CONTEXT 
[DEBUG] ScriptUtils - 0 returned as update count for SQL: DROP TABLE BATCH_STEP_EXECUTION 
[DEBUG] ScriptUtils - 0 returned as update count for SQL: DROP TABLE BATCH_JOB_EXECUTION_PARAMS 
[DEBUG] ScriptUtils - 0 returned as update count for SQL: DROP TABLE BATCH_JOB_EXECUTION 
[DEBUG] ScriptUtils - 0 returned as update count for SQL: DROP TABLE BATCH_JOB_INSTANCE 
[DEBUG] ScriptUtils - 0 returned as update count for SQL: DROP SEQUENCE BATCH_STEP_EXECUTION_SEQ 
[DEBUG] ScriptUtils - 0 returned as update count for SQL: DROP SEQUENCE BATCH_JOB_EXECUTION_SEQ 
[DEBUG] ScriptUtils - 0 returned as update count for SQL: DROP SEQUENCE BATCH_JOB_SEQ 
[INFO] ScriptUtils - Executed SQL script from class path resource [org/springframework/batch/core/schema-drop-db2.sql] in 213 ms.
[INFO] ScriptUtils - Executing SQL script from class path resource [org/springframework/batch/core/schema-db2.sql]
[DEBUG] ScriptUtils - 0 returned as update count for SQL: CREATE TABLE BATCH_JOB_INSTANCE ( JOB_INSTANCE_ID BIGINT NOT NULL PRIMARY KEY , VERSION BIGINT , JOB_NAME VARCHAR(100) NOT NULL, JOB_KEY VARCHAR(32) NOT NULL, constraint JOB_INST_UN unique (JOB_NAME, JOB_KEY) ) 
[DEBUG] ScriptUtils - 0 returned as update count for SQL: CREATE TABLE BATCH_JOB_EXECUTION ( JOB_EXECUTION_ID BIGINT NOT NULL PRIMARY KEY , VERSION BIGINT , JOB_INSTANCE_ID BIGINT NOT NULL, CREATE_TIME TIMESTAMP NOT NULL, START_TIME TIMESTAMP DEFAULT NULL , END_TIME TIMESTAMP DEFAULT NULL , STATUS VARCHAR(10) , EXIT_CODE VARCHAR(2500) , EXIT_MESSAGE VARCHAR(2500) , LAST_UPDATED TIMESTAMP, JOB_CONFIGURATION_LOCATION VARCHAR(2500) NULL, constraint JOB_INST_EXEC_FK foreign key (JOB_INSTANCE_ID) references BATCH_JOB_INSTANCE(JOB_INSTANCE_ID) ) 
[DEBUG] ScriptUtils - 0 returned as update count for SQL: CREATE TABLE BATCH_JOB_EXECUTION_PARAMS ( JOB_EXECUTION_ID BIGINT NOT NULL , TYPE_CD VARCHAR(6) NOT NULL , KEY_NAME VARCHAR(100) NOT NULL , STRING_VAL VARCHAR(250) , DATE_VAL TIMESTAMP DEFAULT NULL , LONG_VAL BIGINT , DOUBLE_VAL DOUBLE PRECISION , IDENTIFYING CHAR(1) NOT NULL , constraint JOB_EXEC_PARAMS_FK foreign key (JOB_EXECUTION_ID) references BATCH_JOB_EXECUTION(JOB_EXECUTION_ID) ) 
[DEBUG] ScriptUtils - 0 returned as update count for SQL: CREATE TABLE BATCH_STEP_EXECUTION ( STEP_EXECUTION_ID BIGINT NOT NULL PRIMARY KEY , VERSION BIGINT NOT NULL, STEP_NAME VARCHAR(100) NOT NULL, JOB_EXECUTION_ID BIGINT NOT NULL, START_TIME TIMESTAMP NOT NULL , END_TIME TIMESTAMP DEFAULT NULL , STATUS VARCHAR(10) , COMMIT_COUNT BIGINT , READ_COUNT BIGINT , FILTER_COUNT BIGINT , WRITE_COUNT BIGINT , READ_SKIP_COUNT BIGINT , WRITE_SKIP_COUNT BIGINT , PROCESS_SKIP_COUNT BIGINT , ROLLBACK_COUNT BIGINT , EXIT_CODE VARCHAR(2500) , EXIT_MESSAGE VARCHAR(2500) , LAST_UPDATED TIMESTAMP, constraint JOB_EXEC_STEP_FK foreign key (JOB_EXECUTION_ID) references BATCH_JOB_EXECUTION(JOB_EXECUTION_ID) ) 
[DEBUG] ScriptUtils - 0 returned as update count for SQL: CREATE TABLE BATCH_STEP_EXECUTION_CONTEXT ( STEP_EXECUTION_ID BIGINT NOT NULL PRIMARY KEY, SHORT_CONTEXT VARCHAR(2500) NOT NULL, SERIALIZED_CONTEXT CLOB , constraint STEP_EXEC_CTX_FK foreign key (STEP_EXECUTION_ID) references BATCH_STEP_EXECUTION(STEP_EXECUTION_ID) ) 
[DEBUG] ScriptUtils - 0 returned as update count for SQL: CREATE TABLE BATCH_JOB_EXECUTION_CONTEXT ( JOB_EXECUTION_ID BIGINT NOT NULL PRIMARY KEY, SHORT_CONTEXT VARCHAR(2500) NOT NULL, SERIALIZED_CONTEXT CLOB , constraint JOB_EXEC_CTX_FK foreign key (JOB_EXECUTION_ID) references BATCH_JOB_EXECUTION(JOB_EXECUTION_ID) ) 
[DEBUG] ScriptUtils - 0 returned as update count for SQL: CREATE SEQUENCE BATCH_STEP_EXECUTION_SEQ AS BIGINT MAXVALUE 9223372036854775807 NO CYCLE
[DEBUG] ScriptUtils - 0 returned as update count for SQL: CREATE SEQUENCE BATCH_JOB_EXECUTION_SEQ AS BIGINT MAXVALUE 9223372036854775807 NO CYCLE
[DEBUG] ScriptUtils - 0 returned as update count for SQL: CREATE SEQUENCE BATCH_JOB_SEQ AS BIGINT MAXVALUE 9223372036854775807 NO CYCLE
[INFO] ScriptUtils - Executed SQL script from class path resource [org/springframework/batch/core/schema-db2.sql] in 580 ms.

我的db-initialization定义如下:

<jdbc:initialize-database  data-source="dataSource" ignore-failures="DROPS">
    <jdbc:script location="org/springframework/batch/core/schema-drop-db2.sql" />
    <jdbc:script location="org/springframework/batch/core/schema-db2.sql" />
    <jdbc:script location="classpath:db/custom-db-assets-drop.sql" />
    <jdbc:script location="classpath:db/custom-db-assets.sql" />
</jdbc:initialize-database>

0 个答案:

没有答案