Uploaded image for project: 'Configuration Persistence Service'
  1. Configuration Persistence Service
  2. CPS-1795

Enable Hibernate write batching

    XMLWordPrintable

Details

    Description

      Hibernate doesn't enable write batching by default. This means that it will send a separate SQL statement for each insert/update operation, greatly limiting write performance.

      Enabling write batching will greatly improve write performance.

      However, as per the Hibernate documentation for IDENTITY generation:

      Hibernate will not be able to batch INSERT statements for the entities using the IDENTITY generation.

      We are presently using IDENTITY generation. Thus it is required to change the ID generation strategy from IDENTITY to SEQUENCE generator. This also requires a change to database schema: instead of auto-incrementing the fragment ID, a sequence generator is added, allowing Hibernate to assign IDs before writing, by requesting the next ID from a sequence that increases by some allocation size (50 in this case):

      ALTER TABLE fragment ALTER COLUMN id DROP IDENTITY;
      CREATE SEQUENCE fragment_id_seq INCREMENT BY 50 START WITH 50;

      The FragmentEntity class can be configured with appropriate annotations to use the sequence generator:

      @Id
      @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "fragment_id_seq_generator")
      @SequenceGenerator(name = "fragment_id_seq_generator", sequenceName = "fragment_id_seq", allocationSize = 50)
      private Long id; 

      And the write batching can be finally enabled with appropriate config in application.yml:

      spring:
          jpa:
              properties:
                  hibernate:
                      id:
                          new_generator_mappings: true
                      order_inserts: true
                      order_updates: true
                      batch_versioned_data: true
                      jdbc:
                          batch_size: 100

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            danielhanrahan Daniel Hanrahan
            danielhanrahan Daniel Hanrahan
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: