Understanding OSIV in Spring Boot — Convenience or Hidden Bottleneck?
By Ercan - 29/10/2025
Spring Boot enables Open Session in View (OSIV) by default, allowing lazy-loaded entities to be accessed even after a transaction has ended.
While this behavior simplifies development and prevents common exceptions, it can quietly turn into a performance bottleneck in production.
In this article, we’ll explore:
- How OSIV works under the hood
- Why it’s helpful for developers
- What risks it introduces in real-world systems
- How to measure its performance impact
TL;DR: If you want to skip the explanation and dive straight into the code, check out the full implementation on GitHub:
https://github.com/ercansormaz/spring-osiv-demo
What Is OSIV?
When OSIV is enabled, Spring keeps the Hibernate session (or JPA EntityManager) open from the start of the request until the response is rendered.
That means even outside of your @Transactional service layer, you can still access lazy relationships — Hibernate will transparently hit the database to fetch them.
This can be convenient during development, but it extends the lifetime of database connections unnecessarily.
How OSIV Affects Performance
In production, every HTTP request holds a database connection open for its entire lifecycle, even after all business logic is completed.
When the connection pool is small, long-running requests can block faster ones, because connections remain occupied until view rendering finishes.
Example Scenario
- Connection pool size: 10
/longendpoint simulates a 5-second operation/shortendpoint simulates a quick query (100ms)
If several /long requests start simultaneously, they fill the pool.
When /short requests arrive, they must wait for a free connection — even though they’re trivial operations.
This is the hidden cost of OSIV: latency introduced by connection starvation.
Observing the Impact
In our test setup, we simulate both cases using ab:
Scenario 1 — Single Endpoint Run
ab -n 50 -c 50 -r http://localhost:8080/api/short
→ All connections are short-lived, system remains responsive.
Scenario 2 — Parallel Endpoint Run
Start /long requests:
ab -n 50 -c 50 -r http://localhost:8080/api/long
After 1–2 seconds, start /short requests in another terminal:
ab -n 50 -c 50 -r http://localhost:8080/api/short
Now, you’ll observe increased latency for /short, even though its logic hasn’t changed.
The pool is busy with long-running transactions — exactly what happens when OSIV keeps connections open too long.
Disabling OSIV in Production
You can disable it globally in your application.yml:
spring:
jpa:
open-in-view: false
Once disabled, the Hibernate session closes immediately after the transaction layer finishes.
This frees up connections faster and improves overall throughput.
✅ Benefits:
- Faster connection recycling
- Reduced risk of connection pool exhaustion
- Clearer transaction boundaries
⚠️ Trade-off:
Lazy relationships can no longer be accessed outside a transaction.
OSIV and Lazy Loading: Development vs Production
Lazy loading often appears to “just work” in development — thanks to OSIV.
When disabled, accessing lazy fields (like entity.getChildren()) outside a transaction causes:
LazyInitializationException: could not initialize proxy - no Session
This exception is not a bug; it’s a signal that your architecture needs a proper data-fetching strategy:
- Use DTOs at the service layer
- Fetch required relations eagerly with
JOIN FETCHor@EntityGraph - Avoid accessing JPA entities in web controllers
So, while OSIV + Lazy Loading feels convenient during early development, it masks architectural issues and hurts scalability later on.
Conclusion
OSIV is a double-edged sword.
It simplifies development but silently extends database session lifetimes — often beyond what’s healthy for production workloads.
In short:
Keep OSIV enabled in local environments for convenience, but disable it in production for performance and clarity.
By understanding how OSIV interacts with connection pools and lazy loading, you can design a system that’s both developer-friendly and production-ready.
👉 You can explore the full source code on GitHub:
https://github.com/ercansormaz/spring-osiv-demo
Tags: spring boot, osiv
