λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
Spring/Spring Batch

Spring Batch (3) DB μŠ€ν‚€λ§ˆ

by 주발2 2022. 10. 30.
λ°˜μ‘ν˜•

πŸ“Œ ν•΄λ‹Ή ν¬μŠ€νŒ…μ˜ λͺ©ν‘œ

  • Spring Batch DB μŠ€ν‚€λ§ˆ 뢄석

 

 

πŸ“Œ Spring Batch DB μŠ€ν‚€λ§ˆ

μ§€λ‚œ μ‹œκ°„μ—λŠ” Spring Batch의 κ°œμš” 및 ν”„λ‘œμ νŠΈ ꡬ성, μ„ΈνŒ… 및 κ°„λ‹¨ν•œ Job 싀행에 λŒ€ν•΄ μ‚΄νŽ΄λ³΄μ•˜μŠ΅λ‹ˆλ‹€.

이번 μ‹œκ°„μ—λŠ” Spring Batch의 Meta Data Schema에 λŒ€ν•΄ μ‚΄νŽ΄λ³΄κ² μŠ΅λ‹ˆλ‹€.

 

Meta Data Schema

  • μŠ€ν”„λ§ 배치의 μ‹€ν–‰ 및 관리λ₯Ό μœ„ν•œ λͺ©μ μœΌλ‘œ μ—¬λŸ¬ 도메인듀(Job, Step, JobParameters λ“±)에 λŒ€ν•œ 정보λ₯Ό μ €μž₯, μ—…λ°μ΄νŠΈ, 쑰회 λ“±μ˜ μž‘μ—…μ„ ν•  수 μžˆλŠ” μŠ€ν‚€λ§ˆ 제곡
  • κ³Όκ±° 및 ν˜„μž¬μ˜ Batch 싀행에 λŒ€ν•œ 정보(성곡, μ‹€νŒ¨ μ—¬λΆ€)λ₯Ό κ΄€λ¦¬ν•¨μœΌλ‘œμ„œ 배치운영 및 μž₯애와 κ΄€λ ¨λœ λŒ€μ²˜ κ°€λŠ₯
  • DB와 연동할 경우 ν•„μˆ˜μ μœΌλ‘œ 메타 ν…Œμ΄λΈ”μ΄ μƒμ„±λ˜μ–΄μ•Ό 함

 

DB μŠ€ν‚€λ§ˆ 정보

spring-batch-core 라이브러리의 내뢀에 schema-*.sql 파일의 λ°μ΄ν„°λ² μ΄μŠ€ 별 메타 데이터 μŠ€ν‚€λ§ˆκ°€ μ‘΄μž¬ν•©λ‹ˆλ‹€.

 

λŒ€ν‘œμ μœΌλ‘œ schema-mysql.sql νŒŒμΌμ— μž‘μ„±λ˜μ–΄ μžˆλŠ” ν…Œμ΄λΈ”μ— λŒ€ν•œ μ •λ³΄λŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

(λ°μ΄ν„°λ² μ΄μŠ€μ— 따라 μΏΌλ¦¬λŠ” 일뢀 λ‹€λ¦…λ‹ˆλ‹€.)

-- Autogenerated: do not edit this file

CREATE TABLE BATCH_JOB_INSTANCE  (
	JOB_INSTANCE_ID BIGINT  NOT NULL PRIMARY KEY ,
	VERSION BIGINT ,
	JOB_NAME VARCHAR(100) NOT NULL,
	JOB_KEY VARCHAR(32) NOT NULL,
	constraint JOB_INST_UN unique (JOB_NAME, JOB_KEY)
) ENGINE=InnoDB;

CREATE TABLE BATCH_JOB_EXECUTION  (
	JOB_EXECUTION_ID BIGINT  NOT NULL PRIMARY KEY ,
	VERSION BIGINT  ,
	JOB_INSTANCE_ID BIGINT NOT NULL,
	CREATE_TIME DATETIME(6) NOT NULL,
	START_TIME DATETIME(6) DEFAULT NULL ,
	END_TIME DATETIME(6) DEFAULT NULL ,
	STATUS VARCHAR(10) ,
	EXIT_CODE VARCHAR(2500) ,
	EXIT_MESSAGE VARCHAR(2500) ,
	LAST_UPDATED DATETIME(6),
	JOB_CONFIGURATION_LOCATION VARCHAR(2500) NULL,
	constraint JOB_INST_EXEC_FK foreign key (JOB_INSTANCE_ID)
	references BATCH_JOB_INSTANCE(JOB_INSTANCE_ID)
) ENGINE=InnoDB;

