Hibernate / MyBatis Interview questions
Explain the role of the Executor in MyBatis's internal architecture, including the SIMPLE, REUSE, and BATCH executor types?
The Executor is the core internal component responsible for actually carrying out a statement's execution against the database, sitting between the SqlSession's public API and the lower-level StatementHandler/ParameterHandler/ResultSetHandler pipeline, and MyBatis supports three distinct Executor implementations trading off differently between simplicity, statement reuse, and batching efficiency.
| Executor Type | Behavior |
| SIMPLE | Creates a new PreparedStatement for every single execution; the default |
| REUSE | Caches and reuses PreparedStatement objects keyed by SQL text within a session |
| BATCH | Groups multiple update statements together, deferring actual execution until flush/commit |
SIMPLE is the safe, predictable default, appropriate for typical request-scoped usage where statement reuse across many executions within the same session isn't a significant concern; REUSE becomes beneficial when the same session executes the identical SQL text repeatedly, since reusing a prepared statement avoids the overhead of re-preparing it with the database driver each time.
BATCH is specifically for high-volume insert/update/delete scenarios, deferring the actual JDBC execution of accumulated statements until commit() or an explicit flush, which is how it achieves its efficiency gain — sending many statements together rather than one at a time — at the cost of not knowing individual statement results (like affected row counts) until that deferred execution actually happens, which is an important behavioral difference to keep in mind when writing code that depends on immediate per-statement feedback.
More Related questions...