I wrote two Java web servlet apps (oauth consumer app, and oauth server app) both of which rely on H2 as database engine. I want H2 to be run in embedded mode instead of server mode. According to the official document, following statement should be added to web.xml
<listener> <listener-class>org.h2.server.web.DbStarter</listener-class> </listener>
This class has methods that listen to the servlet context event (context initialization, context destroyed).
When I deploy just one of the two web apps to tomcat, it works fine. When I deploy both of them, following exception is thrown
Exception in thread "main" org.h2.jdbc.JdbcSQLException: Database may be already in use: Locked by another process. Possible solutions: close all other connecti on(s); use the server mode [90020-63]
I am 100% sure that those two apps use different databases. To make myself 200% sure that my code does not create or use additional databases unintentionaly, I rechecked my code many many times. But my code looks just fine. So I doubted it may be because of the H2 database engine.
Then I used command 'lsof' to list opened files by tomcat java process. Surprisingly, a database named 'test' is created under my home directory.
I reread the document http://www.h2database.com/html/tutorial.html#web_applications, and I got what I missed before.
For details on how to access the database, see the file DbStarter.java. By default the DbStarter listener opens an embedded connection using the database URL 'jdbc:h2:~/test', user name 'sa', and password 'sa'.
Code of class DbStarter can be accessed at http://code.google.com/p/h2database/source/browse/trunk/h2/src/main/org/h2/server/web/DbStarter.java.
I am just curious about why a default database is created by class DbStarter.
Solutions
(1) remove the listener declaration in web.xml
In other words, remove <listener> <listener-class>org.h2.server.web.DbStarter</listener-class> </listener>
(2) change url of created database.
<context-param> <param-name>db.url</param-name> <param-value>jdbc:h2:your_new_db_url</param-value> </context-param>
No comments:
Post a Comment