CREATE TABLE BATCH_JOB_EXECUTION_PARAMS  (
	JOB_EXECUTION_ID BIGINT NOT NULL ,
	TYPE_CD VARCHAR(6) NOT NULL ,
	KEY_NAME VARCHAR(100) NOT NULL ,
	STRING_VAL VARCHAR(250) ,
	DATE_VAL DATETIME(6) DEFAULT NULL ,
	LONG_VAL BIGINT ,
	DOUBLE_VAL DOUBLE PRECISION ,
	IDENTIFYING CHAR(1) NOT NULL ,
	constraint JOB_EXEC_PARAMS_FK foreign key (JOB_EXECUTION_ID)
	references BATCH_JOB_EXECUTION(JOB_EXECUTION_ID)
) ENGINE=InnoDB;

CREATE TABLE BATCH_STEP_EXECUTION  (
	STEP_EXECUTION_ID BIGINT  NOT NULL PRIMARY KEY ,
	VERSION BIGINT NOT NULL,
	STEP_NAME VARCHAR(100) NOT NULL,
	JOB_EXECUTION_ID BIGINT NOT NULL,
	START_TIME DATETIME(6) NOT NULL ,
	END_TIME DATETIME(6) DEFAULT NULL ,
	STATUS VARCHAR(10) ,
	COMMIT_COUNT BIGINT ,
	READ_COUNT BIGINT ,
	FILTER_COUNT BIGINT ,
	WRITE_COUNT BIGINT ,
	READ_SKIP_COUNT BIGINT ,
	WRITE_SKIP_COUNT BIGINT ,
	PROCESS_SKIP_COUNT BIGINT ,
	ROLLBACK_COUNT BIGINT ,
	EXIT_CODE VARCHAR(2500) ,
	EXIT_MESSAGE VARCHAR(2500) ,
	LAST_UPDATED DATETIME(6),
	constraint JOB_EXEC_STEP_FK foreign key (JOB_EXECUTION_ID)
	references BATCH_JOB_EXECUTION(JOB_EXECUTION_ID)
) ENGINE=InnoDB;

CREATE TABLE BATCH_STEP_EXECUTION_CONTEXT  (
	STEP_EXECUTION_ID BIGINT NOT NULL PRIMARY KEY,
	SHORT_CONTEXT VARCHAR(2500) NOT NULL,
	SERIALIZED_CONTEXT TEXT ,
	constraint STEP_EXEC_CTX_FK foreign key (STEP_EXECUTION_ID)
	references BATCH_STEP_EXECUTION(STEP_EXECUTION_ID)
) ENGINE=InnoDB;

CREATE TABLE BATCH_JOB_EXECUTION_CONTEXT  (
	JOB_EXECUTION_ID BIGINT NOT NULL PRIMARY KEY,
	SHORT_CONTEXT VARCHAR(2500) NOT NULL,
	SERIALIZED_CONTEXT TEXT ,
	constraint JOB_EXEC_CTX_FK foreign key (JOB_EXECUTION_ID)
	references BATCH_JOB_EXECUTION(JOB_EXECUTION_ID)
) ENGINE=InnoDB;

CREATE TABLE BATCH_STEP_EXECUTION_SEQ (
	ID BIGINT NOT NULL,
	UNIQUE_KEY CHAR(1) NOT NULL,
	constraint UNIQUE_KEY_UN unique (UNIQUE_KEY)
) ENGINE=InnoDB;

INSERT INTO BATCH_STEP_EXECUTION_SEQ (ID, UNIQUE_KEY) select * from (select 0 as ID, '0' as UNIQUE_KEY) as tmp where not exists(select * from BATCH_STEP_EXECUTION_SEQ);

CREATE TABLE BATCH_JOB_EXECUTION_SEQ (
	ID BIGINT NOT NULL,
	UNIQUE_KEY CHAR(1) NOT NULL,
	constraint UNIQUE_KEY_UN unique (UNIQUE_KEY)
) ENGINE=InnoDB;

INSERT INTO BATCH_JOB_EXECUTION_SEQ (ID, UNIQUE_KEY) select * from (select 0 as ID, '0' as UNIQUE_KEY) as tmp where not exists(select * from BATCH_JOB_EXECUTION_SEQ);

CREATE TABLE BATCH_JOB_SEQ (
	ID BIGINT NOT NULL,
	UNIQUE_KEY CHAR(1) NOT NULL,
	constraint UNIQUE_KEY_UN unique (UNIQUE_KEY)
) ENGINE=InnoDB;

INSERT INTO BATCH_JOB_SEQ (ID, UNIQUE_KEY) select * from (select 0 as ID, '0' as UNIQUE_KEY) as tmp where not exists(select * from BATCH_JOB_SEQ);

