5

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

1
  • missing code part - <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> Commented May 18, 2016 at 14:57

4 Answers 4

7

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;
}
Sign up to request clarification or add additional context in comments.

Comments

2

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" />

Comments

0

Try this lines. These are working fine in my case:

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

1 Comment

Not working for me. Have you deployed it as war on tomcat ? how are you running your batch ? through servlet or context listner ?
0

This worked for me

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

I am using maven as build tool and deploying on tomcat.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.