Search code examples
postgresqljdbcorientdb

OrientDB and PostgreSQL JDBC drivers are clashing (InvocationTargetException): is there an OrientDB JAR with everything except JDBC?


My application uses both OrientDB and PostgreSQL databases for different purposes.

It seems they were able to coexist before, but today my code stopped working. Upon debugging, it seems that the OrientDB driver is attempting to connect to my PostgreSQL database when I'm expecting the PostgreSQL driver to connect instead.

Here is the sequence of events:

  1. OrientDB connection is made (using OrientGraphFactory.setupPool()), transaction is started.
  2. Connection attempt is made on PostgreSQL database, error occurs when trying to create the Connection object.

Here is the segment of code that creates the PostgreSQL connection:

Class.forName("org.postgresql.Driver");
Connection connection = DriverManager.getConnection(
    "jdbc:postgresql://" + [...]);

return connection;

An InvocationTargetException is thrown at the DriverManager.getConnection() line. Here is the stack trace, clearly indicating that the OrientDB driver was the one trying to connect:

Error on opening database 'jdbc:postgresql://[hostname]/[db_name]'
com.orientechnologies.orient.core.exception.ODatabaseException: Error on opening database 'jdbc:postgresql://[hostname]/[db_name]'
    at com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx.<init>(ODatabaseDocumentTx.java:204)
    at com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx.<init>(ODatabaseDocumentTx.java:168)
    at com.orientechnologies.orient.jdbc.OrientJdbcConnection.<init>(OrientJdbcConnection.java:62)
    at com.orientechnologies.orient.jdbc.OrientJdbcDriver.connect(OrientJdbcDriver.java:52)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)

Indeed it seems the JDBC drivers are clashing.

I don't actually need JDBC functionality with OrientDB in this case. However, I can't find the OrientDB JAR that doesn't contain JDBC. The home page lets you download JDBC-all or JDBC-only. Where can I find a JAR with all dependencies bundled into a single JAR, but without JDBC?


Solution

  • This is probably not caused by the drivers themselves but by the DriverManager getting "confused" which driver handles which URL.

    You can bypass the DriverManager by asking the driver directly for a connection:

    Driver drv = new org.postgresql.Driver();
    Properties props = new Properties();
    props.put("username", "foo_user");
    props.put("password", "database_password");
    Connection connection = drv.getConnection("jdbc:postgresql://dbhost/db_name", props);
    

    Unrelated, but: Class.forName("org.postgresql.Driver"); is longer necessary with current Java and driver versions.