当指定为1时,Derby Auto递增100 [英] Derby Auto Increment by 100 when specified as 1

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

问题描述

在用于创建derby数据库表的二手查询中包含主列自动增量。

  CREATE TABLE \table \ (\ n
+\id \INTEGER NOT NULL生成始终为身份(从1开始,增加1)NOT NULL,\ n
+\ path \VARCHAR(2000)DEFAULT NULL,\ n
+\已下载\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

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



解决方案

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



您有两种方法可以解决此问题:


  1. ; shutdown = true 添加到JDBC URL。这将在应用程序结束时关闭数据库。

  2. 设置 derby.language.sequence.preallocator 属性为 1 (默认值 100 )。这将确保永远不会缓存列值。

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

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?

解决方案

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.

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

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