Skip to content

Database connection leak with SseEmitter #12319

@Oduig

Description

@Oduig

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    for: stackoverflowA question that's better suited to stackoverflow.com

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions