<!-- This issue tracker is only for technical issues related to Bitcoin Core.

General bitcoin questions and/or support requests are best directed to the Bitcoin StackExchange at https://bitcoin.stackexchange.com.

For reporting security issues, please read instructions at https://bitcoincore.org/en/contact/.

If the node is "stuck" during sync or giving "block checksum mismatch" errors, please ensure your hardware is stable by running memtest and observe CPU temperature with a load-test tool such as linpack before creating an issue! -->

<!-- Describe the issue -->
<!--- What behavior did you expect? -->

<!--- What was the actual behavior (provide screenshots if the issue is GUI-related)? -->

<!--- How reliably can you reproduce the issue, what are the steps to do so? -->

<!-- What version of Bitcoin Core are you using, where did you get it (website, self-compiled, etc)? -->

<!-- What type of machine are you observing the error on (OS/CPU and disk type)? -->

<!-- Any extra information that might be useful in the debugging process. -->
<!--- This is normally the contents of a `debug.log` or `config.log` file. Raw text or a link to a pastebin type site are preferred. -->

```
src/bitcoind -daemon -regtest
src/bitcoin-cli -regtest stop
cp ~/.bitcoin/wallet/wallet.dat ~/.bitcoin/wallet/wallet2.dat
src/bitcoind -daemon -regtest
src/bitcoin-cli -regtest loadwallet wallet2.dat # The first time it would fail to load wallet
src/bitcoin-cli -regtest loadwallet wallet2.dat # The second time it would succeed to load wallet, but this would cause error because of duplicate fileid
```
It would flush `wallet.dat` and close `Db*` in` mapDb['wallet.dat'].second`, so `mapDb['wallet.dat'].second` would be `nullptr`. Then there is no available `Db*` to detect if the wallet fileid is duplicated.

I'm not sure if the issue is related to #14163, but I found this while debugging it.

https://github.com/bitcoin/bitcoin/blob/920c090f63f4990bf0f3b3d1a6d3d8a8bcd14ba0/src/wallet/db.cpp#L32-L53
{
        return MakeUnique<BerkeleyDatabase>("", true );
    }
    /** Rewrite the entire database on disk, with the exception of key pszSkip if non-zero
     */
    bool Rewrite(const char* pszSkip=nullptr);
    /** Back up the entire database to a file.
     */
    bool Backup(const std::string& strDest);
    /** Make sure all changes are flushed to disk.
     */
    void Flush(bool shutdown);
    void IncrementUpdateCounter();
    void ReloadDbEnv();
    std::atomic<unsigned int> nUpdateCounter;
    unsigned int nLastSeen;
    unsigned int nLastFlushed;
    int64_t nLastWalletUpdate;
private:
    
    BerkeleyEnvironment *env;
    std::string strFile;
    /** Return whether this database handle is a dummy for testing.
     * Only to be used at a low level, application should ideally not care
     * about this.
     */
    bool IsDummy() { return env == nullptr; }
};
class BerkeleyBatch