0

I am trying to write my first spring batch example with spring boot . I want to create database in mysql as soon as application starts up.

I have following dependencies in my pom

        <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>io.spring</groupId>
    <artifactId>hello-world</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>helloWorld</name>
    <description>Hello World for Spring Batch</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-batch</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <addResources>true</addResources>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

and application.properties has following configuration

spring.datasource.url=jdbc:mysql://localhost:3306/batchjob?allowPublicKeyRetrieval=true&useSSL=false
spring.datasource.username=root
spring.datasource.password= abcd1234
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.schema=schema-mysql.sql

spring.batch.initialize-schema=always

But when I'm starting application, it is not able to recognize Mysql and in the logs it is printing following line

2019-03-10 14:32:01.381  INFO 48560 --- [           main] o.s.b.c.r.s.JobRepositoryFactoryBean     : No database type set, using meta data indicating: HSQL

What am I missing which will enable auto schema creation in mySql ?

BobCoder
  • 743
  • 2
  • 10
  • 27
  • Hi!, Did you try putting `spring.jpa.hibernate.ddl-auto = create`? – Marco Mar 10 '19 at 14:52
  • @MarcoPens Do I need to add any other dependency as well ? Tried to put spring.jpa.hibernate.ddl-auto = create. Same result. – BobCoder Mar 10 '19 at 14:57
  • Looks like you don't have the schema created... And for spring you have to tell it the at it needs to create the schema, you need to have hibernate in your dependencies, you need this `spring-boot-starter-jpa`, keep in mind that you always have to create the database – Marco Mar 10 '19 at 15:01
  • @MarcoPens I have added dependency for jpa as well . But even after that my application is not able to recognize mysql database. – BobCoder Mar 10 '19 at 15:14
  • Sorry... But the problem is other.. I was confused this can be help better https://www.google.com/url?sa=t&source=web&rct=j&url=https://stackoverflow.com/questions/49550523/java-spring-batch-using-embedded-database-for-metadata-and-a-second-database-for&ved=2ahUKEwjl-M-j7_fgAhWZErkGHTcXCXQQjjgwAHoECAcQAQ&usg=AOvVaw2gK1wakyPanKJns1DCgM66 – Marco Mar 10 '19 at 15:15
  • remove hsql from your maven dependencies - I guess its there... mysql should be picked up automatically now... – kukkuz Mar 10 '19 at 16:39
  • @kukkuz No there is no dependency for Hsql. I've edit my question with my pom. – BobCoder Mar 10 '19 at 16:47
  • another question - do you have a custom `JobLauncher` bean defined in your config? if yes, then you have to pass the `Datasource` to this bean... – kukkuz Mar 10 '19 at 16:52
  • @kukkuz You may be right but all I want to create the spring batch schema in mySql instead of in memory. I have disabled job run in application.properties file . spring.batch.job.enabled=false so job will not launch. – BobCoder Mar 10 '19 at 16:56
  • `No there is no dependency for Hsql. I've edit my question with my pom`: in that case, the error "No database type set, using meta data indicating: HSQL" should not happen anymore. Please update the question again. I added an answer, it should fix your issue. – Mahmoud Ben Hassine Mar 11 '19 at 09:27

2 Answers2

0

You shoud add this:

  1. spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
0

DDL scripts are in the org.springframework.batch.core package, so your spring.datasource.schema should be:

spring.datasource.schema=org/springframework/batch/core/schema-mysql.sql

You can even omit this property as it will be detected automatically from your datasource.

Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50