44. What is transactional write-behind

Hibernate uses a sophisticated algorithm to determine an efficient ordering that avoids database foreign key constraint violations but is still sufficiently predictable to the user. This feature is called transactional write-behind.

1 comment:

  1. Calling session.save(entity) in the code does not cause an immediate sql insert to be fired. Similarly session.delete(entity) or session.update(entity) will not result immediately in the execution of the sql delete or update queries being fired.
    When objects associated with the persistence context are modified, they are not immediately propagated to the database.
    What Hibernate does is instead collect all such database operations associated with a transaction, creating the minimum set of sql queries and executing them. This gives us two advantages -

    Every property change in the entity does not cause a separate sql update to be executed.
    Avoiding unwanted sql queries ensures minimum trips to database thus reducing the network latency
    In case of multiple updates/inserts or deletes, Hibernate is able to make use of the JDBC Batch API to optimize performance.

    This delayed execution of sql queries is known as transactional write behind. On calling a transaction.commit(), Hibernate flushes all the sql to the database.