Spring boot jpa identity, ids of new rows jumped forward by 10K

216 Views Asked by At

I'm using spring boot JPA 2.1.18.
All my model classes derives from this base class:

@Getter
@Setter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class) 
public abstract class DbEntity {

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   protected Long id;

   ....
}

Everything works fine, but I'm facing a strange behaviour with the ids used for the new rows in the db ( I'm using SqlServer):

enter image description here

As you casn see, suddenly the ids jumped forward by 10000 and I don't understand why (it's impossible that the java code reserves 10000 ids, because I don't have batch processes). Any suggestions?

1

There are 1 best solutions below

1
Otter On

This is because of an option in SQL Server called IDENTITY_CACHE, take a look at this page from Microsoft which explains a bit more

https://learn.microsoft.com/en-us/sql/t-sql/statements/alter-database-scoped-configuration-transact-sql?view=sql-server-ver15

To disable it, run the below under your database

USE DatabaseName
GO
ALTER DATABASE SCOPED CONFIGURATION SET IDENTITY_CACHE = OFF
GO

Note that this can have a performance penalty