0

I have a Spring batch application which uses some sql scripts to initialise the database.

When I extend the application by Spring Batch functionality I need to instantiate the JobRegistryBeanPostProcessor bean in order to use JobRegistry.

I use a similar method as in https://github.com/jbbarquero/spring-batch-sample/blob/master/src/main/java/com/malsolo/springframework/batch/sample/BatchConfiguration.java

However with such addition I have got a problem - sql scripts are not executed anymore.

In the log output I mentioned such new INFO messages with different bean names (XXX) :

] trationDelegate$BeanPostProcessorChecker : Bean XXX is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

Is there an approach to resolve the problem aforementioned ?

Eduard BABKIN
  • 71
  • 1
  • 5
  • Those info messages are just that, informational messages and aren't the issue. – M. Deinum Mar 09 '20 at 06:51
  • Yes, I agree. I just mentioned INFO messages for the complete picture, the actual problem is with sql scripts not running. – Eduard BABKIN Mar 09 '20 at 07:26
  • What are you using to manage your database? – M. Deinum Mar 09 '20 at 07:27
  • I use ordinary entityManagerFactory, JPA with Hibernate and H2-backed datasource. – Eduard BABKIN Mar 09 '20 at 07:37
  • That isn't the answer to my question. What is managing your schema? How do you create the db schema in H2 (hibernate?, FlyWay, ....?). Also are those added by youy or are you using Spring Boots auto-configuration. – M. Deinum Mar 09 '20 at 07:37
  • The DB schema is created by Hibernate. And the schema itself is created properly - I can see empty tables in H2-console. That is my own custom sql script which is not running, so it does not populate certain tables with data. – Eduard BABKIN Mar 09 '20 at 07:49
  • If hibernate is creating the schema it will drop everything and then re-create. So if you first create a schema, that will be destroyed. – M. Deinum Mar 09 '20 at 07:52
  • I am using spring.jpa.hibernate.ddl-auto = update and spring.datasource.continue-on-error=true config policies, so if there is no the JobRegistryBeanPostProcessor bean everything works as expected (tables are created once and custom sql statements populate them). – Eduard BABKIN Mar 09 '20 at 08:32
  • The `spring.jpa.hibernate.ddl-auto` and `spring.datasource.continue-on-error` aren't related. Also hibernate wo't create the spring batch tables (ubnless you created entities for them). – M. Deinum Mar 09 '20 at 08:39
  • Anyway, the tables themselves are created properly, only a custom sql script is not running. – Eduard BABKIN Mar 09 '20 at 08:40
  • And that is the question you haven't answered HOW are those custom scripts executed. – M. Deinum Mar 09 '20 at 08:41
  • The database initialisation is done according to https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-database-initialization (Ch.10.2, Ch.10.3). There is a file data.sql located in src/main/resources/ folder of the project. Without the bean statements of that file are executed properly. – Eduard BABKIN Mar 09 '20 at 14:38

1 Answers1

0

There is a following solution to the problem:

instead of instantiating the JobRegistryBeanPostProcessor bean, directly register the job in the jobRegistry as proposed in https://stackoverflow.com/a/53381283/6931863

Such solution enables initialisation of Database from data.sql as well as stopping jobs using jobOperator.stop.

Eduard BABKIN
  • 71
  • 1
  • 5