Closing database connections on windowClose()


I’m running Vaadin with GlassFish + PostgreSQL and have the following scenario. My application has some potentially long-running queries that are sometimes killled by the user (by closing the window) but leave the query still running on the database. I’d like to be able to know about these connections and close them if/when a user closes a window and queries are still bound to a session. The connections are managed by a connection pool at the GlassFish level and pulled as needed as below:

private static Connection getConnection() {
    Connection conn = null;
    try {
        Context initCtx = new InitialContext();
        Context envCtx = (Context) initCtx.lookup("java:comp/env");
        DataSource ds = (DataSource) envCtx.lookup("jdbc/thedb");
        conn = ds.getConnection();
    } catch (SQLException ex) {
    } catch (NamingException ex) {
    return conn;

What are others doing in this scenario? Do you have a running list of connections pulled from a session that are closed when the application is closed? Are there other ways in Vaadin to detect a closed connection that could clean up orphaned queries? Is this sort of thing even a good idea given that the connections are managed at the server and not the application?