Sequence's current value advances to next 100 upon reconnection in derby

江枫思渺然 提交于 2019-11-29 15:35:09

According to https://db.apache.org/derby/docs/10.9/ref/rrefproperpreallocator.html, yes there is a sequence preallocator:)

In configuration just enter:

derby.language.sequence.preallocator=number

where number is a prealocated number of sequences OR

derby.language.sequence.preallocator=className

where className leads to a class implementing SequencePreallocator if you want some more advanced sequence alocation...

Hope it helps!

Jason McKindly

Being fully aware that this is an old post, I happened to stumble upon it and the accepted answer is a poor answer.

Yes, Derby uses a pre-allocation for it's range of generated indexes, however, simply lowering the pre-allocation size is NOT a good answer to this question.

The issue is actually occurring because OP is not ending his DB session properly (in other words, he is still connected to the DB upon application shutdown). Because of the unexpected shutdown, Derby will leak allocated values and cause the jump by the pre-allocated number.

From the documentation (https://db.apache.org/derby/docs/10.9/ref/rrefproperpreallocator.html)

If the database is shut down in an orderly fashion, Derby will not leak unused preallocated values. Instead, any unused values will be thrown away, and the sequence generator will continue where it left off once the database reboots. However, if the database exits unexpectedly, the sequence generator will skip the unused preallocated values when the database comes up again. This will leave a gap between the last NEXT VALUE FOR (issued before the database exited unexpectedly) and the next NEXT VALUE FOR (issued after the database reboots).

Using Spring in Code:

dbTemplate.execute("SHUTDOWN");

Using JDBC:

try {
    DriverManager.getConnection("jdbc:derby:;shutdown=true");
}
catch (SQLException se) {
    // SQL State XJO15 and SQLCode 50000 mean an OK shutdown.
    if (!(se.getErrorCode() == 50000) && (se.getSQLState().equals("XJ015")))
                System.err.println(se);
}

Properly shutting down your connection to the DB will NOT result in the leak of pre-allocated values and Derby will continue from the previously generated value.

So, if for any reason your application will shut down (e.g. normal shutdown, a fatal exception, etc.), you should always close your connection to the DB.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!