H2 createTcpServer() does not create server?

2019-06-02 09:01发布

问题:

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)

回答1:

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).