question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Database connection leak with SseEmitter

See original GitHub issue

I 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

  1. Open the minimal example (link below)
  2. Run docker-compose up -d --build
  3. Start the application
  4. Open http://localhost:8088 in six tabs
  5. The sixth tab will not load because all connections are in use.

Resources

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Divine1commented, Nov 26, 2021

@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

0reactions
bclozelcommented, Nov 26, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found