Hibernate / MyBatis Interview questions
How does MyBatis's first-level cache work?
The first-level cache is a session-scoped cache, enabled by default and not independently configurable off, that stores the results of queries executed within a single SqlSession — if the exact same query with the exact same parameters is executed again on that same session, MyBatis returns the cached result instead of hitting the database a second time.
Because the cache is scoped to a single session, it's automatically cleared whenever that session commits, rolls back, or is closed, and it's also cleared by any insert, update, or delete executed on that same session — both behaviors exist specifically to prevent the cache from ever serving stale data that would contradict changes the session itself just made.
This scoping is also why the first-level cache provides no benefit across separate sessions: two different SqlSession instances, even executing the identical query with identical parameters, will not share cached results, since the cache exists entirely within the lifetime of one session and is discarded along with it.
More Related questions...