Database connection leak with SseEmitter
See original GitHub issueI have a problem with a database connection pool in Spring Boot 2, where connections are not returned to the pool even though they are neatly wrapped in @Transactional
.
Upon opening the home page, I run a query and then open an SSE stream:
@GetMapping("")
public SseEmitter home() {
derpService.derp();
return obs.subscribeSse();
}
The derp()
call looks like this:
@Transactional
void derp() {
derpRepository.derp();
}
Which leads to:
@Query(value = "SELECT 'derp'", nativeQuery = true)
void derp();
As long as the SSE stream is open, the connection from the derp()
call is not released. This goes against my intuition, because I assumed @Transactional
would return the connection as soon as the flow exits the transaction scope.
Is this a bug in Spring?
Steps to reproduce
- Open the minimal example (link below)
- Run
docker-compose up -d --build
- Start the application
- Open
http://localhost:8088
in six tabs - The sixth tab will not load because all connections are in use.
Resources
- Minimal example on https://github.com/Oduig/connectionleak.
- SO question on https://stackoverflow.com/questions/49071931/database-connection-leak-in-spring-boot-with-sseemitter
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:9 (3 by maintainers)
Top Results From Across the Web
Database connection leak in Spring Boot with SseEmitter
It seems that the associated connection was being held for as long as the SseEmitter was alive, quickly extinguishing the db connection pool....
Read more >Spring Boot Observability: Discovering a Database ...
In this post, we are going to talk about Gatling as a loading test tool, create a database connection leak, use Grafana to...
Read more >Connection leak using Hibernate SessionFactory in Spring ...
Coding example for the question Connection leak using Hibernate SessionFactory in Spring ... Database connection leak in Spring Boot with SseEmitter ...
Read more >Detecting and Resolving Database Connection Leaks with ...
Take a look at this tutorial that demonstrates how you can find and remove connection leaks between your application and database in Java ......
Read more >Server-Sent Events Using Spring - Morioh
You can send unidirectional events using the SseEmitter class in Spring. ... Support any third-party database connection pool, such as DBCP, C3P0, BoneCP, ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@bclozel thank you, i think its the same problem not different
spring.jpa.open-in-view = false
adding above property fixed the issue. it detaches the dbconnectionSession from the incoming session.
references
If you’re starting a transaction, a streaming query or if the session is configured to stay opened with the view, you are effectively binding that database resource to the persistent connection. If you believe you’ve found a different problem, please create a new issue with a sample, minimal project that reproduces the behavior.