Steps to reproduce:
- Set MySQL wait_timeout to a short value: SET @@GLOBAL.wait_timeout=10;
- Start SonarQube server
- Send a first notification (update an issue) => session is opened and attached to NotificationService thread
- Wait for 10 seconds until idle connection is closed by MySQL server
- Try to send another notification => NotificationService try to reuse same previous session but underlying connection is closed => error
In real life it occurs when the NotificationManager didn't send email in a period exceeding MySQL wait_timeout.
There is an easy fix that is to always close session after each execution of the NotificationService. This way during next "wake-up" of the NotificationService a fresh new session will be created and a new connection requested from the pool.