What is the proper way to close H2?

不羁的心 提交于 2019-11-28 22:58:29

The documentation says that H2 db connection is closed when the virtual machine exits normally. And that's what it does. The shutdown hook is already there by default, you don't have to do anything. The shutdown hook is a perfectly valid way of closing resources that only need to be closed when quitting.

If you have .lock.db files remaining after shutdown, then the virtual machine didn't exit normally. You wrote that the process does not stop. You have to find the reason for that, because probably that's what also prevents the H2 shutdown hook from executing.

With big databases, closing could take some time. See with debugger (e.g. VisualVM) what threads remain active after you've invoked (Tomcat) shutdown.

There's on more possibility: file permissions are set so that H2 can create the lock files, but cannot delete them. If the OS prevents H2 from deleting its lock files, there's not much H2 could do about it.

You can execute the statement SHUTDOWN and then close the connection.

The SHUTDOWN command will make H2 free all resources related to the connection immediately. That will, for example, allow you to get rid of an embedded H2 database when you redeploy a web application.

Anthony O.

By looking at the DbStarter.contextDestroyed()'s code (thanks to Allan5's answer), here is the code that will work:

connection.createStatement().execute("SHUTDOWN");

So Aaron Digulla's answer was correct (even if not fully "copy/pastable").

Moreover, if you have started an H2 TCP server using server = Server.createTcpServer("-tcpAllowOthers"), you can stop it simply using server.stop().

No, a shutdown hook is simply a thread which runs when the JVM terminates, no matter if by returning from main(), calling System.exit(int) or throwing an exception. Only a JVM crash would avoid it. See Runtime.addShutdownHook(Thread).

Not sure if this is relevant to your situation but have you tried adding a DBStarter listener?

http://www.h2database.com/html/tutorial.html, see the "Using a Servlet Listener to Start and Stop a Database" section.

The link suggests adding the following to web.xml:

<listener>
    <listener-class>org.h2.server.web.DbStarter</listener-class>
</listener>

Please see discussion here (admittedly from 2008 so may be out of date) - apparently the fix applies to both embedded and non-embedded instances: http://groups.google.com/group/h2-database/browse_thread/thread/eb609d58c96f4317

Alternatively how are you using the connections? Are you sure you are cleaning up the connections properly?

I was having issues before, in my case I was using the connection with a JPA EntityManager and I forgot to close the EntityManager instance after use, which resulted in some issues:

@PersistenceUnit(unitName="myEm")
private EntityManagerFactory emf;

public void doStuff() {
    EntityManager em = emf.createEntityManager();
    ...
    em.close(); // forgot this line
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!