Backport of sharing fixes to stable5

Backport of #4825 (without the Doctrine commits).
Backport of #4856

The Doctrine commits are not required because MDB2 already sends the Oracle queries for us. See https://github.com/owncloud/3rdparty/blob/stable5/MDB2/Driver/oci8.php#L408