Spring Batch Framework - 自动创建批处理表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33249942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:50:12  来源:igfitidea点击:

Spring Batch Framework - Auto create Batch Table

databasespringspring-batchprivilegesora-00942

提问by Einn Hann

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 framework tried to create TABLE_BATCH_INSTANCE. I try to disable the

我刚刚使用 Spring Batch 框架创建了一个批处理作业,但我没有运行 CREATE SQL 的数据库权限。当我尝试运行批处理作业时,我在框架尝试创建 TABLE_BATCH_INSTANCE 时遇到了错误。我尝试禁用

<jdbc:initialize-database data-source="dataSource" enabled="false">    
 ...
</jdbc:initialize-database>

But after I tried I still hit the error

但是在我尝试之后我仍然遇到错误

org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [SELECT JOB_INSTANCE_ID, JOB_NAME from BATCH_JOB_INSTANCE where JOB_NAME = ? and JOB_KEY = ?]; nested exception is java.sql.SQLSyntaxErrorException: ORA-00942: table or view does not exist

Anyway can disable the SQL, I just want to test my reader writer and processor work properly.

反正可以禁用SQL,我只是想测试我的读写器和处理器是否正常工作。

采纳答案by Sergio

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

Spring Batch 使用数据库为其恢复/重试功能保存元数据。

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

如果您可以创建批处理元数据表,但不能在运行时创建,那么您可以手动创建它们

回答by Pim Hazebroek

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 Boot 2.0 你可能需要这个: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.

永久禁用它。

回答by BlaCk HoLe

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

要启用自动创建 spring 批处理数据架构,只需将此行添加到您的 spring application.properties 文件中:

spring.batch.initialize-schema=always

spring.batch.initialize-schema=总是

To understand more about Spring batch meta-data schema :

要了解有关 Spring 批处理元数据模式的更多信息:

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

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

回答by vaquar khan

Spring Batch required following tables to run job

Spring Batch 需要下表来运行作业

  • 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
  • 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

如果您使用的是 h2 db,则默认情况下它将创建所有必需的表

  • 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
  • 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

当您开始使用 Mysql 或任何其他数据库时,您需要将以下属性添加到 application.properties 中

               spring.batch.initialize-schema=always

回答by Dherik

Seems silly, but someone can have the same problem.

看起来很傻,但有人可能会遇到同样的问题。

I was receiving this error after drop all tables from a database. When I tried to start the Spring Batch, I received the error:

从数据库中删除所有表后,我收到此错误。当我尝试启动 Spring Batch 时,收到错误消息:

bad SQL grammar [SELECT JOB_INSTANCE_ID, JOB_NAME from BATCH_JOB_INSTANCE where JOB_NAME = ? and JOB_KEY = ?]

错误的 SQL 语法 [SELECT JOB_INSTANCE_ID, JOB_NAME from BATCH_JOB_INSTANCE where JOB_NAME = ? 和 JOB_KEY = ?]

and:

和:

Invalid object name 'BATCH_JOB_INSTANCE'

无效的对象名称“BATCH_JOB_INSTANCE”

This happened to me because I drop the tables without restart the service. The service was started and receive the database metadata withthe Batch tables on the database. After drop them and not restart the server, the Spring Batch thought that the tables still exists.

这发生在我身上,因为我在没有重新启动服务的情况下删除了表。服务已启动并接收数据库元数据以及数据库的批处理表。删除它们并且没有重新启动服务器后,Spring Batch 认为这些表仍然存在。

After restart the Spring Batch server and execute the batch again, the tables were created without error.

重新启动 Spring Batch 服务器并再次执行批处理后,创建的表没有错误。

回答by Shalika

<?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.JARcompatible.

并确保您使用兼容的spring-jdbc-version 和spring-batch。很可能与spring-jdbc-3.2.2.RELEASE.JAR兼容。

回答by Investigator

When running with Spring Boot:

使用 Spring Boot 运行时:

Running with Spring Boot v1.5.14.RELEASE, Spring v4.3.18.RELEASE

Running with Spring Boot v1.5.14.RELEASE, Spring v4.3.18.RELEASE

This should be enough:

这应该足够了:

spring:
    batch:
        initializer:
            enabled: false

The initialize-schema did not work for this Spring boot version. After that I was able to copy the SQL scripts from the spring-core jar and change the table capitalization since this was my issue with the automatic table creation under Windows/Mac/Linux.

初始化模式不适用于此 Spring 引导版本。之后,我能够从 spring-core jar 复制 SQL 脚本并更改表大写,因为这是我在 Windows/Mac/Linux 下自动创建表的问题。

回答by Shailendra

<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 enabledattribute. Also this value can be disabled by using JVM parameter -Dspring.batch.initializer.enabled=false

<jdbc:initialize-database/>标签由 Spring 使用InitializeDatabaseBeanDefinitionParser. 您可以尝试在 IDE 中调试此类,以确保为enabled属性选取了哪些值。也可以使用 JVM 参数禁用此值-Dspring.batch.initializer.enabled=false

回答by kozla13

this works for me: Spring boot 2.0

这对我有用:Spring Boot 2.0

  batch:
        initialize-schema: never
        initializer:
            enabled: false