New database connection each time user refreshes a webpage?

I just noticed that each time when I click refresh in the browser, this creates a new websession.

In the handler I instantiate a class which establishes a connection to a CubeSQLServer in its constructor.

I guess this is how it is supposed to be and the only way to avoid such consumption of database connections would be to establish one single connection at startup of the webapp?

But then this single connection to the webapp would become the bottleneck, I guess.

So on heavy traffic I risk either non-responsiveness for the user, because there is only one single connection or an inflationary use of connections and memory resources.

How do you handle database connection from a webapp, what would be best practice?

And further it seems like all the database connections established by page refreshes, stay open as long as the browser is not terminated, even though a tab showing the webpage is closed…

I wonder at which moment an unlimited CubeSQLServer would run into a critical situation when connections increase and increase and do not close in a timely manner?

That’s correct. The users have to know that by refreshing they re-start the WebApp (the user has to log in again for example). So this will not happen often.

I don’t know about CubeSQL server, but server databases are made exactly for this. The only thing which could be expensive is the creating of the database object on the client side. You could then create a pool of database objects in App to re-use them. But IMHO this is not necessary.

Mabye you can set a timeout value in the CubeSQLServer?

Don’t use one connection for your whole app. If you use transactions, the sessions will conflict and errors will be VERY hard to track down.

You should be creating a new connection is Session.Open, just for that session and disconnecting on Session.Close.

I solve this by using WebSession.ConfirmMessage.
Set that to a message and a dialog is displayed upon refresh/back button/tab close :slight_smile:

The user can choose to stay or leave your app.

I’m using Xojo 2015r2.4 to compile for Linux 32bit (Ubuntu 14.04 Server)

I added some logging to WebPage1.Close and Session.Close eventhandlers.

Neither of these actions is firing those events:

  • closing the tab which shows the homepage (the webapp runs in an iframe of it)
  • closing the browser (Safari)
  • quitting the browser completely

Therefore my code to close database is never executed.

Has anybody seen this? It looks like a bug to me.

EDIT: Sorry, I was wrong with the above! I just went back to grep the syslog and now I found the corresponding log entries. It just looks like it takes some time until those events fire (or are shown in the log)

Yes, it looks like it takes 5 minutes from closing the browser until the session.close event fires. Then the database connection is closed.

That’s correct. It isn’t possible for the browser to tell your app when it is finished. The only option is a timeout, which is what you’re seeing.

Not exactly pleasant, but that’s just how browsers work.

Thank you all for your input. I’m more clear about this now.