springbootquartz仅在首次启动时初始化模式
这是我的配置:
@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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将需要一个迁移工具来处理数据库创建。
Spring Boot 提供了两个选项:Flyway 和 LiquidBase。
选择一个,创建迁移脚本,然后就可以开始运行了。
我个人喜欢 Flyway 方法。
您只需将
implementation 'org.flywaydb:flyway-core'
添加到您的 build.gradle 文件(或 Maven 替代方案)中。然后将其添加到您的
application.yml
然后在
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
Then create
db/migration
folder inresources
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
tovalidate
.