What is the proper way to close H2?

匿名 (未验证) 提交于 2019-12-03 01:47:02

问题:

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 when I shutdown tomcat and the process does not stop.
I use Tomcat's connection pooling and the url to the database is:
url="jdbc:h2:file:/opt/myOrg/tomcat/webapps/MyApplication/db/myDatabase;SCHEMA=myschema"

From the doc close H2:

Usually, a database is closed when the last connection to it is closed.... By default, a database is closed when the last connection is closed. However, if it is never closed, the database is closed when the virtual machine exits normally, using a shutdown hook

I can not understand if I am doing something wrong.
Should I be forcing the database to close via a command? Is this the meaning of shutdown hook?
What am I doing wrong here?

Note:
I can not find in Google an example of how to close H2 properly (besides the statement that it closes automatically on last connection shutdown). Should I call SHUTDOWN myself? Is this the proper approach?
I already see votes to close the question but there has not been a reason or link on an example of what I am investigating

UPDATE:
After Joonas Pulakka answer some extra info:

From the javacore I got using a kill -3 I see the threads:

"H2 Log Writer MYAPPLICATION" J9VMThread:0x08DC6F00, j9thread_t:0x08C9B790, java/lang/Thread:0xE7206CC8, state:CW, prio=5 3XMTHREADINFO1 (native thread ID:0xA32, native priority:0x5, native policy:UNKNOWN) 3XMTHREADINFO2
(native stack address range from:0xE5E26000, to:0xE5E67000, size:0x41000) 3XMTHREADINFO3 Java callstack:
4XESTACKTRACE at java/lang/Object.wait(Native Method)
4XESTACKTRACE at java/lang/Object.wait(Object.java:196(Compiled Code)) 4XESTACKTRACE at org/h2/store/WriterThread.run(WriterThread.java:102)
4XESTACKTRACE at java/lang/Thread.run(Thread.java:736)

3XMTHREADINFO "pool-8-thread-1" J9VMThread:0x087C0200, j9thread_t:0x0840566C, java/lang/Thread:0xE79BFC80, state:P, prio=5
3XMTHREADINFO1 (native thread ID:0xE1A, native priority:0x5, native policy:UNKNOWN) 3XMTHREADINFO2
(native stack address range from:0xE5F69000, to:0xE5FAA000, size:0x41000) 3XMTHREADINFO3 Java callstack:
4XESTACKTRACE at sun/misc/Unsafe.park(Native Method)
4XESTACKTRACE at java/util/concurrent/locks/LockSupport.park(LockSupport.java:184(Compiled Code)) 4XESTACKTRACE at java/util/concurrent/locks/AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:1998(Compiled Code)) 4XESTACKTRACE at java/util/concurrent/LinkedBlockingQueue.take(LinkedBlockingQueue.java:413(Compiled Code)) 4XESTACKTRACE at java/util/concurrent/ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:958(Compiled Code)) 4XESTACKTRACE at java/util/concurrent/ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918) 4XESTACKTRACE at java/lang/Thread.run(Thread.java:736)

3XMTHREADINFO "H2 File Lock Watchdog opt/myOrg/tomcat/webapps/MyApplication/db/myDatabase.lock.db" J9VMThread:0x08DC6900, j9thread_t:0x08C9BA24, ja
va/lang/Thread:0xE71E9018, state:CW, prio=9 3XMTHREADINFO1
(native thread ID:0xA30, native priority:0x9, native policy:UNKNOWN)
3XMTHREADINFO2 (native stack address range from:0xE5DBA000, to:0xE5DFB000, size:0x41000) 3XMTHREADINFO3 Java callstack: 4XESTACKTRACE at java/lang/Thread.sleep(Native Method) 4XESTACKTRACE
at java/lang/Thread.sleep(Thread.java:851(Compiled Code))
4XESTACKTRACE at org/h2/store/FileLock.run(FileLock.java:490) 4XESTACKTRACE
at java/lang/Thread.run(Thread.java:736)

3XMTHREADINFO "FileWatchdog" J9VMThread:0x087C0800, j9thread_t:0x08C9B4FC, java/lang/Thread:0xE715D878, state:CW, prio=5
3XMTHREADINFO1 (native thread ID:0xA2C, native priority:0x5, native policy:UNKNOWN) 3XMTHREADINFO2
(native stack address range from:0xE5E67000, to:0xE5EA8000, size:0x41000) 3XMTHREADINFO3 Java callstack:
4XESTACKTRACE at java/lang/Thread.sleep(Native Method) 4XESTACKTRACE at java/lang/Thread.sleep(Thread.java:851(Compiled Code)) 4XESTACKTRACE at org/apache/log4j/helpers/FileWatchdog.run(FileWatchdog.java:104)

回答1:

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.



回答2:

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



回答3:

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



回答4:

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:

org.h2.server.web.DbStarter

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 }


回答5:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!