15. 附录B Meta-Data Schema

B.1 Overview

The Spring Batch Meta-Data tables very closely match the Domain objects that represent them in Java. For example, JobInstance, JobExecution, JobParameters, and StepExecution map to BATCH_JOB_INSTANCE, BATCH_JOB_EXECUTION, BATCH_JOB_EXECUTION_PARAMS, and BATCH_STEP_EXECUTION, respectively. ExecutionContext maps to both BATCH_JOB_EXECUTION_CONTEXT and BATCH_STEP_EXECUTION_CONTEXT. The JobRepository is responsible for saving and storing each Java object into its correct table. The following appendix describes the meta-data tables in detail, along with many of the design decisions that were made when creating them. When viewing the various table creation statements below, it is important to realize that the data types used are as generic as possible. Spring Batch provides many schemas as examples, which all have varying data types due to variations in individual database vendors’ handling of data types. Below is an ERD model of all 6 tables and their relationships to one another:

附录B - 图1

B.1.1 Example DDL Scripts

The Spring Batch Core JAR file contains example scripts to create the relational tables for a number of database platforms (which are in turn auto-detected by the job repository factory bean or namespace equivalent). These scripts can be used as is, or modified with additional indexes and constraints as desired. The file names are in the form schema-.sql, where ““ is the short name of the target database platform. The scripts are in the package org.springframework.batch.core.

B.1.2 Version

Many of the database tables discussed in this appendix contain a version column. This column is important because Spring Batch employs an optimistic locking strategy when dealing with updates to the database. This means that each time a record is ‘touched’ (updated) the value in the version column is incremented by one. When the repository goes back to try and save the value, if the version number has change it will throw OptimisticLockingFailureException, indicating there has been an error with concurrent access. This check is necessary since, even though different batch jobs may be running in different machines, they are all using the same database tables.

B.1.3 Identity

BATCH_JOB_INSTANCE, BATCH_JOB_EXECUTION, and BATCH_STEP_EXECUTION each contain columns ending in _ID. These fields act as primary keys for their respective tables. However, they are not database generated keys, but rather they are generated by separate sequences. This is necessary because after inserting one of the domain objects into the database, the key it is given needs to be set on the actual object so that they can be uniquely identified in Java. Newer database drivers (Jdbc 3.0 and up) support this feature with database generated keys, but rather than requiring it, sequences were used. Each variation of the schema will contain some form of the following:

  1. CREATE SEQUENCE BATCH_STEP_EXECUTION_SEQ;
  2. CREATE SEQUENCE BATCH_JOB_EXECUTION_SEQ;
  3. CREATE SEQUENCE BATCH_JOB_SEQ;

Many database vendors don’t support sequences. In these cases, work-arounds are used, such as the following for MySQL:

  1. CREATE TABLE BATCH_STEP_EXECUTION_SEQ (ID BIGINT NOT NULL) type=InnoDB;
  2. INSERT INTO BATCH_STEP_EXECUTION_SEQ values(0);
  3. CREATE TABLE BATCH_JOB_EXECUTION_SEQ (ID BIGINT NOT NULL) type=InnoDB;
  4. INSERT INTO BATCH_JOB_EXECUTION_SEQ values(0);
  5. CREATE TABLE BATCH_JOB_SEQ (ID BIGINT NOT NULL) type=InnoDB;
  6. INSERT INTO BATCH_JOB_SEQ values(0);

In the above case, a table is used in place of each sequence. The Spring core class MySQLMaxValueIncrementer will then increment the one column in this sequence in order to give similar functionality.

B.2 BATCH_JOB_INSTANCE

The BATCH_JOB_INSTANCE table holds all information relevant to a JobInstance, and serves as the top of the overall hierarchy. The following generic DDL statement is used to create it:

  1. CREATE TABLE BATCH_JOB_INSTANCE (
  2. JOB_INSTANCE_ID BIGINT PRIMARY KEY ,
  3. VERSION BIGINT,
  4. JOB_NAME VARCHAR(100) NOT NULL ,
  5. JOB_KEY VARCHAR(2500)
  6. );

Below are descriptions of each column in the table:

  • JOB_INSTANCE_ID: The unique id that will identify the instance, which is also the primary key. The value of this column should be obtainable by calling the getId method on JobInstance.
  • VERSION: See above section.
  • JOB_NAME: Name of the job obtained from the Job object. Because it is required to identify the instance, it must not be null.
  • JOB_KEY: A serialization of the JobParameters that uniquely identifies separate instances of the same job from one another. (JobInstances with the same job name must have different JobParameters, and thus, different JOB_KEY values).

