(#3946) Add 'finalize' method to provider classes

This patch allows provider classes to implement a ::finalize method,
which will be called during the transaction after all resources have
been processed. The purpose is to have a place to peform cleanup actions
for, e.g. connections opened and shared between provider instances.
This cleanup should be performed whether or not resources have changed,
and even if resources have failures, which precludes the use of existing
cleanup methods (like #flush).