I use MsSQL and Spring Batch. I need to interact with two schemas in the same database. However, the driver for some reason ignores the installation of the scheme and uses one of the standard schemes. How can I fix this?
@Bean
public DataSource targetDataSource() {
SimpleDriverDataSource dataSource = getSimpleDriverDataSource();
dataSource.setSchema("target");
return dataSource;
}
@Bean
public DataSource sourceDataSource() {
SimpleDriverDataSource dataSource = getSimpleDriverDataSource();
dataSource.setSchema("source");
return dataSource;
}
private SimpleDriverDataSource getSimpleDriverDataSource() {
SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
dataSource.setDriverClass(com.microsoft.sqlserver.jdbc.SQLServerDriver.class);
dataSource.setUsername("sa");
dataSource.setUrl("jdbc:sqlserver://localhost:1433;database=myDB");
dataSource.setPassword("myPassword");
return dataSource;
}
Code for writhing:
@Bean
public JdbcBatchItemWriter<Person> writer() {
return new JdbcBatchItemWriterBuilder<Person>()
.itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>())
.sql("INSERT INTO target.people (first_name, last_name) VALUES (:firstName, :lastName)") //TODO убрать схему
.dataSource(targetDataSource())
.build();
}
When remove target.
part, there will be a PreparedStatement Callback exception; bad SQL grammar [INSERT INTO people (first_name, last_name) VALUES (?, ?)]; nested exception is java.sql.BatchUpdateException: Invalid object name 'people'.
The same thing happens with reading from the schema. As soon as I remove the schema name from the request, I get an exception