0

I use Primefaces / JSF2. Due to a special case we use jdbc connection instead of connection pool. Every logged in user will hold one and only DB connection at a time. If a logged in user close the browser instead of a proper logout, his DB connection get held till the idle timeout (15 mins) and he cannot login again immediately within this 15 mins timeout.

Is there any way to close the user DB connection, if he closes the browser instead of logout?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Jay
  • 9,189
  • 12
  • 56
  • 96
  • You can create a jdbc session per browser request instead of per browser session. That way you are always closing it when each request finishes. – Aritz Sep 02 '13 at 14:46

1 Answers1

4

No, there is no way. At least, no reliable way. You could perhaps set the session timeout to 1 minute, keep it alive with ajax polling and use HttpSessionListener#sessionDestroyed() to close the connection. But still, the whole approach is very brittle and prone to failure.

You should always acquire and close the DB resources in the shortest possible scope within the very same try-finally block as where the SQL query/queries are fired, no excuses. A Java EE web application is really not comparable to a plain Java desktop application where the enduser can just restart the buggy application itself if it crashes due to DB resource leaking.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555