B.3 BATCH_JOB_EXECUTION_PARAMS

The BATCH_JOB_EXECUTION_PARAMS table holds all information relevant to the JobParameters object. It contains 0 or more key/value pairs passed to a Job and serve as a record of the parameters a job was run with. For each parameter that contributes to the generation of a job’s identity, the IDENTIFYING flag is set to true. It should be noted that the table has been denormalized. Rather than creating a separate table for each type, there is one table with a column indicating the type:

  1. CREATE TABLE BATCH_JOB_EXECUTION_PARAMS (
  2. JOB_EXECUTION_ID BIGINT NOT NULL ,
  3. TYPE_CD VARCHAR(6) NOT NULL ,
  4. KEY_NAME VARCHAR(100) NOT NULL ,
  5. STRING_VAL VARCHAR(250) ,
  6. DATE_VAL DATETIME DEFAULT NULL ,
  7. LONG_VAL BIGINT ,
  8. DOUBLE_VAL DOUBLE PRECISION ,
  9. IDENTIFYING CHAR(1) NOT NULL ,
  10. constraint JOB_EXEC_PARAMS_FK foreign key (JOB_EXECUTION_ID)
  11. references BATCH_JOB_EXECUTION(JOB_EXECUTION_ID)
  12. );

Below are descriptions for each column:

  • JOB_EXECUTION_ID: Foreign Key from the BATCH_JOB_EXECUTION table that indicates the job execution the parameter entry belongs to. It should be noted that multiple rows (i.e key/value pairs) may exist for each execution.
  • TYPE_CD: String representation of the type of value stored, which can be either a string, date, long, or double. Because the type must be known, it cannot be null.
  • KEY_NAME: The parameter key.
  • STRING_VAL: Parameter value, if the type is string.
  • DATE_VAL: Parameter value, if the type is date.
  • LONG_VAL: Parameter value, if the type is a long.
  • DOUBLE_VAL: Parameter value, if the type is double.
  • IDENTIFYING: Flag indicating if the parameter
  • ontributed to the identity of the related
  • obInstance.

It is worth noting that there is no primary key for this table. This is simply because the framework has no use for one, and thus doesn’t require it. If a user so chooses, one may be added with a database generated key, without causing any issues to the framework itself.

B.4 BATCH_JOB_EXECUTION

The BATCH_JOB_EXECUTION table holds all information relevant to the JobExecution object. Every time a Job is run there will always be a new JobExecution, and a new row in this table:

  1. CREATE TABLE BATCH_JOB_EXECUTION (
  2. JOB_EXECUTION_ID BIGINT PRIMARY KEY ,
  3. VERSION BIGINT,
  4. JOB_INSTANCE_ID BIGINT NOT NULL,
  5. CREATE_TIME TIMESTAMP NOT NULL,
  6. START_TIME TIMESTAMP DEFAULT NULL,
  7. END_TIME TIMESTAMP DEFAULT NULL,
  8. STATUS VARCHAR(10),
  9. EXIT_CODE VARCHAR(20),
  10. EXIT_MESSAGE VARCHAR(2500),
  11. LAST_UPDATED TIMESTAMP,
  12. constraint JOB_INSTANCE_EXECUTION_FK foreign key (JOB_INSTANCE_ID)
  13. references BATCH_JOB_INSTANCE(JOB_INSTANCE_ID)
  14. ) ;

Below are descriptions for each column:

  • JOB_EXECUTION_ID: Primary key that uniquely identifies this execution. The value of this column is obtainable by calling the getId method of the JobExecution object.
  • VERSION: See above section.
  • JOB_INSTANCE_ID: Foreign key from the BATCH_JOB_INSTANCE table indicating the instance to which this execution belongs. There may be more than one execution per instance.
  • CREATE_TIME: Timestamp representing the time that the execution was created.
  • START_TIME: Timestamp representing the time the execution was started.
  • END_TIME: Timestamp representing the time the execution was finished, regardless of success or failure. An empty value in this column even though the job is not currently running indicates that there has been some type of error and the framework was unable to perform a last save before failing.
  • STATUS: Character string representing the status of the execution. This may be COMPLETED, STARTED, etc. The object representation of this column is the BatchStatus enumeration.
  • EXIT_CODE: Character string representing the exit code of the execution. In the case of a command line job, this may be converted into a number.
  • EXIT_MESSAGE: Character string representing a more detailed description of how the job exited. In the case of failure, this might include as much of the stack trace as is possible.
  • LAST_UPDATED: Timestamp representing the last time this execution was persisted.

