Derby Auto Increment by 100 when specified as 1

好久不见. 提交于 2019-11-26 18:32:48

问题


In used query to create derby database table consist of primary column auto increment.

CREATE TABLE \"table\" (\n"
            + " \"id\" INTEGER  NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1) NOT NULL,\n"
            + " \"path\" VARCHAR(2000) DEFAULT NULL,\n"
            + " \"downloaded\" BOOLEAN DEFAULT false NOT NULL,\n"
            + " \"retried_times\" SMALLINT DEFAULT 0 NOT NULL,\n"
            + " \"name\" VARCHAR(40),\n"
            + " \"downloaded_date\" TIMESTAMP DEFAULT NULL,\n"
            + " PRIMARY KEY (\"id\")\n"

When i insert a row through spring jdbc it increment by 100. Is there any error in query?


回答1:


This is due to pre-allocation of values for auto-increment columns. Derby being an in-memory database, caches auto-increment values when the database is first loaded into the memory. Then, future values of the auto-increment columns are generated using the cache instead of querying the database again and again. If the database is not shut down properly, unused values from the cache are lost forever.

You have two options to address this:

  1. Add ;shutdown=true to the JDBC URL. This will shut the database down when the application ends.
  2. Set the derby.language.sequence.preallocator property to 1 (its default value is 100). This will ensure that the column value is never cached.

Note that most databases behave similarly for sequences. For example, H2 has the exact same behaviour but uses a cache size of 32 instead of 100 like Derby does.



来源:https://stackoverflow.com/questions/31804210/derby-auto-increment-by-100-when-specified-as-1

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