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
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;
}
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" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With