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

最后都变了- 提交于 2019-11-27 08:22:33

问题


I am having an issue with derby sequences with embedded database. When I first connect to the data base it gives me the correct sequence next value with the following statement.

VALUES (NEXT VALUE FOR :seqNm)

But when restart my desktop application, there by reconnecting to the database, the next value gives me the next hundredth value. It seems like derby uses some caching for preallocating the sequence numbers.

e.g. if my sequence starts with 100, First connection to the data base gives me sequences as 100, 101, 102 and so on.

Second connection to the data base gives me sequences as 200, 201, 202, 203 and so on.

Third connection to the data base gives me sequences as 300, 301, 302, 303 and so on.

Is there any workaround for this issue? I am using spring to connect to the database.

Thanks!!


回答1:


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!




回答2:


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.



来源:https://stackoverflow.com/questions/21227697/sequences-current-value-advances-to-next-100-upon-reconnection-in-derby

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