Search code examples
javadatabaseh2symmetricds

Connect SymmetricDS to an Existing H2 Databse File


I've a H2 database file which I need to replicate using SymmetricDS.

I can access the database from web console in embedded mode using this url:

jdbc:h2:file:E:/Folder/database;AUTO_SERVER=TRUE;IFEXISTS=TRUE;

But using the same url in node properties file of symmtericds throws following error:

ERROR [server-000] [AbstractSymmetricEngine] [symmetric-engine-startup-0] Could not get a connection to the database: Cannot create PoolableConnectionFactory (IO Exception: "E:/Folder/database outside D:/symmetric-server-3.7.26/tmp/h2" [90028-182]).  Waiting for 10 seconds before trying to connect to the database again.

Though if the following url is used in the node properties file of symmetricds, everything works fine but the database is created in symmetric-server-3.7.26/tmp/h2 directory.

jdbc:h2:file:database;AUTO_SERVER=TRUE;

I've not had any luck in solving this issue in past couple of days.

Any kind of help will be greatly appreciated.

Going through H2 documentation I found out that the this behaviour is due to -baseDir option: http://www.h2database.com/html/advanced.html#remote_access .

But how is this option being set automatically and how to disable it..?

Doing some extensive research on the issue, root cause turns out to be the static block of AbstractCommandLauncher class of SymmetricDS:

static {
    String symHome = System.getenv("SYM_HOME");
    if (symHome == null) {
        symHome = ".";
    }
    System.setProperty("log4j.sym.home", symHome);
    if (isBlank(System.getProperty("h2.baseDir"))) {
       System.setProperty("h2.baseDir", symHome + "/tmp/h2");
    }
    DEFAULT_SERVER_PROPERTIES = System.getProperty(SystemConstants.SYSPROP_SERVER_PROPERTIES_PATH, symHome + "/conf/symmetric-server.properties");
    log = LoggerFactory.getLogger(AbstractCommandLauncher.class);
    initFromServerProperties();
}

And as it turns out, source code of SymmetricDS will have to be modified to resolve it.


Solution

  • This behaviour can not be disabled altogether but overriding it is possible by setting -Dh2.baseDir while running SymmetricDS. The original answer is here