简体   繁体   中英

Spring Batch: Could not increment identity; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'BATCH_JOB_SEQ'?

We are in the process of moving to Azure SQL Server from Oracle DB for our Spring Batch application.

I am getting the following error while trying to execute the job post migration to SQL Server

Could not increment identity; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'AppName.BATCH_JOB_SEQ'.

I can that SQL Server has the required sequence

在此处输入图像描述

Below the job repository configuration

<batch:job-repository id="jobRepository"
 isolation-level-for-create="READ_COMMITTED"
 table-prefix="MyApp.BATCH_"/>

Below are tables and sequences are available in Oracle

在此处输入图像描述 在此处输入图像描述

Below are tables and sequences are available in Azure SQL

在此处输入图像描述 在此处输入图像描述

I am using the following version在此处输入图像描述

should I upgrade to

在此处输入图像描述

or should I recreate the tables as per https://github.com/spring-projects/spring-batch/blob/main/spring-batch-core/src/main/resources/org/springframework/batch/core/schema-sqlserver.sql

Probably the error is related to the migration from Oracle to Azure SQL Server.

As you can see in the source code of the library under the hood Spring Batch uses different strategies when generating the ids for jobs, job executions, and step executions .

In the Oracle case, they use sequences ; with SQL Server, they implemented id generation using tables with an identity column .

The migration process also replicated the different Oracle sequences required by Spring Batch and very likely it is causing the issue when the aforementioned SQL Server id generation strategy tries obtaining the next value.

Please, drop the migrated sequences and create the three tables required for SQL Server with the appropriate values:

CREATE TABLE BATCH_STEP_EXECUTION_SEQ (
  ID BIGINT IDENTITY(<last analogous Oracle sequence value>, 1)
);

CREATE TABLE BATCH_JOB_EXECUTION_SEQ (
  ID BIGINT IDENTITY(<last analogous Oracle sequence value>, 1)
);

CREATE TABLE BATCH_JOB_SEQ (
  ID BIGINT IDENTITY(<last analogous Oracle sequence value>, 1)
);

There is more than one post/question in Stack Overflow related to this issue. I provided a cleaner explanation and fix for this issue in here .

Keeping tables as sequences in spring-batch leads to deadlock issues reported by many users already, so better replace them with real sequences if you are working with SQL Server >= 2012

Happy coding!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM