| It is managed as part of the flush process. The main part is `JdbcCoordinator#getBatch(BatchKey)`:
@Override
public Batch getBatch(BatchKey key) {
if ( currentBatch != null ) {
if ( currentBatch.getKey().equals( key ) ) {
return currentBatch;
}
else {
currentBatch.execute();
currentBatch.release();
}
}
currentBatch = batchBuilder().buildBatch( key, this );
return currentBatch;
}
The BatchKey is primarily defined as the action type (insert, update, ...) plus the domain type (entity name, collection role). It comes from an action (Executable) in the ActionQueue. From there we add the values for the action being processed to the Batch. That operation could implicitly trigger the Batch to be executed if we have exceeded the configured number of "row values" allowed in batches (`hibernate.jdbc.batch_size`). At the end of the flush process we execute the currentBatch. An example might be easier. Imagine we have configured the batch size to be 2 and are in the process of a flush with an action stack like:
EntityAction(INSERT,Book,1)
EntityAction(INSERT,Page,1)
EntityAction(INSERT,Page,2)
EntityAction(INSERT,Page,3)
This would be a flow of the following BatchKeys:
[INSERT,Book]
[INSERT,Page]
[INSERT,Page]
[INSERT,Page]
We'd have the following outcome
- Because there is not yet any currentBatch we create one. The values for that Book INSERT are added to the batch. These are the first values added to the batch, so we do not trigger the execution.
- currentBatch is the Batch we created earlier for [INSERT,Book]. Here, the keys do not match so we will execute the current currentBatch, release it and create a new Batch for [INSERT,Page]. The values for Page#1 are added to the batch. As this is the first set of values in this batch, so we do not trigger the execution.
- currentBatch is the Batch we created earlier for [INSERT,Page] from Page#1. The BatchKeys match so we re-use it. The values for Page#2 are added to the batch. We still have not exceeded the batch size, so we do not trigger the execution.
- currentBatch is still the Batch we created earlier for [INSERT,Page] from Page#1. The batch keys match so we re-use it. At this point we have exceeded the batch size, so we trigger the implicit execution. Then we add the values for Page#3 to the batch. As this is the first set of values in this batch, so we do not trigger the execution.
At this point we have no more actions to process, but we still have the "in-flight" Batch. So one of the final steps of the flush process is to execute the currentBatch, if one. HTH |