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

前端 未结 2 1499
清歌不尽
清歌不尽 2020-12-12 01: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 statem

相关标签:
2条回答
  • 2020-12-12 02:08

    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!

    0 讨论(0)
  • 2020-12-12 02:09

    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.

    0 讨论(0)
提交回复
热议问题