指定为 1 时,德比自动递增 100 [英] Derby Auto Increment by 100 when specified as 1

查看:24
本文介绍了指定为 1 时,德比自动递增 100的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用于创建 derby 数据库表的查询中,由主列自动增量组成.

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

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

当我通过 spring jdbc 插入一行时,它会增加 100.查询是否有错误?

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

推荐答案

这是由于 自动递增列的值的预分配.Derby 是一个内存数据库,当数据库第一次加载到内存中时,它会缓存自动递增的值.然后,使用缓存生成自增列的未来值,而不是一次又一次地查询数据库.如果数据库没有正确关闭,缓存中未使用的值将永远丢失.

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. ;shutdown=true 添加到 JDBC URL.这将在应用程序结束时关闭数据库.
  2. 设置derby.language.sequence.preallocator 属性为 1(其默认值为 100).这将确保永远不会缓存列值.
  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.

请注意,大多数数据库对于序列的行为都相似.例如,H2 具有完全相同的行为,但使用 32 的缓存大小,而不是像 Derby 那样使用 100.

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.

这篇关于指定为 1 时,德比自动递增 100的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