Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run H2 database in server mode?

Tags:

java

database

h2

I need to start H2 database in server mode from my application. Having tried the following code:

server = Server.createTcpServer().start(); 

Here is the properties for the connection:

javabase.jdbc.url = jdbc:h2:tcp://localhost:9092/nio:~/source/db/database/db;AUTO_SERVER=TRUE javabase.jdbc.driver = org.h2.Driver javabase.jdbc.username = sa javabase.jdbc.password = 

When I run the program, I got the following error:

client.db.exception.DAOException: org.h2.jdbc.JdbcSQLException: Database may be already in use: "Locked by another process". Possible solutions: close all other connection(s); use the server mode [90020-164]     at org.h2.message.DbException.getJdbcSQLException(DbException.java:329)     at org.h2.message.DbException.get(DbException.java:169)     at org.h2.message.DbException.get(DbException.java:146)     at org.h2.store.FileLock.getExceptionAlreadyInUse(FileLock.java:439)     at org.h2.store.FileLock.lockFile(FileLock.java:336)     at org.h2.store.FileLock.lock(FileLock.java:128)     at org.h2.engine.Database.open(Database.java:542)     at org.h2.engine.Database.openDatabase(Database.java:222)     at org.h2.engine.Database.<init>(Database.java:217)     at org.h2.engine.Engine.openSession(Engine.java:56)     at org.h2.engine.Engine.openSession(Engine.java:159)     at org.h2.engine.Engine.createSessionAndValidate(Engine.java:138)     at org.h2.engine.Engine.createSession(Engine.java:121)     at org.h2.server.TcpServerThread.run(TcpServerThread.java:133)     at java.lang.Thread.run(Thread.java:680) 
like image 488
Feras Odeh Avatar asked Feb 16 '12 19:02

Feras Odeh


People also ask

How do I access my H2 database?

Access the H2 Console You can access the console at the following URL: http://localhost:8080/h2-console/. You need to enter the JDBC URL, and credentials. To access the test database that the greeter quickstart uses, enter these details: JDBC URL: jdbc:h2:mem:greeter-quickstart;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1.

What is H2 server?

H2 is an open-source lightweight Java database. It can be embedded in Java applications or run in the client-server mode. Mainly, H2 database can be configured to run as inmemory database, which means that data will not persist on the disk.


1 Answers

As the exception message says, "Database may be already in use". You need to close all other connection(s), to make sure the database is not open in another process concurrently.

By the way, don't use AUTO_SERVER=TRUE and the server mode at the same time. See the documentation for the automatic mixed mode. Use either one.

I guess you are a bit confused about the different connection modes. I suggest to read the documentation about the connection modes, to make sure you understand it.

like image 172
Thomas Mueller Avatar answered Sep 20 '22 02:09

Thomas Mueller