Saturday, June 18, 2005

Java DatabaseMetaData.getDriver()

In common with other parts of the Bodington system the mapping of Java objects onto a relational database (O/R mapping) is done by some custom code[1]. This code needs to know which database product it is connecting to so that it can work correctly (JDBC API and SQL are almost there but not quite). Now normally this is done by looking at the JDBC class name and setting the flags accordingly ("org.postgresql.Driver" is the PostgreSQL driver). When Bodington is making the JDBC connection itself this is not a problem as it knows the driver class. However I have recently been allowing it to use a JNDI database connection pool supplied from Tomcat. Now when you get your pooled connection from Tomcat you don't know what database product you are connected to. I was hoping that I would be able to do something like:
connection.getMetaData().getDriver();
which would return me the class that is being used as the driver from which I could work out how to configure the database mapping. But no, this call doesn't exist. I can find out the display name of the driver, the version, but not the class. This means that if you are using JNDI to get your database connection you will have to tell Bodington the flavour of database it is connecting to which is unneccessary configuration as you have already given this information to Tomcat and just leads to situations where you are connecting to PostgreSQL but Bodington is configured to talk to Oracle. [1] - The reason for this is that it was written a long time (around 1999) before product like Hibernate and iBatis became the established way of doing this.