Spring boot JPA (Hibernate) with DB sequence generator and batch insert/update

771 Views Asked by At

While using spring boot JPA (Hibernate) with @GeneratedValue(strategy = GenerationType.SEQUENCE) and batch inserts/updates enabled, I am observing sequence numbers coming in a random way.

For eg: Say in first run, an entity is created with id column as 100, in the next run it is going to a lower number say 92 and then randomly to a higher value like 110.

Tech stack

Spring boot 2.7.12
Oracle 19c

Entity - ID column definition

@Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "XXX_SEQ")
    @SequenceGenerator(name = "XXX_SEQ", sequenceName = "SQ_XXX", allocationSize = 1)
    @Column(name = "ID")
    private Long id;

Batch insert/update configuration

spring:
  jpa:
    properties:
      hibernate:
        jdbc:
          time_zone: UTC
          batch_size: 5
        order_updates: true
        batch_versioned_data: true
        order_inserts: true

Few observations -

  1. In lower DB environment, the sequence "increment by" is 1. To support this, I had set @SequenceGenerator (allocationSize) to 1. But in higher environment, the "increment by" seems to be 50. Could this randomness in ID generation be because of this mismatch (application allocationSize=1 but DB increment by is 50)?

  2. Or is this because of the batch inserts/updates?

I saw few similar issue posts citing use of a property named "new_generator_mappings" but am not quite following whether I should continue with the default setting of TRUE or change it to FALSE to support my use case?

0

There are 0 best solutions below