B.5 BATCH_STEP_EXECUTION

The BATCH_STEP_EXECUTION table holds all information relevant to the StepExecution object. This table is very similar in many ways to the BATCH_JOB_EXECUTION table and there will always be at least one entry per Step for each JobExecution created:

  1. CREATE TABLE BATCH_STEP_EXECUTION (
  2. STEP_EXECUTION_ID BIGINT PRIMARY KEY ,
  3. VERSION BIGINT NOT NULL,
  4. STEP_NAME VARCHAR(100) NOT NULL,
  5. JOB_EXECUTION_ID BIGINT NOT NULL,
  6. START_TIME TIMESTAMP NOT NULL ,
  7. END_TIME TIMESTAMP DEFAULT NULL,
  8. STATUS VARCHAR(10),
  9. COMMIT_COUNT BIGINT ,
  10. READ_COUNT BIGINT ,
  11. FILTER_COUNT BIGINT ,
  12. WRITE_COUNT BIGINT ,
  13. READ_SKIP_COUNT BIGINT ,
  14. WRITE_SKIP_COUNT BIGINT ,
  15. PROCESS_SKIP_COUNT BIGINT ,
  16. ROLLBACK_COUNT BIGINT ,
  17. EXIT_CODE VARCHAR(20) ,
  18. EXIT_MESSAGE VARCHAR(2500) ,
  19. LAST_UPDATED TIMESTAMP,
  20. constraint JOB_EXECUTION_STEP_FK foreign key (JOB_EXECUTION_ID)
  21. references BATCH_JOB_EXECUTION(JOB_EXECUTION_ID)
  22. ) ;

Below are descriptions for each column:

  • STEP_EXECUTION_ID: Primary key that uniquely identifies this execution. The value of this column should be obtainable by calling the getId method of the StepExecution object.
  • VERSION: See above section.
  • STEP_NAME: The name of the step to which this execution belongs.
  • JOB_EXECUTION_ID: Foreign key from the BATCH_JOB_EXECUTION table indicating the JobExecution to which this StepExecution belongs. There may be only one StepExecution for a given JobExecution for a given Step name.
  • START_TIME: Timestamp representing the time the execution was started.
  • END_TIME: Timestamp representing the time the execution was finished, regardless of success or failure. An empty value in this column even though the job is not currently running indicates that there has been some type of error and the framework was unable to perform a last save before failing.
  • STATUS: Character string representing the status of the execution. This may be COMPLETED, STARTED, etc. The object representation of this column is the BatchStatus enumeration.
  • COMMIT_COUNT: The number of times in which the step has committed a transaction during this execution.
  • READ_COUNT: The number of items read during this execution.
  • FILTER_COUNT: The number of items filtered out of this execution.
  • WRITE_COUNT: The number of items written and committed during this execution.
  • READ_SKIP_COUNT: The number of items skipped on read during this execution.
  • WRITE_SKIP_COUNT: The number of items skipped on write during this execution.
  • PROCESS_SKIP_COUNT: The number of items skipped during processing during this execution.
  • ROLLBACK_COUNT: The number of rollbacks during this execution. Note that this count includes each time rollback occurs, including rollbacks for retry and those in the skip recovery procedure.
  • EXIT_CODE: Character string representing the exit code of the execution. In the case of a command line job, this may be converted into a number.
  • EXIT_MESSAGE: Character string representing a more detailed description of how the job exited. In the case of failure, this might include as much of the stack trace as is possible.
  • LAST_UPDATED: Timestamp representing the last time this execution was persisted.

B.6 BATCH_JOB_EXECUTION_CONTEXT

The BATCH_JOB_EXECUTION_CONTEXT table holds all information relevant to an Job’s ExecutionContext. There is exactly one Job ExecutionContext per JobExecution, and it contains all of the job-level data that is needed for a particular job execution. This data typically represents the state that must be retrieved after a failure so that a JobInstance can ‘start from where it left off’.

  1. CREATE TABLE BATCH_JOB_EXECUTION_CONTEXT (
  2. JOB_EXECUTION_ID BIGINT PRIMARY KEY,
  3. SHORT_CONTEXT VARCHAR(2500) NOT NULL,
  4. SERIALIZED_CONTEXT CLOB,
  5. constraint JOB_EXEC_CTX_FK foreign key (JOB_EXECUTION_ID)
  6. references BATCH_JOB_EXECUTION(JOB_EXECUTION_ID)
  7. ) ;

