简体   繁体   中英

What does the jpa @SequenceGenerator.allocationSize mean?

I googled a lot and found many people said the allocationSize means ' After 'allocationSize' is reached, the next id will be retrieved from the database sequence again ' in https://www.logicbig.com/tutorials/java-ee-tutorial/jpa/seq-generator.html .

The the jpa document it means.

The amount to increment by when allocating sequence numbers from the sequence

It seems it is the same as the 'increment by' in sql.

Which one is right? Since I tested in h2/jpa, this allocationSize does not work, even it set it to 20, the next value of sequence does not increase by 20.

I also confused about the 'cache' in sql.

In summary, take below for example .

CREATE SEQUENCE ITEM_ID_SEQ START WITH 1 INCREMENT BY 100 cache 30;

What's the difference of the INCREMENT BY 100 , cache 30 , and the allocationSize of jpa .

thanks.

Here a short explanation of the configuration values:

allocationSize

To minimize round trips to the database server, IDs are allocated in groups. The number of IDs in each allocation is specified by the allocationSize attribute.

It is possible that some of the IDs in a given allocation will not be used. Therefore, this strategy does not guarantee there will be no gaps in sequence values.

The default is 50.

INCREMENT BY

Specify the interval between sequence numbers.

This integer value can be any positive or negative integer, but it cannot be 0.

If this value is negative, then the sequence descends. If the value is positive, then the sequence ascends. If you omit this clause, then the interval defaults to 1.

CACHE

Specify how many values of the sequence the database preallocates and keeps in memory for faster access.

If a system failure occurs, all cached sequence values that have not been used in committed DML statements are lost. The potential number of lost values is equal to the value of the CACHE parameter.

Conclusion

allocationSize and INCREMENT BY must have the same value. A higher number reduces the database round trips.

Cache is DB performance optimization and is not available for all database types.

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