Search code examples
javaxodus

Is it safe not to close an Environment or EntityStore?


I want to ask if it's safe not to close an environment?

    final PersistentEntityStore entityStore = manager.getPersistentEntityStore(xodusRoot, instance);
    final List<User> users = new LinkedList<>();
    try {

    } finally {
       //entityStore.close
    }

The reason behind not closing the environment is that this example code here is used in a Servlet environment in which we implemented a sort of Sigleton lookup table (map) to hold the Environments and EntiyStores and if we close it the next HTTP POST request will get a "Environment already closed" error, thus we don't close it.

And the reason we implemented the Lookup table (map) here is to prevent the servlet requests from getting Database lock issues, especially on multiple concurrent requests.

Is this safe to do? Or is this even the correct approach?


Solution

  • Generally, it's safe not to close an Environment or EntityStore. In most cases not closed database will be opened without data loss if JVM exits. In worst case, the database would recover to most recent valid snapshot like it does after system/hardware failures. However not closed database can consume some additional physical space (GC would be affected) which results in a worse performance. You can close all Environments/EntityStores in the lookup table on destroying your servlet.