ConnectionHandler problem for cache

User 9d2c03e26a

11-06-2010 04:19:34

As far as I found, the jchem cache creation will ignore the connection object even if a valid java.sql.Connection object is given:


ConnectionHandler ch = new ConnectionHandler();
ch.setConnection(connection);


The stacktrace is like this:


java.sql.SQLException: The url cannot be null
    at java.sql.DriverManager.getConnection(DriverManager.java:502)
    at java.sql.DriverManager.getConnection(DriverManager.java:171)
    at chemaxon.jchem.db.CacheRegistrationUtil.registerCache(CacheRegistrationUtil.java:77)
    at chemaxon.jchem.db.CacheRegistrationUtil.registerPermanentCache(CacheRegistrationUtil.java:61)


We need to exlicitly set the connection paramters again and call connect() on the object:


            ch.setDriver(driver);
            ch.setUrl(url);
            ch.setLoginName(login);
            ch.setPassword(password);
            ch.setRememberPassword(rememberPassword);
            try {
                ch.connect();
            } catch (Throwable t) {
                t.printStacktrace();
            }


However, for other JChem operations like get structure table names, using UpdateHandler, an active connection will be enough to do the job.


Is this behavior designed to be like this (on purpose)? or did I miss something important?

ChemAxon a9ded07333

11-06-2010 08:42:43

Hi Urs,


It seems that your connection is used in transaction (or at least the autoCommit property of connection is set to false). Since con.getAutocommit() returns false in your code, the registration process
tries to connect to the database by creating a new connection using the parameters (url, login, ...) of the ConnectionHandler, but ConnectionHandler.setConnection() does not set these parameters (and what is more, it also clears them if they are set) so they are null!


We suggest to register outside of a transaction (see http://www.chemaxon.com/jchem/doc/dev/java/intro/index.html#cachereg , "Recommendations").


Your workaround (setting the connection parameters after setting the connection) is also a good solution for the problem, except that you don't have to call connect() in this case since - an opened, connected - connection has been already set in the ConnectionHandler.


Regards,
Tamás