MySQL의 경우 Job, Step κ΄€λ ¨ 6개의 ν…Œμ΄λΈ”κ³Ό 3개의 μ‹œν€€μŠ€μ— λŒ€ν•œ ν…Œμ΄λΈ”μ„ μƒμ„±ν•©λ‹ˆλ‹€.

  • Job: BATCH_JOB_INSTANCE, BATCH_JOB_EXECUTION, BATCH_JOB_EXECUTION_PARAMS, BATCH_JOB_EXECUTION_CONTEXT
  • Step: BATCH_STEP_EXECUTION, BATCH_STEP_EXECUTION_CONTEXT
  • Seq: BATCH_JOB_SEQ, BATCH_JOB_EXECUTION_SEQ, BATCH_STEP_EXECUTION_SEQ

 

μŠ€ν‚€λ§ˆ 생성 μ„€μ •

λ°μ΄ν„°λ² μ΄μŠ€ μŠ€ν‚€λ§ˆλŠ” μˆ˜λ™ 및 μžλ™μœΌλ‘œ 생성할 수 μžˆμŠ΅λ‹ˆλ‹€.

(μš΄μ˜μ—μ„œλŠ” μˆ˜λ™μœΌλ‘œ 슀크립트λ₯Ό μƒμ„±ν•˜μ—¬ μ„€μ •ν•˜λŠ” 것을 ꢌμž₯ν•©λ‹ˆλ‹€.)

  • μˆ˜λ™ 생성: μœ„ schema-*.sql νŒŒμΌμ— μž‘μ„±λœ 쿼리λ₯Ό λ³΅μ‚¬ν•˜μ—¬ 직접 μ‹€ν–‰
  • μžλ™ 생성: spring.batch.jdbc.initialize-schema μ„€μ •
    • ALWAYS: 슀크립트λ₯Ό 항상 μ‹€ν–‰
    • EMBEDDED: λ‚΄μž₯ DB일 λ•Œλ§Œ μ‹€ν–‰λ˜λ©° μŠ€ν‚€λ§ˆκ°€ μžλ™ 생성(default)
    • NEVER: 슀크립트 μ‹€ν–‰ X

 

MetaData Schema

https://docs.spring.io/spring-batch/docs/current/reference/html/schema-appendix.html

BATCH_JOB_INSTANCE

  • Job이 싀행될 λ•Œ JobInstance 정보가 μ €μž₯
  • μ‹€ν–‰λ˜λŠ” Job의 Name, Key에 λŒ€ν•œ 데이터λ₯Ό μ €μž₯

 

BATCH_JOB_EXECUTION

  • Job의 μ‹€ν–‰ 정보가 μ €μž₯되며 Job의 생성, μ‹œμž‘, μ’…λ£Œ μ‹œκ°, μ‹€ν–‰μƒνƒœ, λ©”μ‹œμ§€ 등을 관리

 

BATCH_JOB_EXECUTION_PARAMS

  • Jobκ³Ό ν•¨κ»˜ μ‹€ν–‰λ˜λŠ” JobParameters 정보λ₯Ό μ €μž₯

 

BATCH_JOB_EXECUTION_CONTEXT

  • Job의 μ‹€ν–‰λ™μ•ˆ μ—¬λŸ¬ μƒνƒœμ •λ³΄, 곡유 데이터λ₯Ό 직렬화(JSON)ν•΄μ„œ μ €μž₯
  • Step κ°„ 데이터 κ³΅μœ κ°€ κ°€λŠ₯함

 

BATCH_STEP_EXECUTION

  • Step의 μ‹€ν–‰ 정보가 μ €μž₯되며 Step의 생성, μ‹œμž‘, μ’…λ£Œ μ‹œκ°, μ‹€ν–‰μƒνƒœ, λ©”μ‹œμ§€ 등을 관리

 

BATCH_STEP_EXECUTION_CONTEXT

  • Step의 μ‹€ν–‰λ™μ•ˆ μ—¬λŸ¬ μƒνƒœμ •λ³΄, 곡유 데이터λ₯Ό 직렬화(JSON)ν•΄μ„œ μ €μž₯
  • Stepλ³„λ‘œ μ €μž₯되며 Stepκ°„ 데이터λ₯Ό κ³΅μœ ν•  수 μ—†μŒ

 

각 ν…Œμ΄λΈ”μ— λŒ€ν•œ μ»¬λŸΌμ€ PK, Name, μ‹€ν–‰ 및 μ’…λ£Œ μ‹œκ°, μƒνƒœ, μ’…λ£Œ μ½”λ“œ, VERSION(Optimistic Locking μ „λž΅), μ‹€νŒ¨ λ©”μ‹œμ§€ 등에 λŒ€ν•œ 데이터λ₯Ό μ €μž₯ν•˜λŠ”λ°μš”, μ»¬λŸΌμ— λŒ€ν•œ μ„€λͺ…은 κ³΅μ‹λ¬Έμ„œμ— μžμ„Ένžˆ λ‚˜μ™€μžˆμŠ΅λ‹ˆλ‹€.

 

 

πŸ“ƒ μ°Έκ³  λ¬Έμ„œ

 

λ°˜μ‘ν˜•

λŒ“κΈ€