Friday, February 11, 2011

How can I expose a C ODBC connection to a JVM using JNI?

I'm embedding a JRE in an existing C application using the invocation API, and I'd like to be able to use JDBC to work with the database in that code. This application is a transaction processing application, and the database transaction is managed by code in the C portion of the application, and the java code must run within that transaction. This means that I can't open a new connection, I must re-use the existing one.

So, is there a way to provide JDBC access to an existing ODBC connection handle when setting up the JRE? Some JDBC-ODBC bridge, perhaps, but unlike the existing driver by that name, one that can be set up to use an existing connection and transaction.

My other options, as I see them, are as follows:

  • Provide java equivalents for every C operation that is possible in the application (this is not desirable for a great many reasons -- we have a great many methods and duplicating them is a pain in the ass.

  • Write my own JDBC driver that wraps the ODBC connection with JNI. Sure, it'd be a fun weekend (month) project, but I expect to need something done faster than that.

Help me, Stack-Overflow, you're my only hope!

  • Sun provides a JDBC-ODBC bridge in the JDK.

    EDIT: Rereading sounds like you already know about it and don't want to use it.

    Chris R : The JDBC-ODBC brige provides a way to create a new connection, not re-use an existing one; I noted this in the question itself.
    From Matt
  • Don't know whether this'll work, but... I had a quick look at the decompiled source code of Sun's JDBC-ODBC bridge. Seems like you could subclass the JdbcOdbcConnection so that it initializes itself with a known connection handle and an already-opened state. This assumes that the connection handles on the Java side are actual ODBC connection handles or pointers to connection objects, and that ODBC libraries used by the JDBC-ODBC bridge and your code are compatible in the sense that they can share connection handles.

    You need to check whether Sun's license allows developers to perform such trickery though.

    Chris R : Alexander, this is what I ended up doing; in fact, as I asked the question I was already pursuing that approach, and it turns out to work very well. Thanks for the independent verification!
    From Alexander

0 comments:

Post a Comment