0

I have a working Spring Boot application which embeds a Spring Batch Job. The job is not run on a schedule, instead we kick it with an endpoint. It is working as it should. The basics of the batch are

  • Kick the endpoint to start the job
  • Reader reads from input file
  • Processor reads from oracle database using jpa repository and simple spring datasource config
  • Writer writes to output file

However there are new requirements: The schema of the repository database is from here on unknown on application startup. The tables are the same, it is just an unknown schema. This fact is out of our control and you might think it is stupid but there are reasons for it and this cant be changed. This means that with current functionality we need to reconfigure the datasource when we know the new schema name, and restart the application. This is a job that we will run for a number of times when migrating from one system to another, so it has a limited lifecycle and we just need a "quick fix" to be able to use it without rewriting the whole app. So what I would like to do is: Send the schema name as a query param to the application, put it in job parameters and then - get a new datasource when the processor reads from the repository. Would this be doable at all using Spring Batch? Any help appreciated!

  • Have you tried by passing as JOBParamater it will not work. Instead create a customDataSource based on dynamic schema and use public DataSource getDataSource(String user, String password) { DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create(); dataSourceBuilder.url(DBB_URL); dataSourceBuilder.username(user); dataSourceBuilder.password(password); return dataSourceBuilder.build(); } – Rakesh Feb 10 '21 at 02:43
  • Does this answer your question? [Change database schema during runtime based on logged in user](https://stackoverflow.com/questions/39357367/change-database-schema-during-runtime-based-on-logged-in-user) – Jens Schauder Feb 10 '21 at 07:24
  • Are you using this datasource for the job repository as well, or it is only used by the item processor? Please share your code to be able to help you in an efficient way. Which job repository implementation do you use? This is important to know for your use case. – Mahmoud Ben Hassine Feb 10 '21 at 08:50
  • Hi thanks for your thoughts and replies. I have solved my problem in an easy way. Instead of using an interface extending jpa CrudRepository using @Query("select from .tablename...") I implemented the Repository class using NamedParameterJdbcTemplate. The query used consumes the schema name sent in from endpoint via JobParameters. Maybe not the most effective way, but it does the trick wihout hazzle. – Niklas Skeppstedt Feb 11 '21 at 08:36

0 Answers0