我正在尝试配置Spring Batch以使用PostGres数据库。我已在我的build.gradle.kts
文件中包含了以下依赖项:
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.postgresql:postgresql")
我的SpringBatch模块的application.yml
文件包含以下内容:
spring:
datasource:
url: jdbc:postgresql://postgres:5432/springbatchdb
username: postgres
password: root
driverClassName: org.postgresql.Driver
docker-compose.yml
postgres:
restart: always
image: postgres:12-alpine
container_name: postgres
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=root
- POSTGRES_DB=springbatchdb
ports:
- "5432:5432"
volumes:
- postgresql:/var/lib/postgresql
- postgresql_data:/var/lib/postgresql/data
然而,当我尝试添加数据文件时,我在我的SpringBatch Docker容器和PostGres容器的日志中看到以下错误:
Spring Batch:
<<< Exception in method: org.meanwhileinhell.spring.batch.server.SpringBatchController.handle Error Message: PreparedStatementCallback; bad SQL grammar [SELECT JOB_INSTANCE_ID, JOB_NAME from BATCH_JOB_INSTANCE where JOB_NAME = ? and JOB_KEY = ?]; nested exception is org.postgresql.util.PSQLException: ERROR: relation "batch_job_instance" does not exist
PostGres:
LOG: database system is ready to accept connections
2021-01-08 09:54:56.778 UTC [56] ERROR: relation "batch_job_instance" does not exist at character 39
2021-01-08 09:54:56.778 UTC [56] STATEMENT: SELECT JOB_INSTANCE_ID, JOB_NAME from BATCH_JOB_INSTANCE where JOB_NAME = $1 and JOB_KEY = $2
2021-01-08 09:55:27.033 UTC [56] ERROR: relation "batch_job_instance" does not exist at character 39
2021-01-08 09:55:27.033 UTC [56] STATEMENT: SELECT JOB_INSTANCE_ID, JOB_NAME from BATCH_JOB_INSTANCE where JOB_NAME = $1 and JOB_KEY = $2
我可以看到SB服务器正从我的元数据中成功获取POSTGRES。
JobRepositoryFactoryBean : No database type set, using meta data indicating: POSTGRES
我在启动服务器时,需要配置初始数据库,但是我错过了什么?
编辑:我已经尝试过显式添加 spring.datasource.initialize=true
,但没有任何变化。
spring.batch.jdbc.initialize-schema=always
。https://docs.spring.io/spring-boot/docs/2.7.x/reference/htmlsingle/#howto.data-initialization.batch - Sašo5