Describe the Second Level Cache in Hibernate.

The second-level cache is an optional, SessionFactory-scoped cache in Hibernate that stores entity data across multiple sessions, so repeated lookups of the same data don't have to hit the database each time.

Key Points: • Unlike the first-level cache (tied to a single Session), the second-level cache is shared across all sessions created from the same SessionFactory. • It's disabled by default and requires a caching provider (e.g. Ehcache, Infinispan, Caffeine) plus explicit @Cacheable / @Cache annotations on entities. • Best suited for read-mostly, rarely-changing reference data — heavily-written entities gain little and add cache-invalidation overhead. • A related query cache can also store the results of specific HQL/JPQL queries, separate from entity data caching. • Cache concurrency strategies (read-only, read-write, nonstrict-read-write, transactional) control how updates interact with cached entries.

Example: A lookup table like Country or Currency, which rarely changes but is read constantly across the application, is a good second-level cache candidate — caching it avoids re-querying the database on every request that needs a country name.

Interview Tip: A concise interview answer is:

"The second-level cache sits at the SessionFactory level and is shared across sessions, unlike the first-level cache which is per-session. It needs an external provider like Ehcache configured, and it's most valuable for read-heavy, rarely-changing reference data rather than frequently updated entities."