Search code examples
xodus

What is the proper way of safely shutdown the Xodus environment?


I need a way shutting down the Xodus environment so that

  • It waits for all the writing transactions in all threads to finish (or be aborted after a timeout as an option).
  • It blocks starting new transactions (or throws an exception as an option).
  • Safely closes the environment.

So far we tried something like this

if (env.isOpen()) {            
    env.clear();
    env.close();
}

but I am not sure that it does exactly the right thing, I still get exceptions thrown from env.close() from time to time. So what is the proper way to do that?


Solution

  • First of all, note that Environment#clear() just clears all the data in your environment.

    Minor: you don't have to check whether your environment is open before closing it.

    If you don't care much of the application state then you can set the exodus.env.closeForcedly option on Environment creation:

    Environment env = Environments.newInstance("db path", new EnvironmentConfig().setEnvCloseForcedly(true));
    

    In that case, close() method reports in logs the number of not finished at the moment transactions and does close the environment anyway.