Tweaks for compatibility with MSSQL:
- sqlserv is unavailable on shutdown, so we write and close the session early. Rather than limit it to only MSSQL, it's done on all DB types in case they have the same issue
- Correct use of removeByFilter function