Hibernate / Hibernate 7 Basics Interview Questions
What is the second-level cache in Hibernate 7 and how does it work with StatelessSession?
The second-level cache is an optional cross-session application-wide cache. Hibernate 7 changes StatelessSession's behaviour: it now reads/writes the L2 cache by default (was bypassed in v6).
| Aspect | First-level cache | Second-level cache |
|---|---|---|
| Scope | Per Session | Per SessionFactory (application lifetime) |
| Mandatory | Yes | Optional |
| Shared across sessions | No | Yes |
| Provider | Built-in | Infinispan, Ehcache 3, Caffeine |
// Enable L2 cache on an entity: @Entity @Cacheable @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) public class Country { @Id private Long id; private String name; } // Spring Boot 4 configuration: // spring.jpa.properties.hibernate.cache.use_second_level_cache=true // HIBERNATE 7: StatelessSession now uses L2 cache by default! // To bypass (for bulk processing): StatelessSession ss = sf.openStatelessSession(); ss.setCacheMode(CacheMode.IGNORE); // bypass L2 cache
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
