4.7.10. Sequence Generation
This mechanism enables generating unique numerical sequences via a single API, independent of the DBMS type.
The main part of this mechanism is the UniqueNumbers
bean with the UniqueNumbersAPI
interface. The bean is available in the Middleware block. The interface methods are as follows:
-
getNextNumber()
– get the next value in a sequence. The mechanism enables simultaneous management of several sequences, identified by arbitrary strings. The name of the sequence from which you want to retrieve the value is passed in thedomain
parameter.Sequences do not require initialization. When
getNextNumber()
is called for the first time, the corresponding sequence will be created and a value of 1 will be returned. -
getCurrentNumber()
– obtain the current, i.e. the last generated value of the sequence. Thedomain
parameter sets the sequence name. -
setCurrentNumber()
– set the current value of the sequence. This value incremented by 1 will be returned by the next call togetNextNumber()
.
Below is an example of getting the next value in a sequence in a Middleware bean:
@Inject
private UniqueNumbersAPI uniqueNumbers;
private long getNextValue() {
return uniqueNumbers.getNextNumber("mySequence");
}
The getNextNumber()
method of the UniqueNumbersService
service is used to get sequence values in client blocks.
The app-core.cuba:type=UniqueNumbers
JMX bean with methods duplicating the methods of the UniqueNumbersAPI
is used for sequence management.
The sequence generation mechanism depends on the DBMS type. For HSQL, Microsoft SQL Server 2012+, PostgreSQL and Oracle each UniqueNumbersAPI
sequence corresponds to sec_un_{domain}
table in the database. For Microsoft SQL Server before 2012 each sequence corresponds to sec_un_{domain}
database table with an auto increment field. In this regard, the sequence parameters can also be managed directly in the database.