Search code examples
javajpadb2webspherejpa-2.0

JPA sequence skipping values


I am using Open JPA 2.0, WebSphere V8 and database is DB2 V10.

Created sequence using below syntax

CREATE SEQUENCE "MYSCHEMA"."SEQ_TABLEA" AS INTEGER START WITH 1 INCREMENT BY 1 MINVALUE 1000 MAXVALUE 2147483647 NO CYCLE CACHE 100 ORDER;

My Entity class definition uses sequence as below

@Entity
@Table(name="MYSCHEMA.SEQ_TABLEA")
public class MyEntity implements Serializable {

    @Id
    @SequenceGenerator(name="TABLEA_ID_GENERATOR", sequenceName="MYSCHEMA.SEQ_TABLEA")
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="TABLEA_GENERATOR")
    @Column(name="ID")
    private Integer myId;

    ..

After multiple inserts to TABLEA over 3 to 4 days, I find that sequence number has skipped many values. Inserts with sequence created are like 1,2,3,100,101,102,103,104,105,200,201,202,300,301,302,303,304,305,306,307,308,309,310,400,.. and so on

Any view on what is going wrong? I did not find any error while inserting, neither the DB was bounced during this period.


Solution

  • Try setting the allocation size on the @SequenceGenerator annotation. This specifies the amount to increment when allocating sequence numbers.

    @SequenceGenerator(name="TABLEA_ID_GENERATOR", 
      sequenceName="MYSCHEMA.SEQ_TABLEA", allocationSize=1)
    

    Java API

    Good Article on Sequences in JPA