springbootquartz仅在首次启动时初始化模式

发布于 2025-01-13 02:48:19 字数 1827 浏览 0 评论 0原文

这是我的配置:

@Bean
    @QuartzDataSource
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource quartzDataSource() {
        return DataSourceBuilder.create().build();
    }

这是我的 app.yml:

datasource:
    url: my-url
    jdbcUrl: ${spring.datasource.url}
    username: 'root'
    password: 'root'
...
quartz:
    job-store-type: jdbc
    jdbc:
      initialize-schema: always
    wait-for-jobs-to-complete-on-shutdown: true
    properties:
      org:
        quartz:
          dataSource:
            quartz-data-source:
              provider: hikaricp
              driver: com.mysql.cj.jdbc.Driver
              URL: ${spring.datasource.url}
              user: ${spring.datasource.username}
              password: ${spring.datasource.password}
              maximumPoolSize: 5
              connectionTestQuery: SELECT 1
              validationTimeout: 5000
              idleTimeout: 1
          scheduler:
            instanceId: AUTO
            instanceName: my-project-scheduler
          jobStore:
            class: org.quartz.impl.jdbcjobstore.JobStoreTX
            driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate
            useProperties: false
            misfireThreshold: 60000
            clusterCheckinInterval: 30000
            isClustered: true
            dataSource: quartz-data-source
          threadPool:
            class: org.quartz.simpl.SimpleThreadPool
            threadCount: 1
            threadPriority: 5
            threadsInheritContextClassLoaderOfInitializingThread: true

我的问题:

如果我设置 initialize-schema:always 那么 qrtz 表会在每次应用程序启动时创建。

另一方面,如果我设置 initialize-schema: never 那么我会在第一次启动时收到错误,提示 qrt 表丢失。

有没有办法将其配置为仅在 qrtz 表不存在时才初始化它们?

This is my config:

@Bean
    @QuartzDataSource
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource quartzDataSource() {
        return DataSourceBuilder.create().build();
    }

and this is my app.yml:

datasource:
    url: my-url
    jdbcUrl: ${spring.datasource.url}
    username: 'root'
    password: 'root'
...
quartz:
    job-store-type: jdbc
    jdbc:
      initialize-schema: always
    wait-for-jobs-to-complete-on-shutdown: true
    properties:
      org:
        quartz:
          dataSource:
            quartz-data-source:
              provider: hikaricp
              driver: com.mysql.cj.jdbc.Driver
              URL: ${spring.datasource.url}
              user: ${spring.datasource.username}
              password: ${spring.datasource.password}
              maximumPoolSize: 5
              connectionTestQuery: SELECT 1
              validationTimeout: 5000
              idleTimeout: 1
          scheduler:
            instanceId: AUTO
            instanceName: my-project-scheduler
          jobStore:
            class: org.quartz.impl.jdbcjobstore.JobStoreTX
            driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate
            useProperties: false
            misfireThreshold: 60000
            clusterCheckinInterval: 30000
            isClustered: true
            dataSource: quartz-data-source
          threadPool:
            class: org.quartz.simpl.SimpleThreadPool
            threadCount: 1
            threadPriority: 5
            threadsInheritContextClassLoaderOfInitializingThread: true

My question:

If I set initialize-schema: always then the qrtz tables are created on each application startup.

On the other side, if I set initialize-schema: never then I get an error on the first startup that the qrt tables are missing.

Is there a way to configure it to initialize the qrtz tables only if they do not exist?

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

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

发布评论

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

评论(1

涫野音 2025-01-20 02:48:19

您将需要一个迁移工具来处理数据库创建。

Spring Boot 提供了两个选项:Flyway 和 LiquidBase。

选择一个,创建迁移脚本,然后就可以开始运行了。

我个人喜欢 Flyway 方法。

您只需将 implementation 'org.flywaydb:flyway-core' 添加到您的 build.gradle 文件(或 Maven 替代方案)中。

然后将其添加到您的 application.yml

spring:
  flyway:
    enabled: true
    baseline-on-migrate: true

然后在 resources 文件夹中创建 db/migration 文件夹并放入您的迁移脚本,例如。 V1_0_0__db_init.sql(flyway 有自己的命名约定)。

要获取创建 SQL 脚本,我建议您从正在运行的数据库中导出它们。

另外,不要忘记将 spring.jpa.hibernate.ddl-auto 更改为 validate 。

You are gonna need a migration tool to handle the database creation.

Spring Boot provides two options: Flyway and LiquidBase.

Choose one, create migration scripts and you are up and running.

I personally like the Flyway approach.

You just add implementation 'org.flywaydb:flyway-core' to your build.gradle file (or the maven alternative).

Then add this to your application.yml

spring:
  flyway:
    enabled: true
    baseline-on-migrate: true

Then create db/migration folder in resources folder and put in your migration scripts eg. V1_0_0__db_init.sql (flyway has its own naming convention).

To get the create SQL scripts I recommend that you export them from running database.

Also do not forget to change the spring.jpa.hibernate.ddl-auto to validate.

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