Mea*_*ell 1 postgresql spring-batch spring-boot
我正在尝试配置 Spring Batch 以使用 PostGres DB。我在我的build.gradle.kts文件中包含了以下依赖项:
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.postgresql:postgresql")
Run Code Online (Sandbox Code Playgroud)
我application.yml的 SpringBatch 模块包含以下内容:
spring:
datasource:
url: jdbc:postgresql://postgres:5432/springbatchdb
username: postgres
password: root
driverClassName: org.postgresql.Driver
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试添加数据文件时,我在 SpringBatch Docker 容器和 PostGres 容器的日志中看到以下错误:
春季批次:
<<< 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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
我可以看到 SB 服务器正在从我的元数据中获取 POSTGRES 确定。
JobRepositoryFactoryBean : No database type set, using meta data indicating: POSTGRES
Run Code Online (Sandbox Code Playgroud)
在服务器启动期间我缺少什么来配置初始数据库?
编辑:我试过spring.datasource.initialize=true明确添加,但没有改变。
Zah*_*han 18
我花了很多时间来解决使用最新的 Spring Boot Starter 3.0 和 Spring Batch 5.0 时出现的ERROR: relation " X " does not recognize等问题。
spring.batch.jdbc.initialize-schema=always
Run Code Online (Sandbox Code Playgroud)
但是,它没有为我创建必要的表。尽管根据文档,它应该已经创建了表。
经过大量研究,我发现,在最新的 Spring Batch 5.0 中,有很多改进。当迁移到新的 Spring 5 时,我做了很多错误的事情。
@EnableBatchProcessing从您的配置中删除。因为在最新的Spring Batch 5中您不再需要它了。
例子:
@Configuration
@AllArgsConstructor
@EnableBatchProcessing //please remove it.
public class SpringBatchConfiguration {}
Run Code Online (Sandbox Code Playgroud)
将其更改为:
@Configuration
@AllArgsConstructor
public class SpringBatchConfiguration {}
Run Code Online (Sandbox Code Playgroud)
PlatformTransaction Manager:我做错的第二件事是使用了不正确的事务管理器,如果您使用 JPA 来持久化实体,则需要相应的事务管理器。
我使用的ResourcelessTransactionManager()方法在我的情况下是错误的,并且在跑步时造成了很多头痛。
对于 JPA,您需要一个JpaTransactionManager()
就像是:
@Bean
public PlatformTransactionManager transactionManager() {
return new JpaTransactionManager();
}
Run Code Online (Sandbox Code Playgroud)
错误之后我学到的第三件事;我们不需要创建一个 bean ,Datasource除非我们正在做一些复杂的事情,比如有 2Datasource个用于编写 Spring Batch 关联表,另一个用于保存我们的业务数据。
无论我们需要在哪里使用,JobRepository只需注入它即可。就像是:
@Bean
@Autowired
Job job(JobRepository jobRepository) {
JobBuilder jobBuilderFactory = new JobBuilder("somename", jobRepository );
return jobBuilderFactory.flow(step1(jobRepository)).end()
.build();
}
Run Code Online (Sandbox Code Playgroud)
有关迁移的更多详细信息:Spring Migration 3 Guide。
请检查以下添加在 application.yml 中
spring.batch.initialize-schema: always
Run Code Online (Sandbox Code Playgroud)
请检查以下依赖项是否已添加
<artifactId>spring-boot-starter-batch</artifactId>
Run Code Online (Sandbox Code Playgroud)
小智 5
yaml 文件是
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=1234
spring.datasource.driver-class-name=org.postgresql.Driver
spring.batch.jdbc.initialize-schema=always
Run Code Online (Sandbox Code Playgroud)
梯度依赖
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-batch'
implementation 'org.projectlombok:lombok-maven-plugin:1.18.6.0'
implementation group: 'org.postgresql', name: 'postgresql', version: '42.3.1'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.batch:spring-batch-test'
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2218 次 |
| 最近记录: |