Spring Batch Framework - Auto create Batch Table

后端 未结 9 1558
广开言路
广开言路 2020-12-01 12:02

I just created a batch job using Spring Batch framework, but I don\'t have Database privileges to run CREATE SQL. When I try to run the batch job I hit the error while the f

相关标签:
9条回答
  • 2020-12-01 12:32

    Spring Batch uses the database to save metadata for its recover/retry functionality.

    If you can't create tables in the database then you have to disable this behaviour

    If you can create the batch metadata tables but not in runtime then you might create them manually

    0 讨论(0)
  • 2020-12-01 12:33

    To enable auto create spring batch data-schema simply add this line to your spring application.properties file :

    spring.batch.initialize-schema=always

    To understand more about Spring batch meta-data schema :

    https://docs.spring.io/spring-batch/trunk/reference/html/metaDataSchema.html

    0 讨论(0)
  • 2020-12-01 12:39

    With Spring Boot 2.0 you probably need this: https://docs.spring.io/spring-boot/docs/2.0.0.M7/reference/htmlsingle/#howto-initialize-a-spring-batch-database

    spring.batch.initialize-schema=always
    

    By default it will only create the tables if you are using an embedded database.

    Or

     spring.batch.initialize-schema=never
    

    To permanently disable it.

    0 讨论(0)
  • 2020-12-01 12:39

    <jdbc:initialize-database/> tag is parsed by Spring using InitializeDatabaseBeanDefinitionParser. You can try debugging this class in your IDE to make sure what values are being picked up for enabled attribute. Also this value can be disabled by using JVM parameter -Dspring.batch.initializer.enabled=false

    0 讨论(0)
  • 2020-12-01 12:46

    Spring Batch required following tables to run job

    • BATCH_JOB_EXECUTION
    • BATCH_JOB_EXECUTION_CONTEXT
    • BATCH_JOB_EXECUTION_PARAMS
    • BATCH_JOB_EXECUTION_SEQ
    • BATCH_JOB_INSTANCE
    • BATCH_JOB_SEQ
    • BATCH_STEP_EXECUTION
    • BATCH_STEP_EXECUTION_CONTEXT
    • BATCH_STEP_EXECUTION_SEQ

    If you are using h2 db then it will create all required table by default

    • spring.h2.console.enabled=true
    • spring.datasource.url=jdbc:h2:mem:testdb
    • spring.datasource.driverClassName=org.h2.Driver
    • spring.datasource.username=sa
    • spring.datasource.password=
    • spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

    When you start using Mysql or any other database you need to add follwing properties into application.properties

                   spring.batch.initialize-schema=always
    
    0 讨论(0)
  • 2020-12-01 12:50
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
            http://www.springframework.org/schema/jdbc 
            http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd">
    
    
        <!-- database -->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <property name="url" value="jdbc:mysql://localhost:3306/springbatch" />
            <property name="username" value="root" />
            <property name="password" value="" />
        </bean>
    
        <!-- transaction manager -->
        <bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
    
        <!-- create job-meta tables automatically -->
        <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>
    </beans>
    

    And make sure you are using compatible spring-jdbc -version with spring-batch. Most probably spring-jdbc-3.2.2.RELEASE.JAR compatible.

    0 讨论(0)
提交回复
热议问题