Search code examples
javadatabaseh2creation

H2 createTcpServer() does not create server?


after reading the H2 documentation, I wrote this simple application to create a H2 database in a local directory:

public static void main(String[] args) throws SQLException {

    String path = "C:/Temp/H2/";
    File fpath = new File(path);

    fpath.mkdirs();
    FileUtils.recursiveDelete(fpath);

    String dbName = "tata";
    String connection = "jdbc:h2:file:" + path + dbName;

    Server server = Server.createTcpServer(connection);

    server.start();
    server.stop();

}

This program runs fine, but when I check in the target directory, the database is not there... (i am using release 1.3.161)


Solution

  • You need to actually access the database, files are created lazily:

    server.start();
    DriverManager.getConnection(connection);
    server.stop();
    

    Added line in the middle creates tata.h2.db file where expected (tested with 1.3.155).