Below are descriptions for each column:

  • JOB_EXECUTION_ID: Foreign key representing the JobExecution to which the context belongs. There may be more than one row associated to a given execution.
  • SHORT_CONTEXT: A string version of the SERIALIZED_CONTEXT.
  • SERIALIZED_CONTEXT: The entire context, serialized.

B.7 BATCH_STEP_EXECUTION_CONTEXT

The BATCH_STEP_EXECUTION_CONTEXT table holds all information relevant to an Step’s ExecutionContext. There is exactly one ExecutionContext per StepExecution, and it contains all of the data that needs to persisted for a particular step execution. This data typically represents the state that must be retrieved after a failure so that a JobInstance can ‘start from where it left off’.

  1. CREATE TABLE BATCH_STEP_EXECUTION_CONTEXT (
  2. STEP_EXECUTION_ID BIGINT PRIMARY KEY,
  3. SHORT_CONTEXT VARCHAR(2500) NOT NULL,
  4. SERIALIZED_CONTEXT CLOB,
  5. constraint STEP_EXEC_CTX_FK foreign key (STEP_EXECUTION_ID)
  6. references BATCH_STEP_EXECUTION(STEP_EXECUTION_ID)
  7. ) ;

Below are descriptions for each column:

STEP_EXECUTION_ID: Foreign key representing the StepExecution to which the context belongs. There may be more than one row associated to a given execution.
SHORT_CONTEXT: A string version of the SERIALIZED_CONTEXT.
SERIALIZED_CONTEXT: The entire context, serialized.

B.8 Archiving

Because there are entries in multiple tables every time a batch job is run, it is common to create an archive strategy for the meta-data tables. The tables themselves are designed to show a record of what happened in the past, and generally won’t affect the run of any job, with a couple of notable exceptions pertaining to restart:

  • The framework will use the meta-data tables to determine if a particular JobInstance has been run before. If it has been run, and the job is not restartable, then an exception will be thrown.
  • If an entry for a JobInstance is removed without having completed successfully, the framework will think that the job is new, rather than a restart.
  • If a job is restarted, the framework will use any data that has been persisted to the ExecutionContext to restore the Job’s state. Therefore, removing any entries from this table for jobs that haven’t completed successfully will prevent them from starting at the correct point if run again.

B.9 International and Multi-byte Characters

If you are using multi-byte character sets (e.g. Chines or Cyrillic) in your business processing, then those characters might need to be persisted in the Spring Batch schema. Many users find that simply changing the schema to double the length of the VARCHAR columns is enough. Others prefer to configure the JobRepository with max-varchar-length half the value of the VARCHAR column length is enough. Some users have also reported that they use NVARCHAR in place of VARCHAR in their schema definitions. The best result will depend on the database platform and the way the database server has been configured locally.

B.10 Recommendations for Indexing Meta Data Tables

Spring Batch provides DDL samples for the meta-data tables in the Core jar file for several common database platforms. Index declarations are not included in that DDL because there are too many variations in how users may want to index depending on their precise platform, local conventions and also the business requirements of how the jobs will be operated. The table below provides some indication as to which columns are going to be used in a WHERE clause by the Dao implementations provided by Spring Batch, and how frequently they might be used, so that individual projects can make up their own minds about indexing.

Table B.1. Where clauses in SQL statements (excluding primary keys) and their approximate frequency of use.

Default Table NameWhere ClauseFrequency
BATCH_JOB_INSTANCEJOB_NAME = ? and JOB_KEY = ?Every time a job is launched
BATCH_JOB_EXECUTIONJOB_INSTANCE_ID = ?Every time a job is restarted
BATCH_EXECUTION_CONTEXTEXECUTION_ID = ? and KEY_NAME = ?On commit interval, a.k.a. chunk
BATCH_STEP_EXECUTIONVERSION = ?On commit interval, a.k.a. chunk (and at start and end of
step)
BATCH_STEP_EXECUTIONSTEP_NAME = ? and JOB_EXECUTION_ID = ?Before each step execution