Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring can't find batch initialization db script

I have deployed a spring batch through a war file in tomcat. I am running the batch using ContextListener at server start.

Batch launches fine but during database initialization db script is not running. The script is inside a jar file in WEB-INF/lib folder. Here is code part from config xml -

<jdbc:initialize-database data-source="dataSource">
 <jdbc:script location="jar:file:org/springframework/batch/core/schema-drop-mysql.sql" />
<jdbc:script location="org/springframework/batch/core/schema-mysql.sql" />
  </jdbc:initialize-database>

it gives me below exception -

java.io.FileNotFoundException: Could not open ServletContext resource [/org/springframework/batch/core/schema-drop-mysql.sql] at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:141) at org.springframework.core.io.support.EncodedResource.getReader(EncodedResource.java:132) at org.springframework.jdbc.datasource.init.ScriptUtils.readScript(ScriptUtils.java:278) at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:438) ... 32 more

like image 710
user3258218 Avatar asked Feb 07 '23 23:02

user3258218


2 Answers

Note that you are specifying the locations of the two scripts in two different ways: one beginning with jar:file:org/springframework/... and the other directly: org/springframework/...

Perhaps when you made the changes that others have suggested, you made them only to one of the locations?

But anyways, I was facing the same issue. Adding classpath: as the prefix fixed it for me. I'm using Java config to inject the script locations. Earlier it was (the "not working case"):

@Value("org/springframework/batch/core/schema-drop-postgresql.sql")
private Resource dropRepositoryTables;

@Value("org/springframework/batch/core/schema-postgresql.sql")
private Resource dataRepositorySchema;

But on changing to the following, it worked:

@Value("classpath:org/springframework/batch/core/schema-drop-postgresql.sql")
private Resource dropRepositoryTables;

@Value("classpath:org/springframework/batch/core/schema-postgresql.sql")
private Resource dataRepositorySchema;

The values were then used like this:

@Bean
public DataSourceInitializer dataSourceInitializer(DataSource dataSource) throws MalformedURLException {

    ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
    databasePopulator.addScript(dropRepositoryTables);
    databasePopulator.addScript(dataRepositorySchema);
    databasePopulator.setIgnoreFailedDrops(false);

    DataSourceInitializer initializer = new DataSourceInitializer();
    initializer.setDataSource(dataSource);
    initializer.setDatabasePopulator(databasePopulator);

    return initializer;
}
like image 135
The Student Soul Avatar answered Feb 24 '23 14:02

The Student Soul


I think this:

<jdbc:script location="jar:file:org/springframework/batch/core/schema-drop-mysql.sql" />

Should be this:

<jdbc:script location="classpath:/org/springframework/batch/core/schema-drop-mysql.sql" />
like image 42
Michael Minella Avatar answered Feb 24 '23 15:02

Michael Minella