Announcement Announcement Module
No announcement yet.
Spring Batch / Hibernate Batching Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Batch / Hibernate Batching

    Iím using spring batch/spring data jpa and hibernate for a batch process and have an issue regarding batching, Batching isnít working as expected when the service
    reads an entity, updates the entity and then saves the entity.

    I believe this is to do with hibernate and its default flush policy Ė i.e. flush on reads to ensure that the item is synched with the db - what I need to know is there any way around this ?

    I have all the appropriate hibernate batch settings enabled and can see it working in a certain scenario ( inserts only)

    1st example ( not working)
    Writer -->Service-->ServiceDelegate

    write (items ) // where items is the spring batch size
    Customer customer = customerDao.findByName(item.customerId); //1
    Address address = addressDao.findByCustomerId(customerId);
    customer.setAddress(address);; //2


    with the above approach, hibernate produces batches only of 1, even though the batch size is 10, tracing through to the DB jdbc layer I can see executeBatch being called with only 1 statement

    Where it works is when there is no prior read or find on the entity, the following code works as expected and the inserts are batched by hibernate correctly.

    2nd example( working)

    write (items ) // where items is the spring batch size
    Customer customer = new Customer;
    customer.setLastName(item.getLastName();; //1


    with the appove methods, hibernate is producing batches of 10 and I can trace to the db to see excuteBatch working with 10 statements

    Any ideas?

  • #2
    IMHO your writer is doing to much, it is a writer so it should do only writing but you are doing reading, processing and writing in a writer... IMHO you should have a reader which reads customers a processor which reads and couples the address and then a writer which does nothing more than save the customer. (Reading then occurs in a different transaction).

    You could try messing around with the flush mode but this might give you problems if you want to read the same/new object in the same transaction/sesion (because the changes aren't visible in the database yet, hence the flush ode AUTO).