Automatic Batching Examples
In the previous chapter, we talked about Ent Framework calls batching. Let's provide some more examples.
Batching of load*() Calls
The following code will produce only one SQL query:
SQL query produced under the hood:
Batching of insert*() Calls
Since insertReturning()
first inserts the Ent into the database and then loads the inserted data back, the following code will produce 2 SQL queries.
SQL queries produced:
Even if insertReturning()
is called in nested functions, Ent Framework will still batch them properly and produce just 2 queries:
Batching of Update, Delete and all Other Calls
All Ent Framework API calls are subject for batching the way --described above.
De-batching and Deadlocks
As in most of MVCC databases, In PostgreSQL, reads never block writes, and writes never block reads. Still, if two clients update the same row in the database, one client has to wait for another one to finish.
Deadlocks may occur during the automatic queries batching. It is rare (especially since Ent Framework always orders the updating rows in a consistent way, by e.g. id), but may still happen.
In case of a rare deadlock, when Ent Framework knows that it's safe to retry the write, it performs de-batching: splits the batched query into individual queries and runs them in parallel, independently. This solves the problem of deadlocks entirely, in an exchange of very rare slowdown of the mass insert, update or delete operations.
Last updated
Was this helpful?