As far as I know the SQL is perfect. That sub/function is called when the user session times out, which is 20 minutes by default on my server. I believe it is also called when the session is abandoned.
Donate to UBBDev today to help aid in Operational, Server and Script Maintenance, and Development costs.
Please also see our parent organization VNC Web Services if you're in the need of a new UBB.threads Install or Upgrade, Site/Server Migrations, or Security and Coding Services.