H2数据库问题与列上的自动启动生成差距

发布于 2025-01-21 21:32:33 字数 2070 浏览 0 评论 0 原文

当我第一次启动H2数据库并进行一些插入时,自动化ID正确地为1、2、3等,但是当我停止Springboot应用程序并再次将其打开时,然后再次从上面的30个位置开始,基本上完全相同的问题,这篇文章经历了数据库,auto_increment字段由32?

我可以通过在H2控制台处执行“ shutdown ”命令来避免此问题,然后在关闭我的Springboot App之后,但是事实是我希望通过Java代码以更优雅的方式解决这一问题,以便您不必去H2Console并自己做到这一点,也不是很好。

例如,当您在文件中坚持使用H2数据库时,这才是一个问题,如果您在每个应用程序上创建一个新的启动时,则无关紧要。有人知道如何解决这一问题吗?

我已经在弹簧属性上尝试了一些配置而没有成功,我正在考虑的一种解决方案是制作 @predestroy 方法,该方法将在Spring关闭之前执行关闭语句(通过Eclipse IDE,途中) ,但是我不确定这是否会起作用,或者是否是正确的解决方案,我发布此信息是希望最近有人进入这个问题,并且知道该怎么办问题

我的应用程序属性:

spring.datasource.url=jdbc:h2:./src/main/resources/data;DB_CLOSE_ON_EXIT=FALSE;AUTO_RECONNECT=TRUE
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.h2.console.settings.trace=false
spring.h2.console.settings.web-allow-others=false

其他带有此问题的帖子:

使用Spring JPA和H2数据库,App重新启动后,ID并不是出现ID

更新

在使用该指南中的Actuator端点进行关闭时, https://www.baeldung.com/spring-boot-boot-boot-shutdown 该错误没有发生,我想这是因为它以“好方法”关闭了应用程序,但事实是,我也不认为解决方案是调用关闭应用程序的终点,必须有一个只使用Eclipse中的终止按钮,并且此错误不会发生=(

When I start the h2 database for the first time and make some inserts the autogenerated id goes correctly as 1, 2, 3 and so on, but when I stop the springboot application and turn it on again then it starts again from like 30 positions above, basically exact same problem this post experienced In H2 database, the auto_increment field is incremented by 32?

I can avoid this problem by executing the "shutdown" command at the h2 console and after that shutdown my springboot app, but the thing is that I would like this to be solved in a much more elegant way via java code so that you dont have to go to the h2console and do that by yourself, also in production it would not be good.

This is an issue only when you persist the h2 database for example in a file, if you are creating a new one on each application start then this should not matter. Does anyone knows how can this be resolved?

I have tried some configurations on the spring properties without success, one solution I was thinking about was to make a @PreDestroy method which would execute a shutdown statement before the spring shutdown (via eclipse ide by the way), but im not sure if that will work or if it is the correct solution, im posting this in hopes somebody came into this issue recently and knows what to do since the other posts similar dont provide a solution, just an explanation of what causes the problem

My application properties:

spring.datasource.url=jdbc:h2:./src/main/resources/data;DB_CLOSE_ON_EXIT=FALSE;AUTO_RECONNECT=TRUE
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.h2.console.settings.trace=false
spring.h2.console.settings.web-allow-others=false

other posts with this problem:

With Spring JPA and H2 database the ID isn't consequent after app restart

In H2 database, the auto_increment field is incremented by 32?

UPDATE:

When using the actuator endpoint for shutdown like on this guide https://www.baeldung.com/spring-boot-shutdown
The bug doesnt happen, and I guess it is because it is shutting down the application in a "good way", but the thing is that I also dont think the solution is to call an endpoint for shutting down the application, there must be a way to just use the terminate button in eclipse and that this bug doesnt happen =(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

§普罗旺斯的薰衣草 2025-01-28 21:32:33

您可以使用 no Cache 子句禁用身份列生成器的高速缓存:

CREATE TABLE TEST(
    ID BIGINT GENERATED BY DEFAULT AS IDENTITY(NO CACHE) PRIMARY KEY,
    V INTEGER
);

如果使用序列而不是身份列,则需要以相同的方式禁用其缓存:

CREATE SEQUENCE TEST_SEQUENCE NO CACHE;

但是,它会稍微增加磁盘i/o,但是。您还应该了解,差距仍然可能是可能的,如果应用程序可以插入行并在此之后回滚事务,则生成的值不会重新使用。

实际上,用嵌入式数据库中流产应用程序是一个非常糟糕的主意,因为它可能会导致数据丢失或数据库损坏。如果使用 db_close_on_exit = false ,则必须始终执行 shutdown 命令,然后终止应用程序或关闭所有连接(仅当 db_close_delay 未使用)。

如果您没有理由使用 db_close_on_exit = false (仅当应用程序寄存挂钩挂钩并在JVM关闭期间使用数据库时,才需要使用>),则最好删除此连接选项。

序列发生器的正确闭合数据库缓存不会影响生成的值。

注意
请记住,当使用 @generatedValue(stragity = generationType.Identity)您可以在H2Console上检查H2时,H2在首次启动时会生成随机序列名称,只需使用 alter alter sequence sequence paste_seq_name no cache no cache

示例:
ALTER SECUNESS_SECER_SECONE_SCESENE_63AF2D03_F992_40AD_88AA_6E0F10DA12CE无缓存

.net/qampy.png“ alt =”在此处输入图像说明”>

You can disable cache of identity column generator with NO CACHE clause:

CREATE TABLE TEST(
    ID BIGINT GENERATED BY DEFAULT AS IDENTITY(NO CACHE) PRIMARY KEY,
    V INTEGER
);

If you use sequence instead of identity column, you need to disable its cache in the same way:

CREATE SEQUENCE TEST_SEQUENCE NO CACHE;

It slightly increases disk I/O, however. You should also understand that gaps will be still possible, if application can insert a row and rollback a transaction after that, generated values aren't reused.

Actually it is a very bad idea to abort an application with embedded database in a hard way, because it may cause data loss or database corruption. If you use DB_CLOSE_ON_EXIT=FALSE, you must always execute the SHUTDOWN command before termination of your application or close all connections (this is enough only if DB_CLOSE_DELAY isn't used).

If you don't have a reason to use DB_CLOSE_ON_EXIT=FALSE (it is only needed when application registers a shutdown hook and works with database during shutdown of JVM), it will be better to remove this connection option.

With properly closed database caches of sequence generators don't affect generated values.

NOTE:
Remember that H2 generates random sequence name on first start up when using @GeneratedValue(strategy = GenerationType.IDENTITY) which you can check on the h2console, just use ALTER SEQUENCE paste_seq_name NO CACHE

example:
ALTER SEQUENCE SYSTEM_SEQUENCE_63AF2D03_F992_40AD_88AA_6E0F10DA12CE NO CACHE

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文