How do you avoid db connection leaks in WE?

In my first serious WE-project I have the following problem. The database connection (to Postgres of course, but the db doesn’t matter) is a property of the Session object. If the user properly logs out of my app, the connection is properly closed (by explicitly calling db.close) and all is well.
But of course the users don’t do that, they just close the browser tab or go to a different page.

I need a way to detect this, because else I will be left with dangling db connection objects, effectively creating a connection leak. Those connections eventually pile up and finally exceed the maximum connection count on the server.
To my surprise the Session.timeout doesn’t appear to fire when you are not on the page (it does if you are). Nor does Session.close. So how do people deal with this? I’ve seen many websites just telling users off in that scenario (“You didn’t log out the last time, yada yada”), but that’s a silly approach imho.

Hi Max,

totally agree - if the user closes the TAB but leaves the browser open then you get nothing back,

try this

On your web page ( I use the main one) in the close event you check to see if they “logged out” if they didn’t then you do it for them.
The page.close event takes about 20 seconds to fire.

thanks
Damon

Ah, for some reason I forgot about page.close.
Maybe the example “LoginExample” example project (of which I forked of my project) should implement this technique as well. Thanks Damon!

Hmm. I just tried that, there is a problem. The page.close event gets called by a thread called “SessionShutdownThread”. The session appears to be gone already by then (Session is nil). No chance to call Session.mDatabaseConnection.close.

sorry for the delay - had to take the daughter to a party

correct and WOW i had totally forgotten about that - been years

give this a try with a new project

In the session OPEN event open your database as you have normally and use a variable called “dbase”

in the first PAGE create a variable “dbaseABCD” of type

in the open event of your first page
dbaseABCD = session.dbase
You don’t have to reference dbaseABCD anywhere in your app (however you can if you want)

in the page.close event of a window put this

dim r as recordset
r=dbaseABCD.sqlselect(“select * from something”)
break
dbaseABCD.close

r=session.dbase.sqlselect(“select * from something”)

run the app and ensure you are connected to the database then close the tab

when it breaks you will see that r contains the details which means the database is still open

click on STEP
and when it closes the database
the second call to r will return NIL which means it is closed

hope this helps

Damon

Ah, thanks, okay, I will give that I try. Looks a bit like a hack though. I wish there was a cleaner solution. Might look into using a connection pooler.