What is the proper way to close H2?

前端 未结 5 1944
我在风中等你
我在风中等你 2020-12-15 04:25

This is related to this post.
I think I am having problem with H2 meaning that it does not close properly.
I suspect this since I see myDB.lock.db

相关标签:
5条回答
  • 2020-12-15 04:58

    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.

    0 讨论(0)
  • 2020-12-15 04:59

    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.

    0 讨论(0)
  • 2020-12-15 05:01

    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().

    0 讨论(0)
  • 2020-12-15 05:10

    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).

    0 讨论(0)
  • 2020-12-15 05:16

    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
    }
    
    0 讨论(0)
提交回复
热议问题