Search code examples
javaconnectionh2

Error when trying to connect to H2 database


Did any of you had this below error when trying to connect to H2? The release I'm using is 1.4.199, on windows, with TAFJFuctions.

First I run this command:
java -server -cp h2-1.4.199.jar;C:\Users\...\H2\TAFJFunctions.jar org.h2.tools.Server -web -tcp -tcpPort 9092 -tcpAllowOthers -baseDir C:\Users\...\H2\bin

and I've got connected with TCP server running and Web Console server running as well. Then I've run this command:
SET H2URL=jdbc:h2:tcp://locaLhost/t24db;DB_CLOSE_ON_EXIT=FALSE;MODE=Oracle;TRACE_LEVEL_FILE=0;TRACE_LEVEL_SYSTEM_OUT=0;FILE_LOCK=NO;IFEXISTS=TRUE;CACHE_SIZE=8192;MVCC=TRUE;LOCK_TIMEOUT=60000

And then this command:
java -server -cp h2-1.4.199.jar;C:\Users\...\H2\TAFJFunctions.jar org.h2.tools.Shell -url %H2URL% -driver org.h2.Driver -user t24 -password t24

And here I've got the below error:

"Exception in thread "main" org.h2.jdbc.JdbcSQLNonTransientConnectionException: Database "C:/Users/.../H2/bin/t24db" not found, and IFEXISTS=true, so we cant auto-create it [90146-199]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:617)
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:427)
    at org.h2.message.DbException.get(DbException.java:205)
    at org.h2.message.DbException.get(DbException.java:181)
    at org.h2.engine.Engine.openSession(Engine.java:67)
    at org.h2.engine.Engine.openSession(Engine.java:201)
    at org.h2.engine.Engine.createSessionAndValidate(Engine.java:178)
    at org.h2.engine.Engine.createSession(Engine.java:161)
    at org.h2.server.TcpServerThread.run(TcpServerThread.java:160)
    at java.lang.Thread.run(Unknown Source)
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:617)
    at org.h2.engine.SessionRemote.done(SessionRemote.java:607)
    at org.h2.engine.SessionRemote.initTransfer(SessionRemote.java:143)
    at org.h2.engine.SessionRemote.connectServer(SessionRemote.java:431)
    at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:317)
    at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:169)
    at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:148)
    at org.h2.Driver.connect(Driver.java:69)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at org.h2.tools.Shell.runTool(Shell.java:148)
    at org.h2.tools.Shell.main(Shell.java:81)"

Any idea how to get this working?

Thank you, Carina


Solution

  • With IFEXISTS=TRUE the problem is oblivious, but without this parameter or with attempt to set it to FALSE situation is not going to be changed.

    All recent versions of H2 database don't allow remote (including local TCP connections) database creation by default any more due to security reasons. When it is allowed, everyone who can connect to your port can create a new database, get ADMIN privileges in it and therefore get the same access to your system as your JVM and your user account allows.

    Unfortunately, H2 1.4.199 throws a confusing error message, it was improved only in 1.4.200 and in this version (when IFEXISTS=TRUE wasn't used) the error message is “Database … not found, either pre-create it or allow remote database creation (not recommended in secure environments)”.

    If you use a TCP (or Pg/ODBC) server you need to create your databases in some other way before you try to connect to them with networked protocol.

    For example, you can open the JDBC connection (DriverManager.getConnection()) with embedded URL and close it immediately.

    You can replace org.h2.tools.Server with org.h2.tools.Console and you'll see another http connection URL (if you start it from the command line), something like http://127.0.0.2:8082?key=12c58e1c5f9ce1ae88a2921f74e7655ed91a80746730cc6bfa8d4bbb464f69ee; with this URL you will be able to create the database from the web interface (only if remove web interface is not enabled).

    You can also add the -tool parameter (only to Console, the Server doesn't support it) to get H2 Console icon in the system tray, its context menu has a command to create a new database and this icon can also open the web interface with the same security key. Don't share this key with anyone. It will be different on each restart, however.

    You can also use the command-line Shell tool.

    https://h2database.com/html/tutorial.html#creating_new_databases

    In the worst case you can enable the remove database creation, but it is not really secure even with local connections and it is completely insecure when remote connections are enabled as in your case. You will have a well-known remote security hole in your system, I don't think that it is your intention.