JDBCConnectionPool stayed open. How can I handle it?

I do not understand, I need some help on how to handle it.
There is a very simple (for example) site with a Vaadin tablequery and table.

//Open a connection:
JDBCConnectionPool pool = new SimpleJDBCConnectionPool(
“org.hsqldb.jdbc.JDBCDriver”,
“jdbc:hsqldb:mem:sqlcontainer”, “SA”, “”, 2, 5);

TableQuery tq = new TableQuery(“tablename”, pool);
container = new SQLContainer(tq);
Table table = new Table();
table.setContainerDataSource(container);
addComponent(table);

When the visitor open the site, it open a new connection. If come a new visitor, it open a new connection too. When the visitors type a new url (he/she leave our site), close the tab or close the browser, the connections stayed open. More visitors, more remained opened connections.

I tried the:

JavaScript.getCurrent().addFunction(“catchClose”, new JavaScriptFunction() {
@Override
public void call(JsonArray arguments) throws JSONException {
closeConn();
}
});
Page.getCurrent().getJavaScript().execute(“window.onbeforeunload = function () { catchClose(); };”);

but it can’t run correctly in different browsers.

It is a simple sample. In the real app, the users connected to different databases, and stay a lot of connections open.

How can I handle this situation correctly?

Do you create a connection pool each time you create the table, or is the connection pool some sort of singelton object in your app?

Because each user connected to a separate database for each entry opens a connection. But I thhink these is not important, because the problem there is with one connection too.

Well, I don’t really know much about SimpleJDBCConnectionPool, but the point of a connection pool is that it keeps the connections around, because it’s cheaper to reuse them than to create a new one each time you need one (among other things).

So in most cases connection pools have singleton character, and are shared between components. TableQuery is intended to be shared by containers as well, although I’m not sure (I’ve never used them).

If you create one for each UI (or View, can’t really say what exactly you’re doing), you need to clean up at some point. Although I’d try to find a better way to handle this, if there’s too many concurrent users you’ll run into too many open connections with this design, whether you clean up or not.

You could create a factory for the TableQuery for example, make that factory a singleton, and in that factory allocate the connection pool only once.