Instead of adding BerkeleyEnvironment objects permanently to the g_dbenvs map, use reference counted shared pointers and remove map entries when the last BerkeleyEnvironment reference goes out of scope.
Backport of core PR11911 and PR15322:
https://github.com/bitcoin/bitcoin/pull/11911/files
https://github.com/bitcoin/bitcoin/pull/15322/files
This should fix ASAN and TSAN.
Depends on D4888.