0

This class connects to Database via h2db.

   public class DatabaseServer {


    private final String user;
    private final String password;
    private final String database;
    private Connection conn = null;
    private Server webServer = null;
    private Server tcpServer = null;

    public DatabaseServer(String user, String password, String database) {
        this.user = user;
        this.password = password;
        this.database = database;

    }

    public void startServer() throws Exception {
        new Thread(() -> {
            try {
                webServer = Server.createWebServer("-webAllowOthers", "-webPort", "8092");
                webServer.start();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }).start();
        new Thread(() -> {
            try {
                tcpServer = Server.createTcpServer("-tcpAllowOthers", "-tcpPort", "55756");
                tcpServer.start();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }).start();
        //wait until the servers are created
        while (tcpServer == null || webServer == null) {
            Thread.sleep(40);
        }
    }

The error ocorred here:

public Connection getConnection() throws SQLException {
        String url = "jdbc:h2:" + tcpServer.getURL() + "/~/" + database;
        System.out.println("url: " + url);
        conn = DriverManager.getConnection(url, user, password);
        return conn;
    }

I have h2db in Server mode. User and Password are given in the constructor. The code works fine on my Computer, when I export to another computer and run it, gives me this error:

 org.h2.jdbc.JdbcSQLNonTransientConnectionException: Database "C:/Users/tomas/test" not found, either pre-create it or allow remote database creation (not recommended in secure environments) [90149-202]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:678)
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:477)
    at org.h2.message.DbException.get(DbException.java:223)
    at org.h2.message.DbException.get(DbException.java:199)
    at org.h2.engine.Engine.throwNotFound(Engine.java:189)
    at org.h2.engine.Engine.openSession(Engine.java:72)
    at org.h2.engine.Engine.openSession(Engine.java:222)
    at org.h2.engine.Engine.createSession(Engine.java:201)
    at org.h2.server.TcpServerThread.run(TcpServerThread.java:174)
    at java.base/java.lang.Thread.run(Thread.java:832)

1 Answers1

0

Database must be created on the server side before any attempts to use it on client side. A secure password should be used for the user specified during database creation, because this user will have ADMIN privileges in database and these privileges allow access to the system outside of database itself.

Remote database creation is not allowed by H2 any more, because it creates a remote security hole.

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

Evgenij Ryazanov
  • 6,960
  • 2
  • 10
  • 18