it-source

스프링 배치 프레임워크 - 배치 테이블 자동 생성

criticalcode 2023. 8. 24. 22:12
반응형

스프링 배치 프레임워크 - 배치 테이블 자동 생성

방금 Spring Batch 프레임워크를 사용하여 배치 작업을 생성했지만 CREATE SQL을 실행할 데이터베이스 권한이 없습니다.배치 작업을 실행하려고 할 때 프레임워크가 TAB를 생성하려고 할 때 오류가 발생했습니다.LE_BATCH_INSTANCE.사용하지 않도록 설정합니다.

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

하지만 시도한 후에도 여전히 오류가 발생했습니다.

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

어쨌든 SQL을 비활성화할 수 있습니다. 리더 라이터와 프로세서가 제대로 작동하는지 테스트하고 싶습니다.

업데이트: 2.5.0 봄부터 다음을 사용해야 합니다.spring.batch.jdbc.initialize-schema대신.출처 참조.


Spring Boot 2.0을 사용하려면 다음이 필요할 것입니다. https://docs.spring.io/spring-boot/docs/2.0.0.M7/reference/htmlsingle/ #how-a-Spring-initialize-a-Spring-spring-spring-spring-spring-sl

spring.batch.initialize-schema=always

기본적으로 포함된 데이터베이스를 사용하는 경우에만 테이블이 작성됩니다.

또는

 spring.batch.initialize-schema=never

영구적으로 비활성화합니다.

Spring Batch는 데이터베이스를 사용하여 복구/재시도 기능을 위한 메타데이터를 저장합니다.

데이터베이스에 테이블을 만들 수 없는 경우동작을 사용하지 않도록 설정해야 합니다.

배치 메타데이터 테이블을 생성할 수 있지만 런타임에는 생성할 수 없는 경우 수동으로 생성할 수 있습니다.

작업을 실행하려면 다음 표에 스프링 배치가 필요합니다.

  • BATCH_JOB_EXECUTION
  • BATCH_JOB_EXECUTION_CONTACLE
  • BATCH_JOB_EXECUTION_PARAMS
  • BATCH_JOB_EXECUTION_SEQ
  • BATCH_JOB_INSTANCE
  • BATCH_JOB_SEQ
  • 배치_스텝_실행
  • BATCH_STEP_EXECUTION_CONTACLE
  • BATCH_STEP_EXECUTION_SEQ

h2db를 사용하는 경우 기본적으로 필요한 모든 테이블이 생성됩니다.

  • spring.h2.spring.enabled=true
  • spring.sysource.url=jdbc:h2:mem:testdb
  • spring.datasource.driverClassName=spring.h2.드라이버
  • spring.sysource.spring=sa
  • spring.sysource.password=
  • spring.jpa.spring-platform=spring.certificate.spring.spring.H2 방언

Mysql 또는 다른 데이터베이스를 사용하기 시작할 때는 application.properties에 다음 속성을 추가해야 합니다.

               spring.batch.initialize-schema=always

Spring Boot 2.5.0 이후 사용

# to always initialize the datasource:
spring.batch.jdbc.initialize-schema=always

# to only initialize an embedded datasource:
spring.batch.jdbc.initialize-schema=embedded

# to never initialize the datasource:
spring.batch.jdbc.initialize-schema=never

(spring.batch.initialize-schema2.7.0에서 제거하기 위해 2.5.0 이후에는 더 이상 사용되지 않습니다.)

스프링 배치 데이터 자동 생성을 활성화하려면 다음 행을 스프링 application.properties 파일에 추가하기만 하면 됩니다.

spring.spring.initialize-spring=항상

Spring 배치 메타데이터 스키마에 대한 자세한 내용은 다음과 같습니다.

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

바보같이 보이지만, 누군가는 같은 문제를 가질 수 있습니다.

데이터베이스에서 모든 테이블을 삭제한 후 이 오류가 발생했습니다.Spring Batch를 시작하려고 할 때 다음 오류가 발생했습니다.

잘못된 SQL 문법 [BATCH_JOB_INSTANCE에서 JOB_INSTANCE_ID, JOB_NAME을 선택합니다. 여기서 JOB_NAME = ?, JOB_KEY = ?]

그리고:

잘못된 개체 이름 'BATCH_JOB_INSTANCE'

서비스를 다시 시작하지 않고 테이블을 떨어뜨리기 때문에 이런 일이 발생했습니다.서비스가 시작되고 데이터베이스에 배치 테이블이 있는 데이터베이스 메타데이터를 수신합니다.서버를 다시 시작하지 않고 삭제한 후 Spring Batch는 테이블이 여전히 존재한다고 생각했습니다.

Spring Batch 서버를 다시 시작하고 배치를 다시 실행한 후 오류 없이 테이블이 생성되었습니다.

Spring Boot로 실행하는 경우:

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

이 정도면 충분합니다.

spring:
    batch:
        initializer:
            enabled: false

이 Spring 부팅 버전에는 초기화 스키마가 작동하지 않았습니다.그 후에 Windows/Mac/Linux에서 자동으로 테이블을 생성하는 문제가 발생했기 때문에 Spring-core jar에서 SQL 스크립트를 복사하고 테이블 대문자를 변경할 수 있었습니다.

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

그리고 spring-batch와 호환되는 spring-jdbc-version을 사용하고 있는지 확인합니다.아마도 spring-jdbc-3.2.2일 것입니다.해제합니다.JAR 호환.

<jdbc:initialize-database/>에서 에는태의구분문석됩다니해를 사용하여 분석합니다.InitializeDatabaseBeanDefinitionParser이 IDE에 대해 할 수 .enabled또한 매개 변수인 JVM할 수 있습니다.-Dspring.batch.initializer.enabled=false

이것은 나에게 효과가 있습니다: Spring boot 2.0

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

위에서 제안한 설정은 더 이상 사용되지 않으므로 다음 설정을 사용합니다.

spring.spring.jdbc.initialize-spring=항상

데이터 소스 구성에서 이 코드를 추가해야 합니다.앱이 나타나면 기존 스키마를 삭제하거나 새 스키마를 만듭니다.

private DataSource dataSource() {
    EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
    return builder.setType(EmbeddedDatabaseType.HSQL)
      .addScript("classpath:org/springframework/batch/core/schema-drop-h2.sql")
      .addScript("classpath:org/springframework/batch/core/schema-h2.sql")
      .build();
}

이렇게 할 수 없는 경우 스키마 스크립트가 .m2 폴더에 있습니다. 스크립트를 수동으로 실행하십시오.위치는 스프링 배치 코어 병 안에 있습니다.

C:\Users\XXX.m2\repository\org\springframework\batch\spring-batch-core\4.3.7.

이 jar를 분할하여 열고 원하는 sql 스키마 스크립트를 찾습니다.

  • org\springframework\core\sql-h2.sql입니다.
  • org\springframework\core\sql-drop-h2.sql입니다.

언급URL : https://stackoverflow.com/questions/33249942/spring-batch-framework-auto-create-batch-table

반응형