Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring批处理-连接到Postgres数据库时出现问题_Java_Postgresql_Spring Batch - Fatal编程技术网

Java Spring批处理-连接到Postgres数据库时出现问题

Java Spring批处理-连接到Postgres数据库时出现问题,java,postgresql,spring-batch,Java,Postgresql,Spring Batch,到目前为止,我一直在使用内存H2DB和SpringBatch。然而,现在我切换到连接外部postgres DB。这是我的连接对象(有些模糊): 当我启动我的应用程序时,我得到: 原因:org.springframework.jdbc.badsqlgramarException: PreparedStatementCallback;错误的SQL语法[选择作业\u实例\u ID, 来自批次作业实例的作业名称,其中作业名称=?和作业密钥=?]; 嵌套异常为org.postgresql.util.PSQ

到目前为止,我一直在使用内存H2DB和SpringBatch。然而,现在我切换到连接外部postgres DB。这是我的连接对象(有些模糊):

当我启动我的应用程序时,我得到:

原因:org.springframework.jdbc.badsqlgramarException: PreparedStatementCallback;错误的SQL语法[选择作业\u实例\u ID, 来自批次作业实例的作业名称,其中作业名称=?和作业密钥=?]; 嵌套异常为org.postgresql.util.PSQLException:错误:关系 “批处理作业实例”不存在

然后我了解到SpringBatch使用数据库保存元数据以实现其恢复/重试功能,对于嵌入式数据库,这些表是SpringBatch默认设置的。好的,这就解释了为什么我以前从未见过这个错误

但是,它说我可以设置此属性:

spring.batch.initialize-schema=never

所以我把它放在application.properties文件中。然而,我仍然得到了错误。如果有任何想法,我将不胜感激。

我自己能够解决这个问题。最终,我需要独立于实际目标关系数据库的Spring批处理存储库。所以我找到了这个参考:

我能够从该示例中获取DefaultBatchConfigurer类,并通过添加@Qualifier for embedded/local数据源对数据源进行了一个小的更改:

@Autowired(required = false)
public void setDataSource(@Qualifier("dataSource") DataSource dataSource) {
    this.dataSource = dataSource;
    this.transactionManager = new DataSourceTransactionManager(dataSource);
}
@Bean
public ItemReader<StuffDto> itemReader(@Qualifier("postgresDataSource")DataSource dataSource) {
return new JdbcCursorItemReaderBuilder<StuffDto>()
    .name("cursorItemReader")
    .dataSource(dataSource)
    .sql(GET_DATA)
    .rowMapper(new BeanPropertyRowMapper<>(StuffDto.class))
    .build();
}
然后,在我的Spring批处理读取器上(在我的另一个批处理配置类中),我对数据源做了一个小小的更改,添加了@Qualifier for postgres数据源:

@Autowired(required = false)
public void setDataSource(@Qualifier("dataSource") DataSource dataSource) {
    this.dataSource = dataSource;
    this.transactionManager = new DataSourceTransactionManager(dataSource);
}
@Bean
public ItemReader<StuffDto> itemReader(@Qualifier("postgresDataSource")DataSource dataSource) {
return new JdbcCursorItemReaderBuilder<StuffDto>()
    .name("cursorItemReader")
    .dataSource(dataSource)
    .sql(GET_DATA)
    .rowMapper(new BeanPropertyRowMapper<>(StuffDto.class))
    .build();
}

一旦我完成了以上所有操作,错误就消失了,一切正常。

我自己就能够解决这个问题。最终,我需要独立于实际目标关系数据库的Spring批处理存储库。所以我找到了这个参考:

我能够从该示例中获取DefaultBatchConfigurer类,并通过添加@Qualifier for embedded/local数据源对数据源进行了一个小的更改:

@Autowired(required = false)
public void setDataSource(@Qualifier("dataSource") DataSource dataSource) {
    this.dataSource = dataSource;
    this.transactionManager = new DataSourceTransactionManager(dataSource);
}
@Bean
public ItemReader<StuffDto> itemReader(@Qualifier("postgresDataSource")DataSource dataSource) {
return new JdbcCursorItemReaderBuilder<StuffDto>()
    .name("cursorItemReader")
    .dataSource(dataSource)
    .sql(GET_DATA)
    .rowMapper(new BeanPropertyRowMapper<>(StuffDto.class))
    .build();
}
然后,在我的Spring批处理读取器上(在我的另一个批处理配置类中),我对数据源做了一个小小的更改,添加了@Qualifier for postgres数据源:

@Autowired(required = false)
public void setDataSource(@Qualifier("dataSource") DataSource dataSource) {
    this.dataSource = dataSource;
    this.transactionManager = new DataSourceTransactionManager(dataSource);
}
@Bean
public ItemReader<StuffDto> itemReader(@Qualifier("postgresDataSource")DataSource dataSource) {
return new JdbcCursorItemReaderBuilder<StuffDto>()
    .name("cursorItemReader")
    .dataSource(dataSource)
    .sql(GET_DATA)
    .rowMapper(new BeanPropertyRowMapper<>(StuffDto.class))
    .build();
}

当我完成上述所有操作后,错误消失,一切正常。

这是否回答了您的问题@马哈茂德本哈辛,我也能做类似的事情。虽然你的链接基本上是相同的方法,但我最终用更完整的解决方案回答了这个问题,以防将来有人发现这些细节对他们自己的问题有帮助。这是否回答了你的问题@马哈茂德本哈辛,我也能做类似的事情。虽然你的链接基本上是相同的方法,但我最终用更完整的解决方案回答了这个问题,以防有人发现这些细节对他们将来的问题有帮助。