user3258218
user3258218

Reputation: 81

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

Upvotes: 5

Views: 4285

Answers (4)

The Student Soul
The Student Soul

Reputation: 2492

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;
}

Upvotes: 7

Gautam Tadigoppula
Gautam Tadigoppula

Reputation: 1032

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.

Upvotes: 0

Mahendra
Mahendra

Reputation: 1426

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>

Upvotes: 0

Michael Minella
Michael Minella

Reputation: 21463

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

Upvotes: 2

Related Questions