protected void closeAllConnections ( String databasename ) { String sql = "SELECT pg_terminate_backend(pg_stat_activity.pid)\n" + "FROM pg_stat_activity\n" + "WHERE pg_stat_activity.datname = ?\n" + " AND pid <> pg_backend_pid();" ; try ( Connection conn = getDataSource ( ) . getConnection ( ) ) { DatabaseMetaData meta = conn . getMetaData ( ) ; if ( meta . getDatabaseMajorVersion ( ) == 9 && meta . getDatabaseMinorVersion ( ) <= 1 ) { sql = "SELECT pg_terminate_backend(pg_stat_activity.procpid)\n" + "FROM pg_stat_activity\n" + "WHERE pg_stat_activity.datname = ?\n" + " AND procpid <> pg_backend_pid();" ; } } catch ( SQLException ex ) { log . warn ( "Could not get the PostgreSQL version" , ex ) ; } getJdbcTemplate ( ) . queryForRowSet ( sql , databasename ) ; }
Closes all open idle connections . The current data source must have superuser rights .